1 | # LooksSame
|
2 |
|
3 | [![Build Status](https://travis-ci.org/gemini-testing/looks-same.svg?branch=master)](https://travis-ci.org/gemini-testing/looks-same)
|
4 |
|
5 | Node.js library for comparing PNG-images, taking into account human color
|
6 | perception. It is created specially for the needs of visual regression testing
|
7 | for [`gemini`](http://github.com/gemini-testing/gemini) utility, but can be used
|
8 | for other purposes.
|
9 |
|
10 | ## Comparing images
|
11 |
|
12 | ```javascript
|
13 | var looksSame = require('looks-same');
|
14 |
|
15 | looksSame('image1.png', 'image2.png', function(error, {equal}) {
|
16 | // equal will be true, if images looks the same
|
17 | });
|
18 | ```
|
19 |
|
20 | Parameters can be paths to files or buffer with compressed `png` image.
|
21 |
|
22 | By default, it will detect only noticeable differences. If you wish to detect any difference,
|
23 | use `strict` options:
|
24 |
|
25 | ```javascript
|
26 | looksSame('image1.png', 'image2.png', {strict: true}, function(error, {equal}) {
|
27 | ...
|
28 | });
|
29 | ```
|
30 |
|
31 | You can also adjust the [ΔE](http://en.wikipedia.org/wiki/Color_difference) value that will be treated as error
|
32 | in non-strict mode:
|
33 |
|
34 | ```javascript
|
35 | looksSame('image1.png', 'image2.png', {tolerance: 5}, function(error, {equal}) {
|
36 | ...
|
37 | });
|
38 | ```
|
39 |
|
40 | Default `tolerance` in non-strict mode is 2.3 which is enough for the most cases.
|
41 | Setting `tolerance` to 0 will produce the same result as `strict: true`, but strict mode
|
42 | is faster.
|
43 | Attempt to set `tolerance` in strict mode will produce an error.
|
44 |
|
45 | Some devices can have different proportion between physical and logical screen resolutions also
|
46 | known as `pixel ratio`. Default value for this proportion is 1.
|
47 | This param also affects the comparison result, so it can be set manually with `pixelRatio` option.
|
48 |
|
49 | ```javascript
|
50 | looksSame('image1.png', 'image2.png', {pixelRatio: 2}, function(error, {equal}) {
|
51 | ...
|
52 | });
|
53 | ```
|
54 |
|
55 | ### Comparing images with ignoring caret
|
56 |
|
57 | For visual regression tasks it may be useful to ignore text caret in text input elements.
|
58 | You can do it with `ignoreCaret` option.
|
59 |
|
60 | ```javascript
|
61 | looksSame('image1.png', 'image2.png', {ignoreCaret: true}, function(error, {equal}) {
|
62 | ...
|
63 | });
|
64 | ```
|
65 |
|
66 | Both `strict` and `ignoreCaret` can be set independently of one another.
|
67 |
|
68 | ### Comparing images with ignoring antialiasing
|
69 |
|
70 | Some images has difference while comparing because of antialiasing. These diffs will be ignored by default. You can use `ignoreAntialiasing` option with `false` value to disable ignoring such diffs. In that way antialiased pixels will be marked as diffs. Read more about [anti-aliasing algorithm](http://www.eejournal.ktu.lt/index.php/elt/article/view/10058/5000).
|
71 |
|
72 | ```javascript
|
73 | looksSame('image1.png', 'image2.png', {ignoreAntialiasing: true}, function(error, {equal}) {
|
74 | ...
|
75 | });
|
76 | ```
|
77 |
|
78 | Sometimes the antialiasing algorithm can work incorrectly due to some features of the browser rendering engine. Use the option `antialiasingTolerance` to make the algorithm less strict. With this option you can specify the minimum difference in brightness (zero by default) between the darkest/lightest pixel (which is adjacent to the antialiasing pixel) and theirs adjacent pixels.
|
79 |
|
80 | We recommend that you don't increase this value above 10. If you need to increase more than 10 then this is definitely not antialiasing.
|
81 |
|
82 | Example:
|
83 | ```javascript
|
84 | looksSame('image1.png', 'image2.png', {ignoreAntialiasing: true, antialiasingTolerance: 3}, function(error, {equal}) {
|
85 | ...
|
86 | });
|
87 | ```
|
88 |
|
89 | ### Getting diff bounds
|
90 | Looksame returns information about diff bounds. It returns only first pixel if you passed `stopOnFirstFail` option with `true` value. The whole diff area would be returned if `stopOnFirstFail` option is not passed or it's passed with `false` value.
|
91 |
|
92 | ```javascript
|
93 | looksSame('image1.png', 'image2.png', {stopOnFirstFail: false}, function(error, {equal, diffBounds}) {
|
94 | // {
|
95 | // equal: false,
|
96 | // diffBounds: {left: 10, top: 10, right: 20, bottom: 20}
|
97 | // }
|
98 | });
|
99 | ```
|
100 |
|
101 | ## Building diff image
|
102 |
|
103 | ```javascript
|
104 | looksSame.createDiff({
|
105 | reference: '/path/to/reference/image.png',
|
106 | current: '/path/to/current/image.png',
|
107 | diff: '/path/to/save/diff/to.png',
|
108 | highlightColor: '#ff00ff', // color to highlight the differences
|
109 | strict: false, // strict comparsion
|
110 | tolerance: 2.5,
|
111 | ignoreAntialiasing: false, // do not ignore antialising by default
|
112 | ignoreCaret: false // do not ignore caret by default
|
113 | }, function(error) {
|
114 | ...
|
115 | });
|
116 | ```
|
117 |
|
118 | ## Building diff image as a Buffer
|
119 |
|
120 | If you don't want the diff image to be written on disk, then simply **don't**
|
121 | pass any `diff: path` to the `createDiff` method. The callback will then
|
122 | receive a `Buffer` containing the diff as the 2nd argument.
|
123 |
|
124 | ```javascript
|
125 | looksSame.createDiff({
|
126 | // exactly same options as above, but without diff
|
127 | }, function(error, buffer) {
|
128 | ...
|
129 | });
|
130 | ```
|
131 |
|
132 | ## Comparing colors
|
133 |
|
134 | If you just need to compare two colors you can use `colors` function:
|
135 |
|
136 | ```javascript
|
137 | looksSame.colors(
|
138 | {R: 255, G: 0, B: 0},
|
139 | {R: 254, G: 1, B: 1},
|
140 | {tolerance: 2.5}
|
141 | );
|
142 | ```
|