Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,12 @@ const buildDiffImage = async (img1, img2, options) => {
buf[pixelInd + 2] = B;
};

let isExactMatch = true;

await iterateRect(width, height, (x, y) => {
if (x >= minWidth || y >= minHeight) {
setPixel(resultBuffer, x, y, highlightColor);
isExactMatch = false;
return;
}

Expand All @@ -113,12 +116,15 @@ const buildDiffImage = async (img1, img2, options) => {

if (!options.comparator({color1, color2, img1, img2, x, y, width, height, minWidth, minHeight})) {
setPixel(resultBuffer, x, y, highlightColor);
isExactMatch = false;
} else {
setPixel(resultBuffer, x, y, color1);
}
});

return img.fromBuffer(resultBuffer, {raw: {width, height, channels: 3}});
const imgFromBuffer = await img.fromBuffer(resultBuffer, {raw: {width, height, channels: 3}});

return {diffImage: imgFromBuffer, isExactMatch: isExactMatch};
};

const getToleranceFromOpts = (opts) => {
Expand Down Expand Up @@ -224,14 +230,19 @@ exports.createDiff = async function saveDiff(opts) {

const [image1, image2] = utils.formatImages(opts.reference, opts.current);
const {first, second} = await utils.readPair(image1, image2);
const diffImage = await buildDiffImage(first, second, {
const {diffImage, isExactMatch} = await buildDiffImage(first, second, {
highlightColor: utils.parseColorString(opts.highlightColor),
comparator: createComparator(first, second, opts)
});

return opts.diff === undefined
? diffImage.createBuffer(opts.extension)
: diffImage.save(opts.diff);
if (opts.diff === undefined) {
return diffImage.createBuffer(opts.extension);
}

const resultSaveDiff = await diffImage.save(opts.diff);
resultSaveDiff.equal = isExactMatch;

return resultSaveDiff;
};

exports.colors = (color1, color2, opts) => {
Expand Down
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,24 @@ describe('createDiff', () => {
expect(equal).to.be.equal(true);
});

it('should return an equal property if the diff path option is specified', async () => {
const equalResponse = await looksSame.createDiff({
reference: srcPath('ref.png'),
current: srcPath('same.png'),
diff: this.tempName,
highlightColor: '#ff00ff'
});
const notEqualResponse = await looksSame.createDiff({
reference: srcPath('ref.png'),
current: srcPath('different.png'),
diff: this.tempName,
highlightColor: '#ff00ff'
});

expect(equalResponse).to.have.property('equal', true);
expect(notEqualResponse).to.have.property('equal', false);
});

describe('with comparing by areas', () => {
it('should create diff image equal to reference', async () => {
await looksSame.createDiff({
Expand Down