1
- "use strict" ;
2
-
3
1
/**
4
2
* @author Markus Ekholm
5
3
* @copyright 2012-2023 (c) Markus Ekholm <markus at botten dot org >
29
27
*/
30
28
31
29
/**
32
- * EXPORTS
30
+ * IMPORTS
33
31
*/
34
-
35
- module . exports = { ciede2000 } ;
32
+ import { rgbaToLab } from "./convert.js" ;
36
33
37
34
/**
38
- * IMPORTS
35
+ * EXPORTS
39
36
*/
40
- const sqrt = Math . sqrt ;
41
- const pow = Math . pow ;
42
- const cos = Math . cos ;
43
- const atan2 = Math . atan2 ;
44
- const sin = Math . sin ;
45
- const abs = Math . abs ;
46
- const exp = Math . exp ;
47
- const PI = Math . PI ;
48
-
49
- const { rgbaToLab } = require ( "./convert" ) ;
37
+ export { ciede2000 } ;
50
38
51
39
/**
52
40
* TYPES
@@ -101,19 +89,19 @@ function ciede2000(c1, c2, bc) {
101
89
/**
102
90
* Step 1: Calculate C1p, C2p, h1p, h2p
103
91
*/
104
- const C1 = sqrt ( pow ( a1 , 2 ) + pow ( b1 , 2 ) ) ; // (2)
105
- const C2 = sqrt ( pow ( a2 , 2 ) + pow ( b2 , 2 ) ) ; // (2)
92
+ const C1 = Math . sqrt ( Math . pow ( a1 , 2 ) + Math . pow ( b1 , 2 ) ) ; // (2)
93
+ const C2 = Math . sqrt ( Math . pow ( a2 , 2 ) + Math . pow ( b2 , 2 ) ) ; // (2)
106
94
107
95
const aC1C2 = ( C1 + C2 ) / 2.0 ; // (3)
108
96
109
- const G = 0.5 * ( 1 - sqrt ( pow ( aC1C2 , 7.0 ) /
110
- ( pow ( aC1C2 , 7.0 ) + pow ( 25.0 , 7.0 ) ) ) ) ; // (4)
97
+ const G = 0.5 * ( 1 - Math . sqrt ( Math . pow ( aC1C2 , 7.0 ) /
98
+ ( Math . pow ( aC1C2 , 7.0 ) + Math . pow ( 25.0 , 7.0 ) ) ) ) ; // (4)
111
99
112
100
const a1p = ( 1.0 + G ) * a1 ; // (5)
113
101
const a2p = ( 1.0 + G ) * a2 ; // (5)
114
102
115
- const C1p = sqrt ( pow ( a1p , 2 ) + pow ( b1 , 2 ) ) ; // (6)
116
- const C2p = sqrt ( pow ( a2p , 2 ) + pow ( b2 , 2 ) ) ; // (6)
103
+ const C1p = Math . sqrt ( Math . pow ( a1p , 2 ) + Math . pow ( b1 , 2 ) ) ; // (6)
104
+ const C2p = Math . sqrt ( Math . pow ( a2p , 2 ) + Math . pow ( b2 , 2 ) ) ; // (6)
117
105
118
106
const h1p = hpF ( b1 , a1p ) ; // (7)
119
107
const h2p = hpF ( b2 , a2p ) ; // (7)
@@ -125,7 +113,7 @@ function ciede2000(c1, c2, bc) {
125
113
const dCp = C2p - C1p ; // (9)
126
114
127
115
const dhp = dhpF ( C1 , C2 , h1p , h2p ) ; // (10)
128
- const dHp = 2 * sqrt ( C1p * C2p ) * sin ( radians ( dhp ) / 2.0 ) ; // (11)
116
+ const dHp = 2 * Math . sqrt ( C1p * C2p ) * Math . sin ( radians ( dhp ) / 2.0 ) ; // (11)
129
117
130
118
/**
131
119
* Step 3: Calculate CIEDE2000 Color-Difference
@@ -134,17 +122,17 @@ function ciede2000(c1, c2, bc) {
134
122
const aCp = ( C1p + C2p ) / 2.0 ; // (13)
135
123
136
124
const aHp = aHpF ( C1 , C2 , h1p , h2p ) ; // (14)
137
- const T = 1 - 0.17 * cos ( radians ( aHp - 30 ) ) + 0.24 * cos ( radians ( 2 * aHp ) ) +
138
- 0.32 * cos ( radians ( 3 * aHp + 6 ) ) - 0.20 * cos ( radians ( 4 * aHp - 63 ) ) ; // (15)
139
- const dRo = 30 * exp ( - ( pow ( ( aHp - 275 ) / 25 , 2 ) ) ) ; // (16)
140
- const RC = sqrt ( ( pow ( aCp , 7.0 ) ) / ( pow ( aCp , 7.0 ) + pow ( 25.0 , 7.0 ) ) ) ; // (17)
141
- const SL = 1 + ( ( 0.015 * pow ( aL - 50 , 2 ) ) /
142
- sqrt ( 20 + pow ( aL - 50 , 2.0 ) ) ) ; // (18)
125
+ const T = 1 - 0.17 * Math . cos ( radians ( aHp - 30 ) ) + 0.24 * Math . cos ( radians ( 2 * aHp ) ) +
126
+ 0.32 * Math . cos ( radians ( 3 * aHp + 6 ) ) - 0.20 * Math . cos ( radians ( 4 * aHp - 63 ) ) ; // (15)
127
+ const dRo = 30 * Math . exp ( - ( Math . pow ( ( aHp - 275 ) / 25 , 2 ) ) ) ; // (16)
128
+ const RC = Math . sqrt ( ( Math . pow ( aCp , 7.0 ) ) / ( Math . pow ( aCp , 7.0 ) + Math . pow ( 25.0 , 7.0 ) ) ) ; // (17)
129
+ const SL = 1 + ( ( 0.015 * Math . pow ( aL - 50 , 2 ) ) /
130
+ Math . sqrt ( 20 + Math . pow ( aL - 50 , 2.0 ) ) ) ; // (18)
143
131
const SC = 1 + 0.045 * aCp ; // (19)
144
132
const SH = 1 + 0.015 * aCp * T ; // (20)
145
- const RT = - 2 * RC * sin ( radians ( 2 * dRo ) ) ; // (21)
146
- const dE = sqrt ( pow ( dLp / ( SL * kL ) , 2 ) + pow ( dCp / ( SC * kC ) , 2 ) +
147
- pow ( dHp / ( SH * kH ) , 2 ) + RT * ( dCp / ( SC * kC ) ) *
133
+ const RT = - 2 * RC * Math . sin ( radians ( 2 * dRo ) ) ; // (21)
134
+ const dE = Math . sqrt ( Math . pow ( dLp / ( SL * kL ) , 2 ) + Math . pow ( dCp / ( SC * kC ) , 2 ) +
135
+ Math . pow ( dHp / ( SH * kH ) , 2 ) + RT * ( dCp / ( SC * kC ) ) *
148
136
( dHp / ( SH * kH ) ) ) ; // (22)
149
137
return dE ;
150
138
}
@@ -159,7 +147,7 @@ function ciede2000(c1, c2, bc) {
159
147
* @returns {number }
160
148
*/
161
149
function degrees ( n ) {
162
- return n * ( 180 / PI ) ;
150
+ return n * ( 180 / Math . PI ) ;
163
151
}
164
152
165
153
/**
@@ -168,7 +156,7 @@ function degrees(n) {
168
156
* @returns number
169
157
*/
170
158
function radians ( n ) {
171
- return n * ( PI / 180 ) ;
159
+ return n * ( Math . PI / 180 ) ;
172
160
}
173
161
174
162
/**
@@ -180,7 +168,7 @@ function radians(n) {
180
168
function hpF ( x , y ) { // (7)
181
169
if ( x === 0 && y === 0 ) return 0 ;
182
170
else {
183
- const tmphp = degrees ( atan2 ( x , y ) ) ;
171
+ const tmphp = degrees ( Math . atan2 ( x , y ) ) ;
184
172
if ( tmphp >= 0 ) return tmphp ;
185
173
else return tmphp + 360 ;
186
174
}
@@ -196,7 +184,7 @@ function hpF(x, y) { // (7)
196
184
*/
197
185
function dhpF ( C1 , C2 , h1p , h2p ) { // (10)
198
186
if ( C1 * C2 === 0 ) return 0 ;
199
- else if ( abs ( h2p - h1p ) <= 180 ) return h2p - h1p ;
187
+ else if ( Math . abs ( h2p - h1p ) <= 180 ) return h2p - h1p ;
200
188
else if ( ( h2p - h1p ) > 180 ) return ( h2p - h1p ) - 360 ;
201
189
else if ( ( h2p - h1p ) < - 180 ) return ( h2p - h1p ) + 360 ;
202
190
else throw ( new Error ( ) ) ;
@@ -212,8 +200,8 @@ function dhpF(C1, C2, h1p, h2p) { // (10)
212
200
*/
213
201
function aHpF ( C1 , C2 , h1p , h2p ) { // (14)
214
202
if ( C1 * C2 === 0 ) return h1p + h2p ;
215
- else if ( abs ( h1p - h2p ) <= 180 ) return ( h1p + h2p ) / 2.0 ;
216
- else if ( ( abs ( h1p - h2p ) > 180 ) && ( ( h1p + h2p ) < 360 ) ) return ( h1p + h2p + 360 ) / 2.0 ;
217
- else if ( ( abs ( h1p - h2p ) > 180 ) && ( ( h1p + h2p ) >= 360 ) ) return ( h1p + h2p - 360 ) / 2.0 ;
203
+ else if ( Math . abs ( h1p - h2p ) <= 180 ) return ( h1p + h2p ) / 2.0 ;
204
+ else if ( ( Math . abs ( h1p - h2p ) > 180 ) && ( ( h1p + h2p ) < 360 ) ) return ( h1p + h2p + 360 ) / 2.0 ;
205
+ else if ( ( Math . abs ( h1p - h2p ) > 180 ) && ( ( h1p + h2p ) >= 360 ) ) return ( h1p + h2p - 360 ) / 2.0 ;
218
206
else throw ( new Error ( ) ) ;
219
207
}
0 commit comments