From 1f5b64bcc9bc601c0b79aceca4e7ea6c087e862d Mon Sep 17 00:00:00 2001
From: Alex Chuiko <alex.chuyko9@gmail.com>
Date: Wed, 9 Jun 2021 18:12:35 +0300
Subject: [PATCH] Add custom tolerance options support added the ability to
 override the tolerance for default ignore modes

---
 .../isAntialiased/diffWithCustomTolerance.png | Bin 0 -> 1491 bytes
 nodejs-tests/assets/square1.png               | Bin 0 -> 1413 bytes
 nodejs-tests/assets/square2.png               | Bin 0 -> 1473 bytes
 nodejs-tests/ignore.test.js                   |  28 ++++++++++++++++++
 output.png                                    | Bin 0 -> 1491 bytes
 resemble.js                                   |  18 +++++++++--
 6 files changed, 43 insertions(+), 3 deletions(-)
 create mode 100644 nodejs-tests/assets/isAntialiased/diffWithCustomTolerance.png
 create mode 100644 nodejs-tests/assets/square1.png
 create mode 100644 nodejs-tests/assets/square2.png
 create mode 100644 output.png

diff --git a/nodejs-tests/assets/isAntialiased/diffWithCustomTolerance.png b/nodejs-tests/assets/isAntialiased/diffWithCustomTolerance.png
new file mode 100644
index 0000000000000000000000000000000000000000..856300b0a534480847ba142eea2c78994463c421
GIT binary patch
diff --git a/nodejs-tests/assets/square1.png b/nodejs-tests/assets/square1.png
new file mode 100644
index 0000000000000000000000000000000000000000..32bfe796f6a7c1cee6658ef0866c0b0f4cb26fb9
GIT binary patch
diff --git a/nodejs-tests/assets/square2.png b/nodejs-tests/assets/square2.png
new file mode 100644
index 0000000000000000000000000000000000000000..18dfa4ba4aa3974aef9677eb1ae014760c13f678
GIT binary patch
diff --git a/nodejs-tests/ignore.test.js b/nodejs-tests/ignore.test.js
index 3ac51b7..5911c5a 100644
--- a/nodejs-tests/ignore.test.js
+++ b/nodejs-tests/ignore.test.js
@@ -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");
diff --git a/output.png b/output.png
new file mode 100644
index 0000000000000000000000000000000000000000..856300b0a534480847ba142eea2c78994463c421
GIT binary patch
diff --git a/resemble.js b/resemble.js
index a913d23..4e57f11 100644
--- a/resemble.js
+++ b/resemble.js
@@ -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];
+                    }
                 }
             };
 
@@ -990,7 +999,7 @@ var isNode = function () {
         return resemble;
     }
 
-    function applyIgnore(api, ignore) {
+    function applyIgnore(api, ignore, customTolerance) {
         switch (ignore) {
             case "nothing":
                 api.ignoreNothing();
@@ -1010,6 +1019,8 @@ var isNode = function () {
             default:
                 throw new Error("Invalid ignore: " + ignore);
         }
+
+        api.setupCustomTolerance(customTolerance);
     }
 
     resemble.compare = function (image1, image2, options, cb) {
@@ -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);
             });
         }
 
