Skip to content

Commit 2086a4b

Browse files
authored
Merge pull request #40 from happo/no-array-destructuring
Remove array destructuring in colorDelta, add colorDeltaChannels, deprecate colorDelta
2 parents 98eb1d4 + 77cf1e1 commit 2086a4b

File tree

2 files changed

+94
-33
lines changed

2 files changed

+94
-33
lines changed

src/__tests__/colorDelta-test.js

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,76 @@
1-
const colorDelta = require('../colorDelta');
1+
const colorDelta = require("../colorDelta");
2+
const { colorDeltaChannels } = require("../colorDelta");
23

3-
it('is large when comparing black and white', () => {
4-
expect(colorDelta([0, 0, 0, 255], [255, 255, 255, 255])).toBeGreaterThan(
5-
0.92,
4+
describe("colorDelta", () => {
5+
it("produces the same results as colorDeltaChannels", () => {
6+
const pixels = [
7+
[0, 0, 0, 255],
8+
[255, 255, 255, 255],
9+
[0, 0, 0, 0],
10+
[1, 1, 1, 1],
11+
[1, 46, 250, 0],
12+
[1, 42, 250, 4],
13+
];
14+
15+
for (let i = 0; i < pixels.length; i++) {
16+
for (let j = 0; j < pixels.length; j++) {
17+
expect(colorDelta(pixels[i], pixels[j])).toEqual(
18+
colorDeltaChannels(
19+
pixels[i][0],
20+
pixels[i][1],
21+
pixels[i][2],
22+
pixels[i][3],
23+
pixels[j][0],
24+
pixels[j][1],
25+
pixels[j][2],
26+
pixels[j][3]
27+
)
28+
);
29+
}
30+
}
31+
});
32+
});
33+
34+
it("is large when comparing black and white", () => {
35+
expect(colorDeltaChannels(0, 0, 0, 255, 255, 255, 255, 255)).toBeGreaterThan(
36+
0.92
637
);
738
});
839

9-
it('is small when comparing black and very dark grey', () => {
10-
expect(colorDelta([0, 0, 0, 255], [10, 10, 10, 255])).toBeLessThan(0.02);
40+
it("is small when comparing black and very dark grey", () => {
41+
expect(colorDeltaChannels(0, 0, 0, 255, 10, 10, 10, 255)).toBeLessThan(0.02);
1142
});
1243

13-
it('is medium when comparing black and medium grey', () => {
14-
const delta = colorDelta([0, 0, 0, 255], [127, 127, 127, 255]);
44+
it("is medium when comparing black and medium grey", () => {
45+
const delta = colorDeltaChannels(0, 0, 0, 255, 127, 127, 127, 255);
1546
expect(delta).toBeGreaterThan(0.21);
1647
expect(delta).toBeLessThan(0.24);
1748
});
1849

19-
it('is medium when comparing red and blue', () => {
20-
const delta = colorDelta([255, 0, 0, 255], [0, 0, 255, 255]);
50+
it("is medium when comparing red and blue", () => {
51+
const delta = colorDeltaChannels(255, 0, 0, 255, 0, 0, 255, 255);
2152
expect(delta).toBeGreaterThan(0.5);
2253
expect(delta).toBeLessThan(0.51);
2354
});
2455

25-
it('is one when comparing filler pixel and white', () => {
26-
expect(colorDelta([1, 1, 1, 1], [255, 255, 255, 255])).toEqual(1);
56+
it("is one when comparing filler pixel and white", () => {
57+
expect(colorDeltaChannels(1, 1, 1, 1, 255, 255, 255, 255)).toEqual(1);
2758
});
2859

29-
it('is large when comparing transparent and black', () => {
30-
expect(colorDelta([0, 0, 0, 0], [0, 0, 0, 255])).toBeGreaterThan(0.92);
60+
it("is large when comparing transparent and black", () => {
61+
expect(colorDeltaChannels(0, 0, 0, 0, 0, 0, 0, 255)).toBeGreaterThan(0.92);
3162
});
3263

33-
it('is large when comparing white and filler pixel', () => {
34-
expect(colorDelta([255, 255, 255, 255], [1, 1, 1, 1])).toBeGreaterThan(0.92);
64+
it("is large when comparing white and filler pixel", () => {
65+
expect(colorDeltaChannels(255, 255, 255, 255, 1, 1, 1, 1)).toBeGreaterThan(
66+
0.92
67+
);
3568
});
3669

37-
it('is one when comparing filler pixel and some other color', () => {
38-
expect(colorDelta([1, 1, 1, 1], [33, 33, 33, 10])).toEqual(1);
70+
it("is one when comparing filler pixel and some other color", () => {
71+
expect(colorDeltaChannels(1, 1, 1, 1, 33, 33, 33, 10)).toEqual(1);
3972
});
4073

41-
it('is small when comparing transparent and similar color', () => {
42-
expect(colorDelta([1, 46, 250, 0], [1, 42, 250, 4])).toBeLessThan(0.05);
74+
it("is small when comparing transparent and similar color", () => {
75+
expect(colorDeltaChannels(1, 46, 250, 0, 1, 42, 250, 4)).toBeLessThan(0.05);
4376
});

src/colorDelta.js

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,27 @@ function blend(color, alpha) {
1717
return 255 + (color - 255) * alpha;
1818
}
1919

20-
function isFillerPixel([r, g, b, a]) {
20+
function isFillerPixel(r, g, b, a) {
2121
return r === 1 && g === 1 && b === 1 && a === 1;
2222
}
2323

24-
// calculate color difference according to the paper "Measuring perceived color
25-
// difference using YIQ NTSC transmission color space in mobile applications" by
26-
// Y. Kotsarenko and F. Ramos
27-
//
28-
// Modified from https://github.com/mapbox/pixelmatch
29-
module.exports = function colorDelta(previousPixel, currentPixel) {
30-
let [r1, g1, b1, a1] = previousPixel;
31-
let [r2, g2, b2, a2] = currentPixel;
32-
24+
/**
25+
* Calculate color difference between two pixels
26+
*
27+
* The difference is calculated according to the paper "Measuring perceived
28+
* color difference using YIQ NTSC transmission color space in mobile
29+
* applications" by Y. Kotsarenko and F. Ramos.
30+
*
31+
* Modified from https://github.com/mapbox/pixelmatch
32+
*/
33+
function colorDeltaChannels(r1, g1, b1, a1, r2, g2, b2, a2) {
3334
if (r1 === r2 && g1 === g2 && b1 === b2 && a1 === a2) {
3435
return 0;
3536
}
3637

3738
if (
38-
(isFillerPixel(currentPixel) && a1 > 0) ||
39-
(isFillerPixel(previousPixel) && a2 > 0)
39+
(isFillerPixel(r1, g1, b1, a1) && a1 > 0) ||
40+
(isFillerPixel(r2, g2, b2, a2) && a2 > 0)
4041
) {
4142
return 1;
4243
}
@@ -60,4 +61,31 @@ module.exports = function colorDelta(previousPixel, currentPixel) {
6061
const q = rgb2q(r1, g1, b1) - rgb2q(r2, g2, b2);
6162

6263
return (0.5053 * y * y + 0.299 * i * i + 0.1957 * q * q) / MAX_YIQ_DIFFERENCE;
63-
};
64+
}
65+
66+
/**
67+
* Calculate color difference between two pixels
68+
*
69+
* The difference is calculated according to the paper "Measuring perceived
70+
* color difference using YIQ NTSC transmission color space in mobile
71+
* applications" by Y. Kotsarenko and F. Ramos.
72+
*
73+
* Modified from https://github.com/mapbox/pixelmatch
74+
*
75+
* @deprecated use `colorDeltaChannels` instead
76+
*/
77+
function colorDelta(previousPixel, currentPixel) {
78+
return colorDeltaChannels(
79+
previousPixel[0],
80+
previousPixel[1],
81+
previousPixel[2],
82+
previousPixel[3],
83+
currentPixel[0],
84+
currentPixel[1],
85+
currentPixel[2],
86+
currentPixel[3]
87+
);
88+
}
89+
90+
module.exports = colorDelta;
91+
module.exports.colorDeltaChannels = colorDeltaChannels;

0 commit comments

Comments
 (0)