forked from boronine/colorspaces.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.js
133 lines (123 loc) · 4.13 KB
/
tests.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var assert = require('assert');
var colorspaces = require('./colorspaces.js');
var colors = {
indigo: {
'hex': '#4b0082',
'sRGB': [0.29412, 0.00000, 0.50980],
'CIEXYZ': [0.06931, 0.03108, 0.21354],
'CIExyY': [0.22079, 0.09899, 0.03108],
'CIELAB': [20.470, 51.695, -53.320],
'CIELCH': [20.470, 74.265, 314.113],
'CIELUV': [20.470, 10.084, -61.343]
},
crimson: {
'hex': '#dc143c',
'sRGB': [0.86275, 0.07843, 0.23529],
'CIEXYZ': [0.30581, 0.16042, 0.05760],
'CIExyY': [0.58380, 0.30625, 0.16042],
'CIELAB': [47.030, 70.936, 33.595],
'CIELCH': [47.030, 78.489, 25.342],
'CIELUV': [47.030, 138.278, 19.641]
},
white: {
'hex': '#ffffff',
'sRGB': [1, 1, 1],
'CIEXYZ': [0.95050, 1.00000, 1.08900],
'CIExyY': [0.31272, 0.32900, 1],
'CIELAB': [100, 0.005, -0.010],
'CIELUV': [100, 0.001, -0.017]
},
// CIELCH omitted because Hue is almost completely
// irrelevant for white and its big rounding error
// is acceptable here. Hue is better tested with
// more saturated colors, like the two above
black: {
'hex': '#000000',
'sRGB': [0, 0, 0],
'CIEXYZ': [0, 0, 0],
'CIExyY': [0, 0, 0],
'CIELAB': [0, 0, 0],
'CIELUV': [0, 0, 0]
}
};
// CIELCH omitted
var permissible_error = {
'CIELAB': 0.01,
'CIEXYZ': 0.001,
'CIExyY': 0.001,
'CIELCH': 0.01,
'CIELUV': 0.01,
'sRGB': 0.001
};
// Returns the biggest difference factor between two corresponding
// elements in two tuples
var big_dif = function big_dif(tuple1, tuple2) {
var ret = 0;
var iterable = __range__(0, tuple1.length - 1, true);
for (var j = 0; j < iterable.length; j++) {
var i = iterable[j];
var dif = Math.abs(tuple1[i] - tuple2[i]);
if (dif > ret) {
ret = dif;
}
}
return ret;
};
// For every test color
for (var name in colors) {
// Make a color object for every definition of the test color
var definitions = colors[name];
for (var space1 in definitions) {
// Convert each of those to every color space and compare
var tuple1 = definitions[space1];
for (var space2 in definitions) {
var tuple2 = definitions[space2];
var output = colorspaces.converter(space1, space2)(tuple1);
// If the target space is hex, simply compare the two for equality
if (space2 === 'hex') {
assert.ok(output === tuple2, '\n\n Error when converting ' + space1 + ' to hex\n');
continue;
}
// Otherwise first make sure there are no NaNs
for (var i = 0; i < output.length; i++) {
var val = output[i];
assert.ok(!isNaN(val), '\n\n NaN returned when converting from ' + space1 + ' to ' + space2 + '\n\n ' + output + '\n');
}
// Then calculate the biggest difference
var dif = big_dif(tuple2, output);
// ... and see if it's too big
assert.ok(dif <= permissible_error[space2], '\n\n Big error when converting from ' + space1 + ' to ' + space2 + '\n\n Input: ' + tuple1 + '\n\n Output: ' + output + '\n\n Should be: ' + tuple2 + '\n');
}
}
console.log(name + " ok");
}
var styl = ".someclass\n color CIEXYZ(0.30581, 0.16042, 0.0576)\n color CIExyY(0.58380, 0.30625, 0.16042)\n color CIELAB(47.030, 70.936, 33.595)\n color CIELCH(47.030, 78.489, 25.342)\n color CIELUV(47.030, 138.278, 19.641)\n";
var css = ".someclass { color: #dc143c; color: #dc143c; color: #dc143c; color: #dc143c; color: #dc143c; }";
// Whitespace doesn't matter
var nows = function nows(text) {
return text.replace(/\s+/g, '');
};
try {
var stylus = require('stylus');
} catch (error) {}
if (stylus != null) {
stylus(styl).use(colorspaces()).render(function (err, test_css) {
if (err) {
throw err;
}
if (nows(test_css) === nows(css)) {
return console.log('Stylus works');
} else {
return console.log('STYLUS ERROR' + test_css);
}
});
}
function __range__(left, right, inclusive) {
var range = [];
var ascending = left < right;
var end = !inclusive ? right : ascending ? right + 1 : right - 1;
for (var _i = left; ascending ? _i < end : _i > end; ascending ? _i++ : _i--) {
range.push(_i);
}
return range;
}