-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorLerp.test.js
More file actions
290 lines (251 loc) · 11.7 KB
/
ColorLerp.test.js
File metadata and controls
290 lines (251 loc) · 11.7 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { bakeGradientLUT, bakeGradientLUTRGBA, sampleColorLUT } from './ColorLerp.js';
import { multiStopGradientTo } from '@zakkster/lite-color';
// ---------------------------------------------------------------------------
// We isolate the bridge from `@zakkster/lite-color`. Each test that cares about
// specific OKLCH inputs configures the mock per-test via mockImplementation.
// The default impl below is set in beforeEach so accidental leakage between
// tests is impossible.
// ---------------------------------------------------------------------------
vi.mock('@zakkster/lite-color', () => ({
multiStopGradientTo: vi.fn(),
}));
const dummyColors = [{ l: 0, c: 0, h: 0 }, { l: 1, c: 0, h: 0 }];
beforeEach(() => {
multiStopGradientTo.mockReset();
// Default: write a mid-grey so tests that don't care still get a sane value.
multiStopGradientTo.mockImplementation((colors, t, out) => {
out.l = 0.5; out.c = 0; out.h = 0; out.a = 1.0;
});
});
// ---------------------------------------------------------------------------
// Helpers — extract channels from a packed uint32 in either byte order
// ---------------------------------------------------------------------------
const argb = (v) => ({
a: (v >>> 24) & 0xFF,
r: (v >>> 16) & 0xFF,
g: (v >>> 8) & 0xFF,
b: v & 0xFF,
});
const rgbaLE = (v) => ({
a: (v >>> 24) & 0xFF,
b: (v >>> 16) & 0xFF,
g: (v >>> 8) & 0xFF,
r: v & 0xFF,
});
describe('lite-color-lerp', () => {
describe('Validation', () => {
it('throws when oklchColors is undefined', () => {
expect(() => bakeGradientLUT()).toThrow(/non-empty/);
});
it('throws when oklchColors is empty', () => {
expect(() => bakeGradientLUT([])).toThrow(/non-empty/);
expect(() => bakeGradientLUTRGBA([])).toThrow(/non-empty/);
});
it('throws when resolution < 2', () => {
expect(() => bakeGradientLUT(dummyColors, 1)).toThrow(/resolution/);
expect(() => bakeGradientLUT(dummyColors, 0)).toThrow(/resolution/);
expect(() => bakeGradientLUTRGBA(dummyColors, 1)).toThrow(/resolution/);
});
it('accepts the minimum resolution of 2', () => {
const lut = bakeGradientLUT(dummyColors, 2);
expect(lut.length).toBe(2);
});
});
describe('Output shape & zero-GC contract', () => {
it('returns a Uint32Array of the requested length', () => {
const lut = bakeGradientLUT(dummyColors, 256);
expect(lut).toBeInstanceOf(Uint32Array);
expect(lut.length).toBe(256);
expect(lut.BYTES_PER_ELEMENT).toBe(4);
expect(lut.byteLength).toBe(1024);
});
it('handles large resolutions', () => {
const lut = bakeGradientLUT(dummyColors, 1024);
expect(lut.length).toBe(1024);
});
it('calls multiStopGradientTo exactly `resolution` times', () => {
bakeGradientLUT(dummyColors, 128);
expect(multiStopGradientTo).toHaveBeenCalledTimes(128);
});
it('passes t in [0, 1] across the full range', () => {
const tValues = [];
multiStopGradientTo.mockImplementation((colors, t, out) => {
tValues.push(t);
out.l = 0.5; out.c = 0; out.h = 0; out.a = 1.0;
});
bakeGradientLUT(dummyColors, 5);
expect(tValues).toEqual([0, 0.25, 0.5, 0.75, 1.0]);
});
});
describe('OKLCH → sRGB conversion correctness', () => {
it('OKLCH(1, 0, 0) bakes to white', () => {
multiStopGradientTo.mockImplementation((c, t, out) => {
out.l = 1.0; out.c = 0; out.h = 0; out.a = 1.0;
});
const lut = bakeGradientLUT(dummyColors, 2);
const px = argb(lut[0]);
// Allow ±1 for rounding at the gamut boundary.
expect(px.a).toBe(255);
expect(px.r).toBeGreaterThanOrEqual(254);
expect(px.g).toBeGreaterThanOrEqual(254);
expect(px.b).toBeGreaterThanOrEqual(254);
});
it('OKLCH(0, 0, 0) bakes to opaque black', () => {
multiStopGradientTo.mockImplementation((c, t, out) => {
out.l = 0.0; out.c = 0; out.h = 0; out.a = 1.0;
});
const lut = bakeGradientLUT(dummyColors, 2);
expect(argb(lut[0])).toEqual({ a: 255, r: 0, g: 0, b: 0 });
});
it('mid-grey OKLCH(0.5, 0, 0) bakes to a non-zero, non-white grey', () => {
multiStopGradientTo.mockImplementation((c, t, out) => {
out.l = 0.5; out.c = 0; out.h = 0; out.a = 1.0;
});
const lut = bakeGradientLUT(dummyColors, 2);
const px = argb(lut[0]);
// Equal channels (achromatic), and within the inner sRGB range.
expect(px.r).toBe(px.g);
expect(px.g).toBe(px.b);
expect(px.r).toBeGreaterThan(20);
expect(px.r).toBeLessThan(200);
});
});
describe('Alpha handling', () => {
it('alpha = 1 packs as 255', () => {
multiStopGradientTo.mockImplementation((c, t, out) => {
out.l = 0.5; out.c = 0; out.h = 0; out.a = 1.0;
});
const lut = bakeGradientLUT(dummyColors, 2);
expect(argb(lut[0]).a).toBe(255);
});
it('alpha = 0 packs as 0', () => {
multiStopGradientTo.mockImplementation((c, t, out) => {
out.l = 0.5; out.c = 0; out.h = 0; out.a = 0.0;
});
const lut = bakeGradientLUT(dummyColors, 2);
expect(argb(lut[0]).a).toBe(0);
});
it('alpha = 0.5 quantizes to 128 (rounding mid-band)', () => {
multiStopGradientTo.mockImplementation((c, t, out) => {
out.l = 0.5; out.c = 0; out.h = 0; out.a = 0.5;
});
const lut = bakeGradientLUT(dummyColors, 2);
// (0.5 * 255 + 0.5) | 0 === 128
expect(argb(lut[0]).a).toBe(128);
});
it('defaults alpha to 1.0 when the gradient does not write it', () => {
// Mock simulates a gradient sampler that omits `a`.
multiStopGradientTo.mockImplementation((c, t, out) => {
out.l = 0.5; out.c = 0; out.h = 0;
// intentionally do not touch out.a
});
const lut = bakeGradientLUT(dummyColors, 2);
expect(argb(lut[0]).a).toBe(255);
});
it('resets alpha between samples (no leakage from previous iteration)', () => {
// First call sets a=0.2, second call leaves a unwritten.
// The bake's `_tempOklch.a = 1.0` reset must prevent leakage.
let call = 0;
multiStopGradientTo.mockImplementation((c, t, out) => {
out.l = 0.5; out.c = 0; out.h = 0;
if (call++ === 0) out.a = 0.2;
// second call: do not write a; expect default 1.0
});
const lut = bakeGradientLUT(dummyColors, 2);
expect(argb(lut[0]).a).toBe(51); // 0.2 * 255 + 0.5 | 0
expect(argb(lut[1]).a).toBe(255); // reset honored
});
});
describe('Byte order — ARGB vs RGBA-LE', () => {
// Use a vivid red OKLCH so R clearly dominates, making channel
// positions trivially identifiable in either packing.
const redImpl = (c, t, out) => {
out.l = 0.628; out.c = 0.258; out.h = 29.0; out.a = 1.0;
};
it('ARGB places A in the highest byte and B in the lowest', () => {
multiStopGradientTo.mockImplementation(redImpl);
const lut = bakeGradientLUT(dummyColors, 2);
const px = argb(lut[0]);
expect(px.a).toBe(255);
expect(px.r).toBeGreaterThan(px.b);
expect(px.r).toBeGreaterThan(px.g);
});
it('RGBA-LE places A in the highest byte and R in the lowest', () => {
multiStopGradientTo.mockImplementation(redImpl);
const lut = bakeGradientLUTRGBA(dummyColors, 2);
const px = rgbaLE(lut[0]);
expect(px.a).toBe(255);
expect(px.r).toBeGreaterThan(px.b);
expect(px.r).toBeGreaterThan(px.g);
});
it('ARGB and RGBA-LE differ for non-grayscale colors', () => {
multiStopGradientTo.mockImplementation(redImpl);
const a = bakeGradientLUT(dummyColors, 2)[0];
const b = bakeGradientLUTRGBA(dummyColors, 2)[0];
expect(a).not.toBe(b);
});
it('ARGB and RGBA-LE produce equal values for achromatic colors (R=G=B)', () => {
// Mid-grey: R=G=B, so swapping R and B is a no-op.
multiStopGradientTo.mockImplementation((c, t, out) => {
out.l = 0.5; out.c = 0; out.h = 0; out.a = 1.0;
});
const a = bakeGradientLUT(dummyColors, 2)[0];
const b = bakeGradientLUTRGBA(dummyColors, 2)[0];
expect(a).toBe(b);
});
it('RGBA-LE output writes correctly into Canvas-shaped memory', () => {
// Simulate Canvas2D ImageData: a Uint8ClampedArray viewed as Uint32.
multiStopGradientTo.mockImplementation(redImpl);
const lut = bakeGradientLUTRGBA(dummyColors, 2);
const buf = new ArrayBuffer(4);
const u32 = new Uint32Array(buf);
const u8 = new Uint8ClampedArray(buf);
u32[0] = lut[0];
// After write, byte 0 must be R (highest channel for our red input).
expect(u8[0]).toBeGreaterThan(u8[2]); // R > B
expect(u8[3]).toBe(255); // A
});
});
describe('sampleColorLUT — hot path', () => {
const lut = new Uint32Array([1000, 2000, 3000, 4000, 5000]);
it('returns lut[0] at t=0', () => {
expect(sampleColorLUT(lut, 0)).toBe(1000);
});
it('returns lut[max] at t=1', () => {
expect(sampleColorLUT(lut, 1)).toBe(5000);
});
it('returns the middle index at t=0.5 with maxIdx=4', () => {
// 0.5 * 4 = 2.0 → index 2
expect(sampleColorLUT(lut, 0.5)).toBe(3000);
});
it('clamps t < 0 to lut[0]', () => {
expect(sampleColorLUT(lut, -5.5)).toBe(1000);
expect(sampleColorLUT(lut, -Infinity)).toBe(1000);
});
it('clamps t > 1 to lut[max]', () => {
expect(sampleColorLUT(lut, 42.0)).toBe(5000);
expect(sampleColorLUT(lut, Infinity)).toBe(5000);
});
it('fast-floors fractional indices', () => {
// maxIdx = 4
expect(sampleColorLUT(lut, 0.24)).toBe(1000); // 0.96 → 0
expect(sampleColorLUT(lut, 0.26)).toBe(2000); // 1.04 → 1
expect(sampleColorLUT(lut, 0.74)).toBe(3000); // 2.96 → 2
expect(sampleColorLUT(lut, 0.76)).toBe(4000); // 3.04 → 3
});
it('handles a 2-entry LUT (minimum size)', () => {
// With maxIdx=1, ANY t in [0, 1) hits index 0; only t≥1 hits index 1.
// This is documented behavior of nearest-neighbor sampling.
const tiny = new Uint32Array([111, 222]);
expect(sampleColorLUT(tiny, 0)).toBe(111);
expect(sampleColorLUT(tiny, 0.4)).toBe(111); // 0.4 * 1 = 0.4 → 0
expect(sampleColorLUT(tiny, 0.99)).toBe(111); // 0.99 * 1 = 0.99 → 0
expect(sampleColorLUT(tiny, 1)).toBe(222);
});
it('NaN input falls through to lut[0] (NaN comparisons are false)', () => {
// tc = NaN, NaN * anything = NaN, NaN | 0 === 0
expect(sampleColorLUT(lut, NaN)).toBe(1000);
});
});
});