Skip to content

Commit d337f54

Browse files
authored
Merge pull request #17 from happo/different-size-diffs
Improve showing different dimension diffs
2 parents b80a8c5 + 27991a7 commit d337f54

17 files changed

+941
-840
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ jobs:
77
steps:
88
- checkout
99
- run: yarn install
10-
- run: yarn test
10+
- run: yarn test-ci

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
],
1313
"license": "MIT",
1414
"scripts": {
15+
"start-server": "http-server -p 5411 -c1 src/__tests__/test-images",
16+
"test-ci": "yarn start-server & yarn test",
1517
"test": "jest"
1618
},
1719
"dependencies": {
1820
"imagetracerjs": "^1.2.5"
1921
},
2022
"devDependencies": {
23+
"http-server": "^0.12.3",
2124
"jest": "^24.8.0",
2225
"jimp": "^0.8.5"
2326
}

snapshots/logo/diff.png

-897 Bytes
Loading

snapshots/long-example/diff.png

-644 Bytes
Loading

src/__tests__/colorDelta-test.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const colorDelta = require('../colorDelta');
22

33
it('is large when comparing black and white', () => {
4-
expect(colorDelta([0, 0, 0, 255], [255, 255, 255, 255]))
5-
.toBeGreaterThan(0.92);
4+
expect(colorDelta([0, 0, 0, 255], [255, 255, 255, 255])).toBeGreaterThan(
5+
0.92,
6+
);
67
});
78

89
it('is small when comparing black and very dark grey', () => {
9-
expect(colorDelta([0, 0, 0, 255], [10, 10, 10, 255]))
10-
.toBeLessThan(0.02);
10+
expect(colorDelta([0, 0, 0, 255], [10, 10, 10, 255])).toBeLessThan(0.02);
1111
});
1212

