UNPKG

4.94 kBMarkdownView Raw
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
5Node.js library for comparing PNG-images, taking into account human color
6perception. It is created specially for the needs of visual regression testing
7for [`gemini`](http://github.com/gemini-testing/gemini) utility, but can be used
8for other purposes.
9
10## Comparing images
11
12```javascript
13var looksSame = require('looks-same');
14
15looksSame('image1.png', 'image2.png', function(error, {equal}) {
16 // equal will be true, if images looks the same
17});
18```
19
20Parameters can be paths to files or buffer with compressed `png` image.
21
22By default, it will detect only noticeable differences. If you wish to detect any difference,
23use `strict` options:
24
25```javascript
26looksSame('image1.png', 'image2.png', {strict: true}, function(error, {equal}) {
27 ...
28});
29```
30
31You can also adjust the [ΔE](http://en.wikipedia.org/wiki/Color_difference) value that will be treated as error
32in non-strict mode:
33
34```javascript
35looksSame('image1.png', 'image2.png', {tolerance: 5}, function(error, {equal}) {
36 ...
37});
38```
39
40Default `tolerance` in non-strict mode is 2.3 which is enough for the most cases.
41Setting `tolerance` to 0 will produce the same result as `strict: true`, but strict mode
42is faster.
43Attempt to set `tolerance` in strict mode will produce an error.
44
45Some devices can have different proportion between physical and logical screen resolutions also
46known as `pixel ratio`. Default value for this proportion is 1.
47This param also affects the comparison result, so it can be set manually with `pixelRatio` option.
48
49```javascript
50looksSame('image1.png', 'image2.png', {pixelRatio: 2}, function(error, {equal}) {
51 ...
52});
53```
54
55### Comparing images with ignoring caret
56
57For visual regression tasks it may be useful to ignore text caret in text input elements.
58You can do it with `ignoreCaret` option.
59
60```javascript
61looksSame('image1.png', 'image2.png', {ignoreCaret: true}, function(error, {equal}) {
62 ...
63});
64```
65
66Both `strict` and `ignoreCaret` can be set independently of one another.
67
68### Comparing images with ignoring antialiasing
69
70Some 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
73looksSame('image1.png', 'image2.png', {ignoreAntialiasing: true}, function(error, {equal}) {
74 ...
75});
76```
77
78Sometimes 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
80We 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
82Example:
83```javascript
84looksSame('image1.png', 'image2.png', {ignoreAntialiasing: true, antialiasingTolerance: 3}, function(error, {equal}) {
85 ...
86});
87```
88
89### Getting diff bounds
90Looksame 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
93looksSame('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
104looksSame.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
120If you don't want the diff image to be written on disk, then simply **don't**
121pass any `diff: path` to the `createDiff` method. The callback will then
122receive a `Buffer` containing the diff as the 2nd argument.
123
124```javascript
125looksSame.createDiff({
126 // exactly same options as above, but without diff
127}, function(error, buffer) {
128 ...
129});
130```
131
132## Comparing colors
133
134If you just need to compare two colors you can use `colors` function:
135
136```javascript
137looksSame.colors(
138 {R: 255, G: 0, B: 0},
139 {R: 254, G: 1, B: 1},
140 {tolerance: 2.5}
141);
142```