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 Jan 23, 2023
1 parent b9399bc commit 8965612
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ await 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,
antialiasingTolerance: 0,
ignoreAntialiasing: true, // ignore antialising by default
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const buildDiffImage = async (img1, img2, options) => {
const minHeight = Math.min(img1.height, img2.height);

const highlightColor = options.highlightColor;
const alphaLevel = isNaN(options.transparency) ? 255 : options.transparency;
const resultBuffer = Buffer.alloc(width * height * 3);

const setPixel = (buf, x, y, {R, G, B}) => {
Expand All @@ -98,7 +99,7 @@ const buildDiffImage = async (img1, img2, options) => {
if (!options.comparator({color1, color2, img1, img2, x, y, width, height})) {
setPixel(resultBuffer, x, y, highlightColor);
} else {
setPixel(resultBuffer, x, y, color1);
setPixel(resultBuffer, x, y, color1, alphaLevel);
}
});

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.
30 changes: 30 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,37 @@ describe('createDiff', () => {

expect(equal).to.equal(true);
});
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', async () => {
await looksSame.createDiff({
reference: srcPath('ref.png'),
Expand Down

0 comments on commit 8965612

Please sign in to comment.