Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add custom tolerance options support
added the ability to override the tolerance for default ignore modes
  • Loading branch information
alex-chuyko authored and Alexander Chuiko committed Jun 10, 2021
1 parent 218dc7e commit 1f5b64b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added nodejs-tests/assets/square1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added nodejs-tests/assets/square2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions nodejs-tests/ignore.test.js
Expand Up @@ -25,6 +25,34 @@ describe("ignore", () => {
});
});

test("ignore antialiasing on with custom tolerance", async () => {
const text = fs.readFileSync("./nodejs-tests/assets/square1.png");
const textAa = fs.readFileSync("./nodejs-tests/assets/square2.png");

return new Promise((resolve) => {
const opts = {
ignore: "antialiasing",
tolerance: {
red: 16,
green: 16,
blue: 16
}
};

resemble.compare(text, textAa, opts, async (_x, data) => {
expect(data.misMatchPercentage).toBe("100.00");
const buffer = data.getBuffer();

expect(buffer).toBeInstanceOf(Buffer);

const comparison = fs.readFileSync("./nodejs-tests/assets/isAntialiased/diffWithCustomTolerance.png");

expect(buffer.equals(comparison)).toBe(true);
resolve();
});
});
});

test("ignore antialiasing off", async () => {
const text = fs.readFileSync("./nodejs-tests/assets/text.png");
const textAa = fs.readFileSync("./nodejs-tests/assets/textAa.png");
Expand Down
Binary file added output.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 15 additions & 3 deletions resemble.js
Expand Up @@ -960,6 +960,15 @@ var isNode = function () {
wrapper();

return getCompareApi(wrapper);
},
setupCustomTolerance: function (customSettings) {
for (var property in tolerance) {
if (!customSettings.hasOwnProperty(property)) {
continue;
}

tolerance[property] = customSettings[property];
}
}
};

Expand Down Expand Up @@ -990,7 +999,7 @@ var isNode = function () {
return resemble;
}

function applyIgnore(api, ignore) {
function applyIgnore(api, ignore, customTolerance) {
switch (ignore) {
case "nothing":
api.ignoreNothing();
Expand All @@ -1010,6 +1019,8 @@ var isNode = function () {
default:
throw new Error("Invalid ignore: " + ignore);
}

api.setupCustomTolerance(customTolerance);
}

resemble.compare = function (image1, image2, options, cb) {
Expand Down Expand Up @@ -1041,11 +1052,12 @@ var isNode = function () {
compare.scaleToSameSize();
}

var toleranceSettings = opt.tolerance || {};
if (typeof opt.ignore === "string") {
applyIgnore(compare, opt.ignore);
applyIgnore(compare, opt.ignore, toleranceSettings);
} else if (opt.ignore && opt.ignore.forEach) {
opt.ignore.forEach(function (v) {
applyIgnore(compare, v);
applyIgnore(compare, v, toleranceSettings);
});
}

Expand Down

0 comments on commit 1f5b64b

Please sign in to comment.