looks-same
Advanced tools
Comparing version 1.0.1 to 1.1.0
39
index.js
@@ -73,3 +73,3 @@ 'use strict'; | ||
var comparator = opts.strict? areColorsSame : areColorsLookSame; | ||
var comparator = opts.strict? areColorsSame : makeCIEDE2000Comparator(opts.tolerance); | ||
if (opts.ignoreCaret) { | ||
@@ -85,3 +85,3 @@ comparator = makeNoCaretColorComparator(comparator); | ||
return function compareNoCaret(color1, color2, x, y) { | ||
if (comparator(color1, color2)) { | ||
if (comparator(color1, color2, x, y)) { | ||
return true; | ||
@@ -100,11 +100,13 @@ } | ||
function areColorsLookSame(c1, c2) { | ||
if (areColorsSame(c1, c2)) { | ||
return true; | ||
} | ||
/*jshint camelcase:false*/ | ||
var lab1 = colorDiff.rgb_to_lab(c1), | ||
lab2 = colorDiff.rgb_to_lab(c2); | ||
function makeCIEDE2000Comparator(tolerance) { | ||
return function doColorsLookSame(c1, c2) { | ||
if (areColorsSame(c1, c2)) { | ||
return true; | ||
} | ||
/*jshint camelcase:false*/ | ||
var lab1 = colorDiff.rgb_to_lab(c1), | ||
lab2 = colorDiff.rgb_to_lab(c2); | ||
return colorDiff.diff(lab1, lab2) < JND; | ||
return colorDiff.diff(lab1, lab2) < tolerance; | ||
}; | ||
} | ||
@@ -123,2 +125,5 @@ | ||
} | ||
opts.tolerance = getToleranceFromOpts(opts); | ||
readPair(reference, image, function(error, result) { | ||
@@ -189,2 +194,4 @@ if (error) { | ||
exports.createDiff = function saveDiff(opts, callback) { | ||
var tolerance = getToleranceFromOpts(opts); | ||
readPair(opts.reference, opts.current, function(error, result) { | ||
@@ -196,3 +203,3 @@ if (error) { | ||
highlightColor: parseColorString(opts.highlightColor), | ||
comparator: opts.strict? areColorsSame : areColorsLookSame | ||
comparator: opts.strict? areColorsSame : makeCIEDE2000Comparator(tolerance) | ||
}; | ||
@@ -205,1 +212,11 @@ | ||
}; | ||
function getToleranceFromOpts(opts) { | ||
if ('tolerance' in opts) { | ||
if ('strict' in opts) { | ||
throw new TypeError('Unable to use "strict" and "tolerance" options together'); | ||
} | ||
return opts.tolerance; | ||
} | ||
return JND; | ||
} |
{ | ||
"name": "looks-same", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Pure node.js library for comparing PNG-images, taking into account human color perception.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -30,2 +30,16 @@ # LooksSame | ||
You can also adjust the [ΔE](http://en.wikipedia.org/wiki/Color_difference) value that will be treated as error | ||
in non-strict mode: | ||
```javascript | ||
looksSame('image1.png', 'image2.png', {tolerance: 5}, function(error, equal) { | ||
... | ||
}); | ||
``` | ||
Default `tolerance` in non-strict mode is 2.3 which is enough for the most cases. | ||
Setting `tolerance` to 0 will produce the same result as `strict: true`, but strict mode | ||
is faster. | ||
Attempt to set `tolerance` in strict mode will produce an error. | ||
For visual regression tasks it may be useful to ignore text caret in text input elements. | ||
@@ -49,6 +63,7 @@ You can do it with `ignoreCaret` option. | ||
diff: '/path/to/save/diff/to.png', | ||
highlightColor: '#ff00ff' //color to highlight the differences | ||
strict: true //strict comparsion | ||
highlightColor: '#ff00ff', //color to highlight the differences | ||
strict: false,//strict comparsion | ||
tolerance: 2.5 | ||
}, function(error) { | ||
}); | ||
``` |
16477
10
323
68