Skip to content

Commit 8bf8e74

Browse files
authored
Merge pull request markusn#22 from markusn/esm-module
convert to esm module, cjs support using rollup
2 parents 0a7010e + 2b4b95b commit 8bf8e74

12 files changed

+234
-1514
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,6 @@ temp/
4949
coverage/
5050
.eslintcache
5151
.nyc_output
52+
53+
# built using rollup
54+
index.cjs

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.4.0
4+
5+
* Use ESM modules. Backwards compatible with CJS using rollup.
6+
* Replaced nyc with c8 (nyc does not support esm).
7+
38
## 1.3.0
49

510
* don't support mixing casing for r,g,b properties, either all upper or all lower

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# color-diff
2+
23
[![Build Status](https://github.com/markusn/color-diff/actions/workflows/build-latest.yaml/badge.svg?branch=master)](https://github.com/markusn/color-diff/actions/workflows/build-latest.yaml)
34
[![Coverage Status](https://coveralls.io/repos/markusn/color-diff/badge.png?branch=master)](https://coveralls.io/r/markusn/color-diff?branch=master)
45

@@ -23,6 +24,7 @@ npm test
2324
## Usage
2425

2526
```js
27+
// CommonJS
2628
const {
2729
closest,
2830
furthest,
@@ -32,7 +34,19 @@ const {
3234
rgbaToLab,
3335
mapPaletteLab,
3436
labPaletteMapKey,
35-
} = require('color-diff');
37+
} = require("color-diff");
38+
39+
// ESM
40+
import {
41+
closest,
42+
furthest,
43+
diff,
44+
mapPalette,
45+
paletteMapKey,
46+
rgbaToLab,
47+
mapPaletteLab,
48+
labPaletteMapKey,
49+
} from "color-diff";
3650
```
3751

3852
### closest(color, palette, bc)

index.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use strict";
2-
31
// Types
42

53
/**
@@ -21,18 +19,18 @@
2119
*/
2220

2321
// Imports
24-
const { rgbaToLab } = require("./lib/convert");
25-
const { ciede2000 } = require("./lib/diff");
26-
const {
22+
import { rgbaToLab } from "./lib/convert.js";
23+
import { ciede2000 } from "./lib/diff.js";
24+
import {
2725
mapPalette,
2826
paletteMapKey,
2927
matchPaletteLab,
3028
mapPaletteLab,
3129
labPaletteMapKey,
32-
} = require("./lib/palette");
30+
} from "./lib/palette.js";
3331

3432
// Exports
35-
module.exports = {
33+
export {
3634
closest,
3735
closestLab,
3836
furthest,
@@ -42,7 +40,7 @@ module.exports = {
4240
matchPaletteLab,
4341
mapPaletteLab,
4442
labPaletteMapKey,
45-
diff: ciede2000,
43+
ciede2000 as diff,
4644
rgbaToLab,
4745
// eslint-disable-next-line camelcase
4846
rgb_to_lab,

lib/convert.js

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use strict";
2-
31
/**
42
* @author Markus Ekholm
53
* @copyright 2012-2023 (c) Markus Ekholm <markus at botten dot org >
@@ -31,12 +29,7 @@
3129
/**
3230
* EXPORTS
3331
*/
34-
module.exports = { rgbaToLab, normalize };
35-
36-
/**
37-
* IMPORTS
38-
*/
39-
const pow = Math.pow;
32+
export { rgbaToLab, normalize };
4033

4134
/**
4235
* TYPES
@@ -90,11 +83,11 @@ function rgbToXyz(c) {
9083
let G = (c.G / 255);
9184
let B = (c.B / 255);
9285

93-
if (R > 0.04045) R = pow(((R + 0.055) / 1.055), 2.4);
86+
if (R > 0.04045) R = Math.pow(((R + 0.055) / 1.055), 2.4);
9487
else R = R / 12.92;
95-
if (G > 0.04045) G = pow(((G + 0.055) / 1.055), 2.4);
88+
if (G > 0.04045) G = Math.pow(((G + 0.055) / 1.055), 2.4);
9689
else G = G / 12.92;
97-
if (B > 0.04045) B = pow(((B + 0.055) / 1.055), 2.4);
90+
if (B > 0.04045) B = Math.pow(((B + 0.055) / 1.055), 2.4);
9891
else B = B / 12.92;
9992

10093
R *= 100;
@@ -121,11 +114,11 @@ function xyzToLab(c) {
121114
let Y = c.Y / refY;
122115
let Z = c.Z / refZ;
123116
let X = c.X / refX;
124-
if (X > 0.008856) X = pow(X, 1 / 3);
117+
if (X > 0.008856) X = Math.pow(X, 1 / 3);
125118
else X = (7.787 * X) + (16 / 116);
126-
if (Y > 0.008856) Y = pow(Y, 1 / 3);
119+
if (Y > 0.008856) Y = Math.pow(Y, 1 / 3);
127120
else Y = (7.787 * Y) + (16 / 116);
128-
if (Z > 0.008856) Z = pow(Z, 1 / 3);
121+
if (Z > 0.008856) Z = Math.pow(Z, 1 / 3);
129122
else Z = (7.787 * Z) + (16 / 116);
130123
const L = (116 * Y) - 16;
131124
const a = 500 * (X - Y);

lib/diff.js

+27-39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use strict";
2-
31
/**
42
* @author Markus Ekholm
53
* @copyright 2012-2023 (c) Markus Ekholm <markus at botten dot org >
@@ -29,24 +27,14 @@
2927
*/
3028

3129
/**
32-
* EXPORTS
30+
* IMPORTS
3331
*/
34-
35-
module.exports = { ciede2000 };
32+
import { rgbaToLab } from "./convert.js";
3633

3734
/**
38-
* IMPORTS
35+
* EXPORTS
3936
*/
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 };
5038

5139
/**
5240
* TYPES
@@ -101,19 +89,19 @@ function ciede2000(c1, c2, bc) {
10189
/**
10290
* Step 1: Calculate C1p, C2p, h1p, h2p
10391
*/
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)
10694

10795
const aC1C2 = (C1 + C2) / 2.0; // (3)
10896

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)
11199

112100
const a1p = (1.0 + G) * a1; // (5)
113101
const a2p = (1.0 + G) * a2; // (5)
114102

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)
117105

118106
const h1p = hpF(b1, a1p); // (7)
119107
const h2p = hpF(b2, a2p); // (7)
@@ -125,7 +113,7 @@ function ciede2000(c1, c2, bc) {
125113
const dCp = C2p - C1p; // (9)
126114

127115
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)
129117

130118
/**
131119
* Step 3: Calculate CIEDE2000 Color-Difference
@@ -134,17 +122,17 @@ function ciede2000(c1, c2, bc) {
134122
const aCp = (C1p + C2p) / 2.0; // (13)
135123

136124
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)
143131
const SC = 1 + 0.045 * aCp;// (19)
144132
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)) *
148136
(dHp / (SH * kH))); // (22)
149137
return dE;
150138
}
@@ -159,7 +147,7 @@ function ciede2000(c1, c2, bc) {
159147
* @returns {number}
160148
*/
161149
function degrees(n) {
162-
return n * (180 / PI);
150+
return n * (180 / Math.PI);
163151
}
164152

165153
/**
@@ -168,7 +156,7 @@ function degrees(n) {
168156
* @returns number
169157
*/
170158
function radians(n) {
171-
return n * (PI / 180);
159+
return n * (Math.PI / 180);
172160
}
173161

174162
/**
@@ -180,7 +168,7 @@ function radians(n) {
180168
function hpF(x, y) { // (7)
181169
if (x === 0 && y === 0) return 0;
182170
else {
183-
const tmphp = degrees(atan2(x, y));
171+
const tmphp = degrees(Math.atan2(x, y));
184172
if (tmphp >= 0) return tmphp;
185173
else return tmphp + 360;
186174
}
@@ -196,7 +184,7 @@ function hpF(x, y) { // (7)
196184
*/
197185
function dhpF(C1, C2, h1p, h2p) { // (10)
198186
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;
200188
else if ((h2p - h1p) > 180) return (h2p - h1p) - 360;
201189
else if ((h2p - h1p) < -180) return (h2p - h1p) + 360;
202190
else throw (new Error());
@@ -212,8 +200,8 @@ function dhpF(C1, C2, h1p, h2p) { // (10)
212200
*/
213201
function aHpF(C1, C2, h1p, h2p) { // (14)
214202
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;
218206
else throw (new Error());
219207
}

lib/palette.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use strict";
2-
31
/**
42
* @author Markus Ekholm
53
* @copyright 2012-2023 (c) Markus Ekholm <markus at botten dot org >
@@ -28,23 +26,23 @@
2826
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2927
*/
3028

29+
/**
30+
* IMPORTS
31+
*/
32+
import { ciede2000 } from "./diff.js";
33+
import { normalize } from "./convert.js";
34+
3135
/**
3236
* EXPORTS
3337
*/
34-
module.exports = {
38+
export {
3539
mapPalette,
3640
mapPaletteLab,
3741
matchPaletteLab,
3842
paletteMapKey,
3943
labPaletteMapKey,
4044
};
4145

42-
/**
43-
* IMPORTS
44-
*/
45-
const { ciede2000 } = require("./diff");
46-
const { normalize } = require("./convert");
47-
4846
/**
4947
* TYPES
5048
*/

0 commit comments

Comments
 (0)