-
Notifications
You must be signed in to change notification settings - Fork 89
/
color-4-conversions.test.ts
251 lines (221 loc) · 8.95 KB
/
color-4-conversions.test.ts
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2023 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
// General space-specific info and construction
import {SassColor} from 'sass';
import type {
ColorSpaceXyz,
GamutMapMethod,
HueInterpolationMethod,
KnownColorSpace,
} from 'sass';
import {spaces} from './spaces';
import {interpolations} from './interpolation-examples';
const spaceNames = Object.keys(spaces) as KnownColorSpace[];
describe('Color 4 SassColors Conversions', () => {
spaceNames.forEach(spaceId => {
const space = spaces[spaceId];
describe(space.name, () => {
let color: SassColor, blue: SassColor;
beforeEach(() => {
color = space.constructor(...space.pink);
blue = space.constructor(...space.blue);
});
describe('toSpace', () => {
spaceNames.forEach(destinationSpaceId => {
it(`converts pink to ${destinationSpaceId}`, () => {
const destinationSpace = spaces[destinationSpaceId];
const res = color.toSpace(destinationSpace.name);
expect(res.space).toBe(destinationSpace.name);
const expected = destinationSpace.constructor(
...destinationSpace.pink
);
expect(res).toLooselyEqualColor(expected);
});
});
});
describe('interpolate', () => {
it('interpolates examples', () => {
const examples = interpolations[space.name];
examples.forEach(([input, output]) => {
const res = color.interpolate(blue, {
weight: input.weight,
method: input.method as HueInterpolationMethod,
});
const outputColor = space.constructor(...output);
expect(res).toLooselyEqualColor(outputColor);
});
});
});
describe('change', () => {
it('changes all channels in own space', () => {
space.channels.forEach((channelName, index) => {
const expectedChannels = [
space.pink[0],
space.pink[1],
space.pink[2],
] as [number, number, number];
expectedChannels[index] = 0;
expect(color.change({[channelName]: 0})).toLooselyEqualColor(
space.constructor(...expectedChannels)
);
});
expect(color.change({alpha: 0})).toLooselyEqualColor(
space.constructor(...space.pink, 0)
);
});
it('change with explicit undefined makes no change', () => {
space.channels.forEach(channelName => {
expect(
color.change({[channelName]: undefined})
).toLooselyEqualColor(space.constructor(...space.pink));
});
expect(color.change({alpha: undefined})).toLooselyEqualColor(
space.constructor(...space.pink, 1)
);
});
it('explicit null sets channel to missing', () => {
// Explicitly set space to avoid legacy null-alpha behavior, which is
// tested in Legacy suite.
space.channels.forEach((channelName, index) => {
const expectedChannels = [
space.pink[0],
space.pink[1],
space.pink[2],
] as [number | null, number | null, number | null];
expectedChannels[index] = null;
const changed = color.change({
[channelName]: null,
space: space.name as 'xyz',
});
expect(changed).toLooselyEqualColor(
space.constructor(...expectedChannels)
);
expect(changed.isChannelMissing(channelName)).toBeTrue();
});
expect(
color.change({alpha: null, space: space.name as 'xyz'})
).toLooselyEqualColor(space.constructor(...space.pink, null));
});
describe('allows out-of-range channel values', () => {
const baseColor = space.constructor(
(space.ranges[0][0] + space.ranges[0][1]) / 2,
(space.ranges[1][0] + space.ranges[1][1]) / 2,
(space.ranges[2][0] + space.ranges[2][1]) / 2
);
for (let i = 0; i < 3; i++) {
const channel = space.channels[i];
if (channel === 'hue') continue;
it(`for ${channel}`, () => {
const aboveRange = space.ranges[i][1] + 10;
const belowRange = space.ranges[i][0] - 10;
const above = baseColor.change({[channel]: aboveRange});
const below = baseColor.change({[channel]: belowRange});
expect(above.channels.get(i)).toEqual(aboveRange);
switch (channel) {
case 'saturation':
expect(below.channels.get(i)).toEqual(Math.abs(belowRange));
expect(below.channels.get(0)).toEqual(
(baseColor.channels.get(0) + 180) % 360
);
break;
case 'chroma':
expect(below.channels.get(i)).toEqual(Math.abs(belowRange));
expect(below.channels.get(2)).toEqual(
(baseColor.channels.get(2) + 180) % 360
);
break;
default:
expect(below.channels.get(i)).toEqual(belowRange);
break;
}
});
}
});
spaceNames.forEach(destinationSpaceId => {
it(`changes all channels with space set to ${destinationSpaceId}`, () => {
const destinationSpace = spaces[destinationSpaceId];
destinationSpace.channels.forEach((channel, index) => {
const destinationChannels = [
destinationSpace.pink[0],
destinationSpace.pink[1],
destinationSpace.pink[2],
] as [number, number, number];
// Certain channel values cause equality issues on 1-3 of 16*16*3
// cases. 0.45 is a magic number that works around this until the
// root cause is determined.
const scale = 0.45;
const channelValue = destinationSpace.ranges[index][1] * scale;
destinationChannels[index] = channelValue;
const expected = destinationSpace
.constructor(...destinationChannels)
.toSpace(space.name);
expect(
color.change({
[channel]: channelValue,
space: destinationSpace.name as ColorSpaceXyz,
})
).toLooselyEqualColor(expected);
});
});
});
it('should throw on invalid alpha', () => {
expect(() => color.change({alpha: -1})).toThrow();
expect(() => color.change({alpha: 1.1})).toThrow();
});
});
describe('isInGamut', () => {
// Our `pink` color is in gamut for every space.
it('is true for in gamut colors in own space', () => {
expect(color.isInGamut()).toBe(true);
});
spaceNames.forEach(destinationSpaceId => {
it(`is true for in gamut colors in ${destinationSpaceId}`, () => {
const destinationSpace = spaces[destinationSpaceId];
expect(color.isInGamut(destinationSpace.name)).toBe(true);
});
});
it(`is ${!space.hasOutOfGamut} for out of range colors in own space`, () => {
const outOfGamut = space.constructor(...space.gamutExamples[0][0]);
expect(outOfGamut.isInGamut()).toBe(!space.hasOutOfGamut);
});
});
describe('toGamut', () => {
for (const [input, outputs] of space.gamutExamples) {
const outputsObj =
outputs instanceof Array
? {clip: outputs, 'local-minde': outputs}
: outputs;
for (const [method, output] of Object.entries(outputsObj)) {
describe(method, () => {
it(`in own space, ${input} -> ${output}`, () => {
const res = space
.constructor(...input)
.toGamut({method: method as GamutMapMethod});
expect(res).toLooselyEqualColor(space.constructor(...output));
});
});
}
}
});
});
});
describe('interpolate()', () => {
it('interpolates a rectangular space without options', () =>
expect(
new SassColor({red: 100, green: 200, blue: 50}).interpolate(
new SassColor({red: 255, green: 255, blue: 255})
)
).toLooselyEqualColor(
new SassColor({red: 177.5, green: 227.5, blue: 152.5})
));
it('interpolates a polar space without options', () =>
expect(
new SassColor({hue: 180, saturation: 100, lightness: 50}).interpolate(
new SassColor({red: 100, green: 200, blue: 50})
)
).toLooselyEqualColor(
new SassColor({hue: 140, saturation: 80, lightness: 49.5098039216})
));
});
});