-
Notifications
You must be signed in to change notification settings - Fork 8
/
color-threshold.js
25 lines (23 loc) · 1.03 KB
/
color-threshold.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const NodeLibpng = require("node-libpng");
const NativeImageDiff = require("native-image-diff");
// Read the images from PNG images on the disk.
const imageRed = NodeLibpng.readPngFileSync(`${__dirname}/../../images/red-rectangle-example-red.png`);
const imageGradient = NodeLibpng.readPngFileSync(`${__dirname}/../../images/red-rectangle-example-gradient.png`);
// Compare the images with different color thresholds.
for (let colorThreshold = 0; colorThreshold <= 0.7; colorThreshold += 0.1) {
const result = NativeImageDiff.diffImages({
image1: imageRed,
image2: imageGradient,
colorThreshold,
detectAntialiasing: false,
});
const percent = Math.floor(colorThreshold * 100);
// Log the result for this threshold.
console.log(`Color threshold ${percent}%: ${result.pixels} pixels were different.`);
// Write the diff image.
NodeLibpng.writePngFileSync(
`${__dirname}/../../images/red-rectangle-example-${percent}-diff.png`,
result.image.data,
result.image,
);
}