Skip to content

Commit d772300

Browse files
authored
Merge pull request #37 from happo/intentional-transparency
Handle almost-transparent and transparent pixels better
2 parents 809bb90 + 7d6a88f commit d772300

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

snapshots/logo/diff.png

613 Bytes
Loading

src/__tests__/colorDelta-test.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,22 @@ it('is medium when comparing red and blue', () => {
2222
expect(delta).toBeLessThan(0.51);
2323
});
2424

25-
it('is one when comparing transparent and white', () => {
26-
expect(colorDelta([0, 0, 0, 0], [255, 255, 255, 255])).toEqual(1);
25+
it('is one when comparing filler pixel and white', () => {
26+
expect(colorDelta([1, 1, 1, 1], [255, 255, 255, 255])).toEqual(1);
2727
});
2828

2929
it('is large when comparing transparent and black', () => {
3030
expect(colorDelta([0, 0, 0, 0], [0, 0, 0, 255])).toBeGreaterThan(0.92);
3131
});
3232

33-
it('is large when comparing white and transparent', () => {
34-
expect(colorDelta([255, 255, 255, 255], [0, 0, 0, 0])).toBeGreaterThan(0.92);
33+
it('is large when comparing white and filler pixel', () => {
34+
expect(colorDelta([255, 255, 255, 255], [1, 1, 1, 1])).toBeGreaterThan(0.92);
35+
});
36+
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);
39+
});
40+
41+
it('is small when comparing transparent and similar color', () => {
42+
expect(colorDelta([1, 46, 250, 0], [1, 42, 250, 4])).toBeLessThan(0.05);
3543
});

src/__tests__/getDiffPixel-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ it('returns magenta when diff', () => {
2222
});
2323
});
2424

25-
it('returns diff when after alpha is zero', () => {
26-
currentPixel[3] = 0;
25+
it('returns diff when after is filler pixel', () => {
26+
currentPixel = [1, 1, 1, 1];
2727
expect(subject()).toEqual({
2828
diff: 1,
2929
pixel: [179, 54, 130, 255],
3030
});
3131
});
3232

33-
it('returns diff when before alpha is zero', () => {
34-
previousPixel[3] = 0;
33+
it('returns diff when before is filler pixel', () => {
34+
previousPixel = [1, 1, 1, 1];
3535
expect(subject()).toEqual({
3636
diff: 1,
3737
pixel: [179, 54, 130, 255],

src/colorDelta.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ function blend(color, alpha) {
1717
return 255 + (color - 255) * alpha;
1818
}
1919

20+
function isFillerPixel([r, g, b, a]) {
21+
return r === 1 && g === 1 && b === 1 && a === 1;
22+
}
23+
2024
// calculate color difference according to the paper "Measuring perceived color
2125
// difference using YIQ NTSC transmission color space in mobile applications" by
2226
// Y. Kotsarenko and F. Ramos
@@ -30,7 +34,10 @@ module.exports = function colorDelta(previousPixel, currentPixel) {
3034
return 0;
3135
}
3236

33-
if ((a2 === 0 && a1 > 0) || (a1 === 0 && a2 > 0)) {
37+
if (
38+
(isFillerPixel(currentPixel) && a1 > 0) ||
39+
(isFillerPixel(previousPixel) && a2 > 0)
40+
) {
3441
return 1;
3542
}
3643

src/computeAndInjectDiffs.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ function imageTo2DArray({ data, width, height }, paddingRight) {
1313
for (let location = 0; location < rowSize; location += 1) {
1414
pixelsInRow[location] = data[row * rowSize + location];
1515
}
16+
for (let location = rowSize; location < rowSize + (paddingRight * 4); location += 1) {
17+
pixelsInRow[location] = 1;
18+
}
19+
1620
newData.push(pixelsInRow);
1721
}
1822
return newData;

0 commit comments

Comments
 (0)