1313
it('is medium when comparing black and medium grey', () => {
@@ -22,12 +22,14 @@ it('is medium when comparing red and blue', () => {
2222
expect(delta).toBeLessThan(0.51);
2323
});
2424

25-
it('is zero when comparing transparent and white', () => {
26-
expect(colorDelta([0, 0, 0, 0], [255, 255, 255, 255]))
27-
.toEqual(0);
25+
it('is one when comparing transparent and white', () => {
26+
expect(colorDelta([0, 0, 0, 0], [255, 255, 255, 255])).toEqual(1);
2827
});
2928

3029
it('is large when comparing transparent and black', () => {
31-
expect(colorDelta([0, 0, 0, 0], [0, 0, 0, 255]))
32-
.toBeGreaterThan(0.92);
30+
expect(colorDelta([0, 0, 0, 0], [0, 0, 0, 255])).toBeGreaterThan(0.92);
31+
});
32+
33+
it('is large when comparing white and transparent', () => {
34+
expect(colorDelta([255, 255, 255, 255], [0, 0, 0, 0])).toBeGreaterThan(0.92);
3335
});

src/__tests__/createDiffImage-test.js

+28-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@ let image2;
1111
let subject;
1212

1313
beforeEach(async () => {
14-
image1 = (await Jimp.read(
15-
'https://dummyimage.com/200/000/ffffff.png&text=aa',
16-
)).bitmap;
17-
image2 = (await Jimp.read(
18-
'https://dummyimage.com/200/000/f7f7f7.png&text=aa',
19-
)).bitmap;
14+
image1 = (await Jimp.read('http://localhost:5411/aa-ffffff.png')).bitmap;
15+
image2 = (await Jimp.read('http://localhost:5411/aa-f7f7f7.png')).bitmap;
2016
subject = () =>
2117
createDiffImage(
2218
computeAndInjectDiffs({
@@ -31,3 +27,29 @@ it('has a total diff value and a max diff', async () => {
3127
expect(diff).toEqual(0.000013924627638992437);
3228
expect(maxDiff).toEqual(0.0009183359547574563);
3329
});
30+
31+
describe('when images are of different width', () => {
32+
beforeEach(async () => {
33+
image1 = (await Jimp.read('http://localhost:5411/alert-before.png')).bitmap;
34+
image2 = (await Jimp.read('http://localhost:5411/alert-after.png')).bitmap;
35+
});
36+
37+
it('has a total diff and a max diff', async () => {
38+
const { diff, maxDiff } = await subject();
39+
expect(diff).toEqual(0.20997431506849315);
40+
expect(maxDiff).toEqual(1);
41+
});
42+
});
43+
44+
describe('when images are of different height', () => {
45+
beforeEach(async () => {
46+
image1 = (await Jimp.read('http://localhost:5411/button-before.png')).bitmap;
47+
image2 = (await Jimp.read('http://localhost:5411/button-after.png')).bitmap;
48+
});
49+
50+
it('has a total diff and a max diff', async () => {
51+
const { diff, maxDiff } = await subject();
52+
expect(diff).toEqual(0.0078125);
53+
expect(maxDiff).toEqual(1);
54+
});
55+
});

src/__tests__/getDiffPixel-test.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const getDiffPixel = require('../getDiffPixel');
2+
3+
let subject;
4+
let previousPixel;
5+
let currentPixel;
6+
7+
beforeEach(() => {
8+
previousPixel = [255, 255, 255, 255];
9+
currentPixel = [255, 255, 255, 255];
10+
subject = () => getDiffPixel(previousPixel, currentPixel);
11+
});
12+
13+
it('returns semi-opaque source if no diff', () => {
14+
expect(subject()).toEqual({ diff: 0, pixel: [255, 255, 255, 140] });
15+
});
16+
17+
it('returns magenta when diff', () => {
18+
currentPixel = [120, 120, 255, 255];
19+
expect(subject()).toEqual({
20+
diff: 0.23089126029146917,
21+
pixel: [179, 54, 130, 58.877271374324636],
22+
});
23+
});
24+
25+
it('returns diff when after alpha is zero', () => {
26+
currentPixel[3] = 0;
27+
expect(subject()).toEqual({
28+
diff: 1,
29+
pixel: [179, 54, 130, 255],
30+
});
31+
});
32+
33+
it('returns diff when before alpha is zero', () => {
34+
previousPixel[3] = 0;
35+
expect(subject()).toEqual({
36+
diff: 1,
37+
pixel: [179, 54, 130, 255],
38+
});
39+
});

src/__tests__/index-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ it('produces a trace svg', () => {
3737

3838
it('has meta-data', () => {
3939
const { diff, maxDiff } = subject();
40-
expect(diff).toEqual(0.049155430620799745);
41-
expect(maxDiff).toEqual(0.7809273602519097);
40+
expect(diff).toEqual(0.20505992912433182);
41+
expect(maxDiff).toEqual(1);
4242
});
1.12 KB
Loading
1.12 KB
Loading
2.51 KB
Loading
2.19 KB
Loading
7.87 KB
Loading
7.86 KB
Loading

src/colorDelta.js

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ module.exports = function colorDelta(previousPixel, currentPixel) {
3030
return 0;
3131
}
3232

33+
if ((a2 === 0 && a1 > 0) || (a1 === 0 && a2 > 0)) {
34+
return 1;
35+
}
36+
3337
if (a1 < 255) {
3438
a1 /= 255;
3539
r1 = blend(r1, a1);

src/createDiffImage.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ const GREEN = [106, 133, 0, 255];
55
const MAGENTA = [197, 39, 114, 255];
66

77
function getDataIndex(row, width, index) {
8-
return (width * row) + index;
8+
return width * row + index;
99
}
1010

1111
module.exports = function createDiffImage({ image1Data, image2Data }) {
12+
// Images have the same width and height here
1213
const width = image1Data[0].length;
1314
const height = image1Data.length;
1415

0 commit comments

Comments
 (0)