Skip to content

Commit 0dae96a

Browse files
committed
fix #10 with the addition of a transparency option to control the alpha of unchanged pixels
1 parent fac1692 commit 0dae96a

7 files changed

Lines changed: 46 additions & 8 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ looksSame.createDiff({
7979
current: '/path/to/current/image.png',
8080
diff: '/path/to/save/diff/to.png',
8181
highlightColor: '#ff00ff', //color to highlight the differences
82-
strict: false,//strict comparsion
82+
transparency: 255, //0-255, controls the alpha channel for unchanged pixels
83+
strict: false,//strict comparison
8384
tolerance: 2.5
8485
}, function(error) {
8586
});

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ const buildDiffImage = (png1, png2, options, callback) => {
144144
const minWidth = Math.min(png1.width, png2.width);
145145
const minHeight = Math.min(png1.height, png2.height);
146146
const highlightColor = options.highlightColor;
147+
const alphaLevel = isNaN(options.transparency) ? 255 : options.transparency;
147148
const result = png.empty(width, height);
148149

149150
iterateRect(width, height, (x, y) => {
@@ -158,7 +159,7 @@ const buildDiffImage = (png1, png2, options, callback) => {
158159
if (!options.comparator({color1, color2})) {
159160
result.setPixel(x, y, highlightColor);
160161
} else {
161-
result.setPixel(x, y, color1);
162+
result.setPixel(x, y, color1, alphaLevel);
162163
}
163164
}, () => callback(result));
164165
};
@@ -216,7 +217,8 @@ exports.createDiff = function saveDiff(opts, callback) {
216217

217218
const diffOptions = {
218219
highlightColor: parseColorString(opts.highlightColor),
219-
comparator: opts.strict ? areColorsSame : makeCIEDE2000Comparator(tolerance)
220+
comparator: opts.strict ? areColorsSame : makeCIEDE2000Comparator(tolerance),
221+
transparency: opts.transparency
220222
};
221223

222224
buildDiffImage(result.first, result.second, diffOptions, (result) => {

lib/png.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,24 @@ class PNGImage {
2626
return {
2727
R: this._png.data[idx],
2828
G: this._png.data[idx + 1],
29-
B: this._png.data[idx + 2]
29+
B: this._png.data[idx + 2],
30+
A: this._png.data[idx + 3]
3031
};
3132
}
3233

3334
/**
3435
* Sets color data to pixel with given coordinates
3536
* @param {Number} x coordinate
3637
* @param {Number} y coordinate
37-
* @param {Object} color
38+
* @param {Object} color RGB formatted color {R:0-255, G:0-255, B:0-255}
39+
* @param {Number} [alpha=255] A level to set for the alpha channel, 0-255
3840
*/
39-
setPixel(x, y, color) {
41+
setPixel(x, y, color, alpha) {
4042
const idx = this._getIdx(x, y);
4143
this._png.data[idx] = color.R;
4244
this._png.data[idx + 1] = color.G;
4345
this._png.data[idx + 2] = color.B;
44-
this._png.data[idx + 3] = 255;
46+
this._png.data[idx + 3] = isNaN(alpha) ? 255 : alpha;
4547
}
4648

4749
/**

lib/same-colors.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ module.exports = (data) => {
66

77
return c1.R === c2.R
88
&& c1.G === c2.G
9-
&& c1.B === c2.B;
9+
&& c1.B === c2.B
10+
&& c1.A === c2.A;
1011
};
562 Bytes
Loading
597 Bytes
Loading

test/test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,38 @@ describe('createDiff', () => {
296296
});
297297
});
298298

299+
it('should apply full transparency to the diff if set to 0', (done) => {
300+
const _this = this;
301+
looksSame.createDiff({
302+
reference: srcPath('ref.png'),
303+
current: srcPath('different.png'),
304+
diff: this.tempName,
305+
highlightColor: '#ff00ff',
306+
transparency: 0
307+
}, () => {
308+
looksSame(imagePath('diffs/different-0-alpha.png'), _this.tempName, {strict: true}, (error, equal) => {
309+
expect(equal).to.equal(true);
310+
done();
311+
});
312+
});
313+
});
314+
315+
it('should support partial transparency in the diff', (done) => {
316+
const _this = this;
317+
looksSame.createDiff({
318+
reference: srcPath('ref.png'),
319+
current: srcPath('different.png'),
320+
diff: this.tempName,
321+
highlightColor: '#ff00ff',
322+
transparency: 50
323+
}, () => {
324+
looksSame(imagePath('diffs/different-50-alpha.png'), _this.tempName, {strict: true}, (error, equal) => {
325+
expect(equal).to.equal(true);
326+
done();
327+
});
328+
});
329+
});
330+
299331
it('should allow to build diff for taller images', (done) => {
300332
const _this = this;
301333
looksSame.createDiff({

0 commit comments

Comments
 (0)