-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path.tape.js
More file actions
184 lines (157 loc) · 5.24 KB
/
.tape.js
File metadata and controls
184 lines (157 loc) · 5.24 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'use strict';
// convert module
const convert = require('.');
// test a range of colors in a given color space
function test(opts) {
const from = opts.from;
const to = opts.to;
const channel1 = opts.channel1;
const channel2 = opts.channel2;
const channel3 = opts.channel3;
console.log('Test:', opts.from, '=>', opts.to);
for (let channel1value = channel1.start; channel1value <= channel1.stop; channel1value += channel1.increment) {
for (let channel2value = channel2.start; channel2value <= channel2.stop; channel2value += channel2.increment) {
for (let channel3value = channel3.start; channel3value <= channel3.stop; channel3value += channel3.increment) {
// the source color in the original color space
const source1 = [channel1value, channel2value, channel3value];
// the source color in the converted color space
const source2 = convert[`${from}2${to}`](source1[0], source1[1], source1[2]);
// the resulting color converted back to its original color space
const result1 = convert[`${to}2${from}`](source2[0], source2[1], source2[2], channel1value);
// the resulting color converted back to its converted color space
const result2 = convert[`${from}2${to}`](result1[0], result1[1], result1[2]);
if (
// if the source color has changed
source1.map(Math.round).join() !== result1.map(Math.round).join() &&
// and the converted color has also changed
source2.map(Math.round).join() !== result2.map(Math.round).join()
) {
// log the failing color conversion
console.log({ [from]: source1, [to]: source2, 'became': result1 });
// exit with failure
process.exit(1);
}
}
}
}
}
function testExpression(text, result, expect, precession) {
if (typeof result === 'number') {
const multiplier = Math.pow(10, precession || 0);
const normalizedResult = Math.round(result * multiplier) / multiplier;
const normalizedExpect = Math.round(expect * multiplier) / multiplier;
console.log(`Test: ${text} is ${normalizedExpect}`);
if (normalizedResult !== normalizedExpect) {
console.log({ expect: normalizedExpect, result: normalizedResult });
process.exit(1);
}
} else {
console.log(`Test: ${text} is ${expect}`);
if (result !== expect) {
console.log({ expect, result });
process.exit(1);
}
}
}
// test RGB to HSL conversion
test({
from: 'rgb', to: 'hsl',
channel1: { start: 0, stop: 100, increment: 4 },
channel2: { start: 0, stop: 100, increment: 4 },
channel3: { start: 0, stop: 100, increment: 4 }
});
// test RGB to HWB conversion
test({
from: 'rgb', to: 'hwb',
channel1: { start: 0, stop: 100, increment: 4 },
channel2: { start: 0, stop: 100, increment: 4 },
channel3: { start: 0, stop: 100, increment: 4 }
});
// test RGB to Lab conversion
test({
from: 'rgb', to: 'lab',
channel1: { start: 0, stop: 100, increment: 4 },
channel2: { start: 0, stop: 100, increment: 4 },
channel3: { start: 0, stop: 100, increment: 4 }
});
// test RGB to LCH conversion
test({
from: 'rgb', to: 'lch',
channel1: { start: 0, stop: 100, increment: 4 },
channel2: { start: 0, stop: 100, increment: 4 },
channel3: { start: 0, stop: 100, increment: 4 }
});
// test HSL to RGB conversion
test({
from: 'hsl', to: 'rgb',
channel1: { start: 0, stop: 359, increment: 1 },
channel2: { start: 0, stop: 100, increment: 4 },
channel3: { start: 0, stop: 100, increment: 4 }
});
// test HSL to HWB conversion
test({
from: 'hsl', to: 'hwb',
channel1: { start: 0, stop: 359, increment: 1 },
channel2: { start: 0, stop: 100, increment: 4 },
channel3: { start: 0, stop: 100, increment: 4 }
});
// test HSL to Lab conversion
test({
from: 'hsl', to: 'lab',
channel1: { start: 0, stop: 359, increment: 1 },
channel2: { start: 0, stop: 100, increment: 4 },
channel3: { start: 0, stop: 100, increment: 4 }
});
// test HSL to LCH conversion
test({
from: 'hsl', to: 'lch',
channel1: { start: 0, stop: 359, increment: 1 },
channel2: { start: 0, stop: 100, increment: 4 },
channel3: { start: 0, stop: 100, increment: 4 }
});
// test HWB to RGB conversion
test({
from: 'hwb', to: 'rgb',
channel1: { start: 0, stop: 359, increment: 1 },
channel2: { start: 0, stop: 100, increment: 4 },
channel3: { start: 0, stop: 100, increment: 4 }
}, 10);
// test HWB to HSL conversion
test({
from: 'hwb', to: 'hsl',
channel1: { start: 0, stop: 359, increment: 1 },
channel2: { start: 0, stop: 100, increment: 4 },
channel3: { start: 0, stop: 100, increment: 4 }
}, 10);
testExpression(
'rgb(100% 100% 100%) / rgb(0% 0% 0%) contrast',
convert.rgb2contrast([100, 100, 100], [0, 0, 0]),
21
);
testExpression(
'rgb(100% 100% 100%) / rgb(50% 50% 50%) contrast',
convert.rgb2contrast([100, 100, 100], [50, 50, 50]),
3.98, 2
);
testExpression(
'rgb(100% 100% 100%) / rgb(100% 0% 0%) contrast',
convert.rgb2contrast([100, 100, 100], [100, 0, 0]),
4, 2
);
testExpression(
'contrast rgb(97.647% 97.647% 97.647%) / rgb(100% 0% 0%)',
convert.rgb2contrast([97.647, 97.647, 97.647], [100, 0, 0]),
3.8, 2
);
testExpression(
'rgb2ciede matches lab2ciede',
convert.rgb2ciede([100, 100, 100], [0, 0, 0]),
convert.lab2ciede(convert.rgb2lab(100, 100, 100), convert.rgb2lab(0, 0, 0))
);
testExpression(
'rgb2ciede rgb(50% 50% 50%) / rgb(0% 0% 0%)',
convert.rgb2ciede([50, 50, 50], [0, 0, 0]),
40
);
// exit with success
process.exit(0);