Skip to content

Commit

Permalink
fix gemini-testing#10 with the addition of a transparency option to…
Browse files Browse the repository at this point in the history
… control the alpha of unchanged pixels
  • Loading branch information
chrisdeely committed Oct 20, 2017
1 parent fac1692 commit 0dae96a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ looksSame.createDiff({
current: '/path/to/current/image.png',
diff: '/path/to/save/diff/to.png',
highlightColor: '#ff00ff', //color to highlight the differences
strict: false,//strict comparsion
transparency: 255, //0-255, controls the alpha channel for unchanged pixels
strict: false,//strict comparison
tolerance: 2.5
}, function(error) {
});
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ const buildDiffImage = (png1, png2, options, callback) => {
const minWidth = Math.min(png1.width, png2.width);
const minHeight = Math.min(png1.height, png2.height);
const highlightColor = options.highlightColor;
const alphaLevel = isNaN(options.transparency) ? 255 : options.transparency;
const result = png.empty(width, height);

iterateRect(width, height, (x, y) => {
Expand All @@ -158,7 +159,7 @@ const buildDiffImage = (png1, png2, options, callback) => {
if (!options.comparator({color1, color2})) {
result.setPixel(x, y, highlightColor);
} else {
result.setPixel(x, y, color1);
result.setPixel(x, y, color1, alphaLevel);
}
}, () => callback(result));
};
Expand Down Expand Up @@ -216,7 +217,8 @@ exports.createDiff = function saveDiff(opts, callback) {

const diffOptions = {
highlightColor: parseColorString(opts.highlightColor),
comparator: opts.strict ? areColorsSame : makeCIEDE2000Comparator(tolerance)
comparator: opts.strict ? areColorsSame : makeCIEDE2000Comparator(tolerance),
transparency: opts.transparency
};

buildDiffImage(result.first, result.second, diffOptions, (result) => {
Expand Down
10 changes: 6 additions & 4 deletions lib/png.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,24 @@ class PNGImage {
return {
R: this._png.data[idx],
G: this._png.data[idx + 1],
B: this._png.data[idx + 2]
B: this._png.data[idx + 2],
A: this._png.data[idx + 3]
};
}

/**
* Sets color data to pixel with given coordinates
* @param {Number} x coordinate
* @param {Number} y coordinate
* @param {Object} color
* @param {Object} color RGB formatted color {R:0-255, G:0-255, B:0-255}
* @param {Number} [alpha=255] A level to set for the alpha channel, 0-255
*/
setPixel(x, y, color) {
setPixel(x, y, color, alpha) {
const idx = this._getIdx(x, y);
this._png.data[idx] = color.R;
this._png.data[idx + 1] = color.G;
this._png.data[idx + 2] = color.B;
this._png.data[idx + 3] = 255;
this._png.data[idx + 3] = isNaN(alpha) ? 255 : alpha;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/same-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ module.exports = (data) => {

return c1.R === c2.R
&& c1.G === c2.G
&& c1.B === c2.B;
&& c1.B === c2.B
&& c1.A === c2.A;
};
Binary file added test/data/diffs/different-0-alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/diffs/different-50-alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,38 @@ describe('createDiff', () => {
});
});

it('should apply full transparency to the diff if set to 0', (done) => {
const _this = this;
looksSame.createDiff({
reference: srcPath('ref.png'),
current: srcPath('different.png'),
diff: this.tempName,
highlightColor: '#ff00ff',
transparency: 0
}, () => {
looksSame(imagePath('diffs/different-0-alpha.png'), _this.tempName, {strict: true}, (error, equal) => {
expect(equal).to.equal(true);
done();
});
});
});

it('should support partial transparency in the diff', (done) => {
const _this = this;
looksSame.createDiff({
reference: srcPath('ref.png'),
current: srcPath('different.png'),
diff: this.tempName,
highlightColor: '#ff00ff',
transparency: 50
}, () => {
looksSame(imagePath('diffs/different-50-alpha.png'), _this.tempName, {strict: true}, (error, equal) => {
expect(equal).to.equal(true);
done();
});
});
});

it('should allow to build diff for taller images', (done) => {
const _this = this;
looksSame.createDiff({
Expand Down

0 comments on commit 0dae96a

Please sign in to comment.