Skip to content

Commit 3e20df5

Browse files
committed
Use rgba 1,1,1,1 as "filler" pixel
Some code we had was assuming that everything fully transparent was intentionally added by us as filler for missing content on the right of either the before or after image. To make things more explicit, I'm using the term "filler pixel" here and setting it to always be 1,1,1,1. This way the intentions in the code become clearer and at the same time we have less risk of collisions with a transparent pixel that we do not control.
1 parent 59a59e5 commit 3e20df5

File tree

4 files changed

+20
-22
lines changed

4 files changed

+20
-22
lines changed

src/__tests__/colorDelta-test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +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);
3535
});
3636

37-
it('is one when comparing intentionally transparent and some other color', () => {
38-
expect(colorDelta([0, 0, 0, 0], [33, 33, 33, 10])).toEqual(1);
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);
3939
});
4040

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

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: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,8 @@ function blend(color, alpha) {
1717
return 255 + (color - 255) * alpha;
1818
}
1919

20-
function isIntentionalTransparent([r, g, b, a]) {
21-
if (a !== 0) {
22-
return false;
23-
}
24-
if (r === g && g === b && (r === 255 || r === 0)) {
25-
return true;
26-
}
27-
return false;
20+
function isFillerPixel([r, g, b, a]) {
21+
return r === 1 && g === 1 && b === 1 && a === 1;
2822
}
2923

3024
// calculate color difference according to the paper "Measuring perceived color
@@ -41,8 +35,8 @@ module.exports = function colorDelta(previousPixel, currentPixel) {
4135
}
4236

4337
if (
44-
(isIntentionalTransparent(currentPixel) && a1 > 0) ||
45-
(isIntentionalTransparent(previousPixel) && a2 > 0)
38+
(isFillerPixel(currentPixel) && a1 > 0) ||
39+
(isFillerPixel(previousPixel) && a2 > 0)
4640
) {
4741
return 1;
4842
}

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)