@celestia-island/hikari 0.55.410 → 0.55.412
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/theme/index.ts +2 -0
- package/src/theme/semanticPalette.test.ts +812 -0
- package/src/theme/semanticPalette.ts +565 -0
|
@@ -0,0 +1,812 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { stockDefaultPreset, type ThemeTokenRGB } from "./presets";
|
|
4
|
+
import {
|
|
5
|
+
CANONICAL_SEMANTIC,
|
|
6
|
+
contrastRatio,
|
|
7
|
+
deriveSemanticPalette,
|
|
8
|
+
hueDistance,
|
|
9
|
+
oklchToRgb,
|
|
10
|
+
rgbToOklch,
|
|
11
|
+
} from "./semanticPalette";
|
|
12
|
+
|
|
13
|
+
const rgb = (r: number, g: number, b: number): ThemeTokenRGB => ({ r, g, b });
|
|
14
|
+
|
|
15
|
+
// Guide-shaped three-anchor fixtures (Catppuccin Mocha / Latte anchors)
|
|
16
|
+
// — the shape chest's plugin guide documents and its applier fills.
|
|
17
|
+
const DARK_FILLS = {
|
|
18
|
+
primary: rgb(24, 24, 37),
|
|
19
|
+
background: rgb(17, 17, 27),
|
|
20
|
+
surface: rgb(49, 50, 68),
|
|
21
|
+
};
|
|
22
|
+
const LIGHT_FILLS = {
|
|
23
|
+
primary: rgb(30, 102, 245),
|
|
24
|
+
background: rgb(239, 241, 245),
|
|
25
|
+
surface: rgb(255, 255, 255),
|
|
26
|
+
};
|
|
27
|
+
const OPPOSITE_FILLS = {
|
|
28
|
+
primary: rgb(136, 192, 208),
|
|
29
|
+
background: rgb(245, 245, 240), // light bg …
|
|
30
|
+
surface: rgb(22, 27, 38), // … dark surface
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
describe("OKLCH conversion", () => {
|
|
34
|
+
it("round-trips 8-bit colours EXACTLY (any matrix typo reddens)", () => {
|
|
35
|
+
for (let i = 0; i < 64; i++) {
|
|
36
|
+
const c = rgb(
|
|
37
|
+
(i * 37) % 256,
|
|
38
|
+
(i * 91) % 256,
|
|
39
|
+
(i * 151) % 256,
|
|
40
|
+
);
|
|
41
|
+
expect(oklchToRgb(rgbToOklch(c))).toEqual(c);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("matches Ottosson's reference values (the toe and the matrices)", () => {
|
|
46
|
+
expect(rgbToOklch(rgb(255, 0, 0)).h).toBeCloseTo(29.23, 1);
|
|
47
|
+
expect(rgbToOklch(rgb(0, 0, 255)).h).toBeCloseTo(264.05, 1);
|
|
48
|
+
expect(rgbToOklch(rgb(255, 255, 255)).l).toBeCloseTo(1.0, 3);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("keeps a constant hue when only lightness changes", () => {
|
|
52
|
+
const a = rgbToOklch(oklchToRgb({ l: 0.7, c: 0.11, h: 150 }));
|
|
53
|
+
const b = rgbToOklch(oklchToRgb({ l: 0.4, c: 0.11, h: 150 }));
|
|
54
|
+
expect(hueDistance(a.h, 150)).toBeLessThan(2);
|
|
55
|
+
expect(hueDistance(b.h, 150)).toBeLessThan(2);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe("the canonical family", () => {
|
|
60
|
+
it("matches BOTH shipped stock tables' hues within 20°", () => {
|
|
61
|
+
const families = [
|
|
62
|
+
{ success: stockDefaultPreset.dark.success, error: stockDefaultPreset.dark.error, warning: stockDefaultPreset.dark.warning, info: stockDefaultPreset.dark.info },
|
|
63
|
+
{ success: stockDefaultPreset.light.success, error: stockDefaultPreset.light.error, warning: stockDefaultPreset.light.warning, info: stockDefaultPreset.light.info },
|
|
64
|
+
];
|
|
65
|
+
for (const fam of families) {
|
|
66
|
+
for (const key of ["success", "error", "warning", "info"] as const) {
|
|
67
|
+
expect(
|
|
68
|
+
hueDistance(rgbToOklch(fam[key]).h, CANONICAL_SEMANTIC[key].h),
|
|
69
|
+
`${key} canonical hue`,
|
|
70
|
+
).toBeLessThan(20);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// The mid-tone regime (the readable band sits at the DARK end): the
|
|
77
|
+
// gamut mapping must keep the four hues distinct here, where a
|
|
78
|
+
// per-channel clamp collapsed error and warning to the same bytes.
|
|
79
|
+
const GRAPHITE_FILLS = {
|
|
80
|
+
primary: rgb(120, 120, 125),
|
|
81
|
+
background: rgb(126, 126, 131),
|
|
82
|
+
surface: rgb(144, 144, 149),
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
describe("deriveSemanticPalette — the documented 3-anchor shapes", () => {
|
|
86
|
+
it("every TEXT-bearing semantic slot clears AA on BOTH fills (dark)", () => {
|
|
87
|
+
const p = deriveSemanticPalette(DARK_FILLS);
|
|
88
|
+
for (const slot of ["success", "error", "warning", "info", "secondary", "accent"] as const) {
|
|
89
|
+
expect(
|
|
90
|
+
contrastRatio(p[slot], DARK_FILLS.background),
|
|
91
|
+
`${slot}/background`,
|
|
92
|
+
).toBeGreaterThanOrEqual(4.5);
|
|
93
|
+
expect(contrastRatio(p[slot], DARK_FILLS.surface), `${slot}/surface`).toBeGreaterThanOrEqual(4.5);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("every TEXT-bearing semantic slot clears AA on BOTH fills (light)", () => {
|
|
98
|
+
const p = deriveSemanticPalette(LIGHT_FILLS);
|
|
99
|
+
for (const slot of ["success", "error", "warning", "info", "secondary", "accent"] as const) {
|
|
100
|
+
expect(contrastRatio(p[slot], LIGHT_FILLS.background), `${slot}/background`).toBeGreaterThanOrEqual(4.5);
|
|
101
|
+
expect(contrastRatio(p[slot], LIGHT_FILLS.surface), `${slot}/surface`).toBeGreaterThanOrEqual(4.5);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("the four semantic tones stay DISTINCT (pairwise hue ≥ 30°)", () => {
|
|
106
|
+
for (const fills of [DARK_FILLS, LIGHT_FILLS, OPPOSITE_FILLS]) {
|
|
107
|
+
const p = deriveSemanticPalette(fills);
|
|
108
|
+
const hues = {
|
|
109
|
+
success: rgbToOklch(p.success).h,
|
|
110
|
+
error: rgbToOklch(p.error).h,
|
|
111
|
+
warning: rgbToOklch(p.warning).h,
|
|
112
|
+
info: rgbToOklch(p.info).h,
|
|
113
|
+
};
|
|
114
|
+
const entries = Object.entries(hues);
|
|
115
|
+
for (let i = 0; i < entries.length; i++) {
|
|
116
|
+
for (let j = i + 1; j < entries.length; j++) {
|
|
117
|
+
expect(
|
|
118
|
+
hueDistance(entries[i][1], entries[j][1]),
|
|
119
|
+
`${entries[i][0]} vs ${entries[j][0]}`,
|
|
120
|
+
).toBeGreaterThanOrEqual(30);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("the derived tones couple to CANONICAL_SEMANTIC (the constant IS the hue)", () => {
|
|
127
|
+
for (const fills of [DARK_FILLS, LIGHT_FILLS, OPPOSITE_FILLS]) {
|
|
128
|
+
const p = deriveSemanticPalette(fills);
|
|
129
|
+
for (const key of ["success", "error", "warning", "info"] as const) {
|
|
130
|
+
expect(
|
|
131
|
+
hueDistance(rgbToOklch(p[key]).h, CANONICAL_SEMANTIC[key].h),
|
|
132
|
+
`${key} derives from the canonical constant`,
|
|
133
|
+
).toBeLessThan(15);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("secondary/accent are the primary's hue rotated ±40° (before clamping)", () => {
|
|
139
|
+
const p = deriveSemanticPalette(DARK_FILLS);
|
|
140
|
+
const h = rgbToOklch(DARK_FILLS.primary).h;
|
|
141
|
+
// Post-chroma-reduction the rotations hold tightly (measured dark
|
|
142
|
+
// +40.2/-38.0, light +39.7/-39.5); the tolerance absorbs the grid
|
|
143
|
+
// and the 1504-step resolution's slight pick shifts.
|
|
144
|
+
expect(hueDistance(rgbToOklch(p.secondary).h, (h + 40) % 360)).toBeLessThanOrEqual(2);
|
|
145
|
+
expect(hueDistance(rgbToOklch(p.accent).h, (h + 320) % 360)).toBeLessThanOrEqual(3);
|
|
146
|
+
// LIGHT too: the accent's pick must not drive it to the band bottom
|
|
147
|
+
// (near-black, hue destroyed) on light fills.
|
|
148
|
+
const lightP = deriveSemanticPalette(LIGHT_FILLS);
|
|
149
|
+
const lh = rgbToOklch(LIGHT_FILLS.primary).h;
|
|
150
|
+
expect(hueDistance(rgbToOklch(lightP.secondary).h, (lh + 40) % 360)).toBeLessThanOrEqual(2);
|
|
151
|
+
expect(hueDistance(rgbToOklch(lightP.accent).h, (lh + 320) % 360)).toBeLessThanOrEqual(2);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("opposite-polarity fills sit at the chroma-rich NEAR-optimum (≥3.6, distinct)", () => {
|
|
155
|
+
// No lightness clears AA on opposite anchors; the fallback keeps the
|
|
156
|
+
// most surviving chroma within the window — the measured band is
|
|
157
|
+
// 3.57-3.68 here (the raw optimum would trade the hue for a few
|
|
158
|
+
// hundredths it does not need).
|
|
159
|
+
const p = deriveSemanticPalette(OPPOSITE_FILLS);
|
|
160
|
+
const bytes = (["success", "error", "warning", "info"] as const).map(
|
|
161
|
+
(slot) => `${p[slot].r},${p[slot].g},${p[slot].b}`,
|
|
162
|
+
);
|
|
163
|
+
expect(new Set(bytes).size, "the four tones stay distinct").toBe(4);
|
|
164
|
+
for (const slot of ["success", "error", "warning", "info", "secondary", "accent"] as const) {
|
|
165
|
+
const worst = Math.min(
|
|
166
|
+
contrastRatio(p[slot], OPPOSITE_FILLS.background),
|
|
167
|
+
contrastRatio(p[slot], OPPOSITE_FILLS.surface),
|
|
168
|
+
);
|
|
169
|
+
expect(worst, `${slot} near-optimal`).toBeGreaterThanOrEqual(3.55);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("the semantic hues are the CANONICAL hues, not the primary's", () => {
|
|
174
|
+
const p = deriveSemanticPalette(DARK_FILLS);
|
|
175
|
+
// The guide fixture's primary is a desaturated indigo (~295°): every
|
|
176
|
+
// canonical hue must be far from it.
|
|
177
|
+
const primaryHue = rgbToOklch(DARK_FILLS.primary).h;
|
|
178
|
+
for (const key of ["success", "error", "warning", "info"] as const) {
|
|
179
|
+
expect(hueDistance(rgbToOklch(p[key]).h, primaryHue), key).toBeGreaterThanOrEqual(30);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("the brand chroma cap holds (secondary/accent ≤0.125 after rounding)", () => {
|
|
184
|
+
for (const fills of [DARK_FILLS, LIGHT_FILLS, GRAPHITE_FILLS]) {
|
|
185
|
+
const p = deriveSemanticPalette(fills);
|
|
186
|
+
expect(rgbToOklch(p.secondary).c).toBeLessThanOrEqual(0.125);
|
|
187
|
+
expect(rgbToOklch(p.accent).c).toBeLessThanOrEqual(0.125);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("borders follow the primary's hue at reduced chroma", () => {
|
|
192
|
+
const p = deriveSemanticPalette(DARK_FILLS);
|
|
193
|
+
const primaryHue = rgbToOklch(DARK_FILLS.primary).h;
|
|
194
|
+
// The 15° tolerance: the border chroma is ~0.005 (25% of the
|
|
195
|
+
// primary's), where 8-bit quantisation makes hue noisy — the
|
|
196
|
+
// measured DARK border sits at 12.77° with a DIFFERENT byte
|
|
197
|
+
// (77,78,81 vs the 376-grid's 78,78,82). The focused border (at
|
|
198
|
+
// higher chroma) holds <1.5°.
|
|
199
|
+
expect(hueDistance(rgbToOklch(p.border).h, primaryHue)).toBeLessThan(15);
|
|
200
|
+
expect(hueDistance(rgbToOklch(p.focusedBorder).h, primaryHue)).toBeLessThan(15);
|
|
201
|
+
expect(rgbToOklch(p.border).c).toBeLessThan(rgbToOklch(p.focusedBorder).c);
|
|
202
|
+
// Borders are non-text: they clear 1.5 / 3 respectively.
|
|
203
|
+
expect(contrastRatio(p.border, DARK_FILLS.background)).toBeGreaterThanOrEqual(1.5);
|
|
204
|
+
expect(contrastRatio(p.focusedBorder, DARK_FILLS.background)).toBeGreaterThanOrEqual(3);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("a mid-tone (graphite) theme keeps the four hues distinct", () => {
|
|
208
|
+
const p = deriveSemanticPalette(GRAPHITE_FILLS);
|
|
209
|
+
const hues = {
|
|
210
|
+
success: rgbToOklch(p.success).h,
|
|
211
|
+
error: rgbToOklch(p.error).h,
|
|
212
|
+
warning: rgbToOklch(p.warning).h,
|
|
213
|
+
info: rgbToOklch(p.info).h,
|
|
214
|
+
};
|
|
215
|
+
const entries = Object.entries(hues);
|
|
216
|
+
for (let i = 0; i < entries.length; i++) {
|
|
217
|
+
for (let j = i + 1; j < entries.length; j++) {
|
|
218
|
+
expect(
|
|
219
|
+
hueDistance(entries[i][1], entries[j][1]),
|
|
220
|
+
`${entries[i][0]} vs ${entries[j][0]}`,
|
|
221
|
+
).toBeGreaterThanOrEqual(30);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
for (const slot of ["success", "error", "warning", "info"] as const) {
|
|
225
|
+
expect(contrastRatio(p[slot], GRAPHITE_FILLS.background)).toBeGreaterThanOrEqual(4.5);
|
|
226
|
+
expect(contrastRatio(p[slot], GRAPHITE_FILLS.surface)).toBeGreaterThanOrEqual(4.5);
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("the 0.70 calibration is pinned by SNAPSHOT (dark guide fixture)", () => {
|
|
231
|
+
// The bias is a VALUE choice; any drift (0.55, 0.85, ...) moves
|
|
232
|
+
// bytes inside the clearing band with no other pin the wiser.
|
|
233
|
+
const p = deriveSemanticPalette(DARK_FILLS);
|
|
234
|
+
expect(`${p.success.r},${p.success.g},${p.success.b}`).toBe("108,177,119");
|
|
235
|
+
expect(`${p.error.r},${p.error.g},${p.error.b}`).toBe("245,115,119");
|
|
236
|
+
expect(`${p.warning.r},${p.warning.g},${p.warning.b}`).toBe("218,137,68");
|
|
237
|
+
expect(`${p.info.r},${p.info.g},${p.info.b}`).toBe("84,173,193");
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("the search keeps the AA MARGIN (graphite tightest slot ≥4.55)", () => {
|
|
241
|
+
const p = deriveSemanticPalette(GRAPHITE_FILLS);
|
|
242
|
+
let tightest = Number.POSITIVE_INFINITY;
|
|
243
|
+
for (const slot of ["success", "error", "warning", "info"] as const) {
|
|
244
|
+
tightest = Math.min(
|
|
245
|
+
tightest,
|
|
246
|
+
contrastRatio(p[slot], GRAPHITE_FILLS.background),
|
|
247
|
+
contrastRatio(p[slot], GRAPHITE_FILLS.surface),
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
expect(tightest).toBeGreaterThanOrEqual(4.55);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("the border rungs never invert (the ceiling holds the order everywhere)", () => {
|
|
254
|
+
// The CEILING holds the min-anchor order on mixed-polarity fills
|
|
255
|
+
// (567/864 invert without it in an independent grid, 0 with it —
|
|
256
|
+
// 5,184 more swept pairs agree). The focused 3 rung itself can be
|
|
257
|
+
// UNREACHABLE on some mixed fills (measured down to ~1.8 — no
|
|
258
|
+
// single colour clears 3 on opposite anchors; the same physics as
|
|
259
|
+
// the text tones' no-win band), so this pin asserts the ORDER
|
|
260
|
+
// everywhere and the thresholds where they are reachable.
|
|
261
|
+
//
|
|
262
|
+
// (Historical: an earlier revision claimed mixed polarity
|
|
263
|
+
// "necessarily inverts" — that proof held only for a construction
|
|
264
|
+
// without the ceiling.)
|
|
265
|
+
for (const fills of [DARK_FILLS, LIGHT_FILLS, GRAPHITE_FILLS, OPPOSITE_FILLS]) {
|
|
266
|
+
const p = deriveSemanticPalette(fills);
|
|
267
|
+
const rest = Math.min(
|
|
268
|
+
contrastRatio(p.border, fills.background),
|
|
269
|
+
contrastRatio(p.border, fills.surface),
|
|
270
|
+
);
|
|
271
|
+
const focus = Math.min(
|
|
272
|
+
contrastRatio(p.focusedBorder, fills.background),
|
|
273
|
+
contrastRatio(p.focusedBorder, fills.surface),
|
|
274
|
+
);
|
|
275
|
+
expect(rest, "the resting border sits under the focused rung").toBeLessThan(focus);
|
|
276
|
+
expect(rest).toBeGreaterThanOrEqual(1.5);
|
|
277
|
+
expect(focus).toBeGreaterThanOrEqual(3);
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("a no-win fill pair keeps the four tones DISTINCT (the chroma-rich fallback)", () => {
|
|
282
|
+
// gray-500 neutrals: no lightness clears AA on both fills — the raw
|
|
283
|
+
// max-min optimum starves the chroma at the achromatic extremes and
|
|
284
|
+
// collapsed error/warning to the same bytes. The fallback picks the
|
|
285
|
+
// NEAR-optimal candidate with the most surviving chroma, so the
|
|
286
|
+
// tones stay distinguishable; AA is unattainable here by physics.
|
|
287
|
+
const noWin = {
|
|
288
|
+
primary: rgb(120, 120, 120),
|
|
289
|
+
background: rgb(107, 114, 128),
|
|
290
|
+
surface: rgb(156, 163, 175),
|
|
291
|
+
};
|
|
292
|
+
const p = deriveSemanticPalette(noWin);
|
|
293
|
+
const bytes = (["success", "error", "warning", "info"] as const).map(
|
|
294
|
+
(slot) => `${p[slot].r},${p[slot].g},${p[slot].b}`,
|
|
295
|
+
);
|
|
296
|
+
expect(new Set(bytes).size, "the four tones are byte-distinct").toBe(4);
|
|
297
|
+
for (const slot of ["success", "error", "warning", "info"] as const) {
|
|
298
|
+
const worst = Math.min(
|
|
299
|
+
contrastRatio(p[slot], noWin.background),
|
|
300
|
+
contrastRatio(p[slot], noWin.surface),
|
|
301
|
+
);
|
|
302
|
+
expect(worst, `${slot} soft floor`).toBeGreaterThanOrEqual(3.0);
|
|
303
|
+
for (const ch of [p[slot].r, p[slot].g, p[slot].b]) {
|
|
304
|
+
expect(ch).toBeGreaterThanOrEqual(0);
|
|
305
|
+
expect(ch).toBeLessThanOrEqual(255);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it("both barrels export the derivation (anchored export statements)", async () => {
|
|
311
|
+
// An anchored ^export{} regex, not a substring: a commented-out
|
|
312
|
+
// export line no longer matches. (A real import of the package root
|
|
313
|
+
// pulls the whole component tree — 5s+ — so the statement form is
|
|
314
|
+
// the honest cheap pin.)
|
|
315
|
+
const { readFileSync } = await import("node:fs");
|
|
316
|
+
const { resolve } = await import("node:path");
|
|
317
|
+
const root = readFileSync(resolve(process.cwd(), "src/index.ts"), "utf-8");
|
|
318
|
+
const theme = readFileSync(resolve(process.cwd(), "src/theme/index.ts"), "utf-8");
|
|
319
|
+
const names = [
|
|
320
|
+
"deriveSemanticPalette",
|
|
321
|
+
"CANONICAL_SEMANTIC",
|
|
322
|
+
"contrastRatio",
|
|
323
|
+
"hueDistance",
|
|
324
|
+
"rgbToOklch",
|
|
325
|
+
"oklchToRgb",
|
|
326
|
+
"SemanticPalette",
|
|
327
|
+
"SemanticFills",
|
|
328
|
+
"OKLCH",
|
|
329
|
+
];
|
|
330
|
+
for (const [label, src] of [
|
|
331
|
+
["root", root],
|
|
332
|
+
["theme", theme],
|
|
333
|
+
] as const) {
|
|
334
|
+
const stmts = [...src.matchAll(/^export (?:type )?\{([^}]*)\}/gm)].map((m) => m[1]).join(" ");
|
|
335
|
+
for (const name of names) {
|
|
336
|
+
expect(stmts, `${label} exports ${name}`).toContain(name);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it("the NON-CONVEX gamut spots round-trip exactly (the prescan)", () => {
|
|
342
|
+
// The in-gamut set along chroma is non-convex (channels are cubic in
|
|
343
|
+
// c): a bare binary search stops at the first boundary. These are
|
|
344
|
+
// the measured misses (h≈264°, the blue boundary) — deleting the
|
|
345
|
+
// linear prescan reddens here.
|
|
346
|
+
for (const c of [rgb(0, 0, 110), rgb(0, 0, 231), rgb(0, 0, 119)]) {
|
|
347
|
+
expect(oklchToRgb(rgbToOklch(c)), `${c.r},${c.g},${c.b}`).toEqual(c);
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it("oklchToRgb sanitizes hostile inputs (never NaN into a cssvar)", () => {
|
|
352
|
+
const out = oklchToRgb({ l: NaN, c: 0.1, h: 100 });
|
|
353
|
+
expect(Number.isFinite(out.r)).toBe(true);
|
|
354
|
+
expect(Number.isFinite(out.g)).toBe(true);
|
|
355
|
+
expect(Number.isFinite(out.b)).toBe(true);
|
|
356
|
+
const mirrored = oklchToRgb({ l: 0.5, c: -0.1, h: 100 });
|
|
357
|
+
// Negative chroma clamps to ZERO — a grey (a mirrored hue would be
|
|
358
|
+
// the silent alternative).
|
|
359
|
+
expect(mirrored.r).toBe(mirrored.g);
|
|
360
|
+
expect(mirrored.g).toBe(mirrored.b);
|
|
361
|
+
const wrapped = oklchToRgb({ l: 0.5, c: 0.1, h: 460 });
|
|
362
|
+
expect(rgbToOklch(wrapped).h).toBeCloseTo(100, 0);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it("a grey primary on LIGHT/dark fills splits via the full AA band", () => {
|
|
366
|
+
// The r11 S1 class: the pick flip only probed the two extremes and
|
|
367
|
+
// missed the interior candidate.
|
|
368
|
+
const p = deriveSemanticPalette({
|
|
369
|
+
primary: rgb(128, 128, 128),
|
|
370
|
+
background: rgb(240, 240, 240),
|
|
371
|
+
surface: rgb(179, 64, 205),
|
|
372
|
+
});
|
|
373
|
+
const sec = `${p.secondary.r},${p.secondary.g},${p.secondary.b}`;
|
|
374
|
+
const acc = `${p.accent.r},${p.accent.g},${p.accent.b}`;
|
|
375
|
+
expect(sec !== acc, `sec ${sec} vs acc ${acc}`).toBe(true);
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
it("the coincidence scan's own effect (byte snapshot on the divergence shape)", () => {
|
|
379
|
+
// BYTE SNAPSHOT on the shape where the scan and the fallback
|
|
380
|
+
// DISAGREE (r26 delta): accent 2,0,0 with the scan, 1,0,0 without.
|
|
381
|
+
// This is the ONLY fixture class that pins the scan itself.
|
|
382
|
+
const p = deriveSemanticPalette({
|
|
383
|
+
primary: rgb(110, 251, 80),
|
|
384
|
+
background: rgb(100, 106, 212),
|
|
385
|
+
surface: rgb(101, 156, 44),
|
|
386
|
+
});
|
|
387
|
+
expect(`${p.accent.r},${p.accent.g},${p.accent.b}`).toBe("2,0,0");
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
it("the ACHROMATIC branch puts secondary at the band TOP (byte snapshot)", () => {
|
|
391
|
+
// BYTE SNAPSHOT (r25: the property-based sec!==acc was vacuous —
|
|
392
|
+
// the fallback guarantees it regardless). With the branch:
|
|
393
|
+
// secondary 255,255,255 (band top, pick "high"), accent 241,241,241
|
|
394
|
+
// (band bottom, pick "low"). Without: the bytes swap.
|
|
395
|
+
const p = deriveSemanticPalette({
|
|
396
|
+
primary: rgb(120, 120, 120),
|
|
397
|
+
background: rgb(50, 50, 50),
|
|
398
|
+
surface: rgb(150, 150, 150),
|
|
399
|
+
});
|
|
400
|
+
expect(`${p.secondary.r},${p.secondary.g},${p.secondary.b}`).toBe("255,255,255");
|
|
401
|
+
expect(`${p.accent.r},${p.accent.g},${p.accent.b}`).toBe("241,241,241");
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
it("a grey primary on WHITE/BLACK fills splits secondary/accent (the fine band)", () => {
|
|
405
|
+
// The 4.55 target band holds ONE byte (117) but the AA band holds
|
|
406
|
+
// two (117@4.5578, 118@4.5422): the retake flips the pick and takes
|
|
407
|
+
// 118 — distinct AND readable (the coarse grid + unguarded path
|
|
408
|
+
// returned 117 for BOTH slots on every grey primary).
|
|
409
|
+
const p = deriveSemanticPalette({
|
|
410
|
+
primary: rgb(128, 128, 128),
|
|
411
|
+
background: rgb(255, 255, 255),
|
|
412
|
+
surface: rgb(0, 0, 0),
|
|
413
|
+
});
|
|
414
|
+
const sec = `${p.secondary.r},${p.secondary.g},${p.secondary.b}`;
|
|
415
|
+
const acc = `${p.accent.r},${p.accent.g},${p.accent.b}`;
|
|
416
|
+
expect(sec !== acc, `sec ${sec} vs acc ${acc}`).toBe(true);
|
|
417
|
+
for (const slot of [p.secondary, p.accent]) {
|
|
418
|
+
expect(Math.min(contrastRatio(slot, { r: 255, g: 255, b: 255 }), contrastRatio(slot, { r: 0, g: 0, b: 0 }))).toBeGreaterThanOrEqual(4.5);
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it("an ACHROMATIC primary keeps secondary !== accent (incl. no-win fills)", () => {
|
|
423
|
+
// The ±40° rotations are vacuous at c=0 — the split is by
|
|
424
|
+
// LIGHTNESS (the fallback tie-break), and it must hold on the
|
|
425
|
+
// no-win shapes where the hit path never runs.
|
|
426
|
+
const shapes = [
|
|
427
|
+
{ primary: rgb(128, 128, 128), background: rgb(224, 224, 224), surface: rgb(255, 255, 255) },
|
|
428
|
+
{ primary: rgb(120, 120, 120), background: rgb(107, 114, 128), surface: rgb(156, 163, 175) },
|
|
429
|
+
{ primary: rgb(120, 120, 120), background: rgb(245, 245, 240), surface: rgb(22, 27, 38) },
|
|
430
|
+
];
|
|
431
|
+
for (const fills of shapes) {
|
|
432
|
+
const p = deriveSemanticPalette(fills);
|
|
433
|
+
const sec = `${p.secondary.r},${p.secondary.g},${p.secondary.b}`;
|
|
434
|
+
const acc = `${p.accent.r},${p.accent.g},${p.accent.b}`;
|
|
435
|
+
expect(sec !== acc, `sec ${sec} vs acc ${acc}`).toBe(true);
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it("rgbToOklch sanitises hostile inputs (finite LCH, never NaN)", () => {
|
|
440
|
+
const out = rgbToOklch({ r: NaN, g: 10, b: 10 });
|
|
441
|
+
expect(Number.isFinite(out.l)).toBe(true);
|
|
442
|
+
expect(Number.isFinite(out.c)).toBe(true);
|
|
443
|
+
expect(Number.isFinite(out.h)).toBe(true);
|
|
444
|
+
const neg = rgbToOklch({ r: -40, g: 300, b: 12.7 });
|
|
445
|
+
expect(neg.l).toBeGreaterThanOrEqual(0);
|
|
446
|
+
expect(neg.l).toBeLessThanOrEqual(1);
|
|
447
|
+
expect(Number.isFinite(contrastRatio({ r: NaN, g: NaN, b: NaN }, { r: 0, g: 0, b: 0 }))).toBe(true);
|
|
448
|
+
// NaN reads as hue 0 — the distance to 10 is 10, and 0 to itself.
|
|
449
|
+
expect(hueDistance(NaN, 10)).toBe(10);
|
|
450
|
+
expect(hueDistance(NaN, NaN)).toBe(0);
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
it("the accent split is PINNED (dropping it reds here)", () => {
|
|
454
|
+
// The r19 S2: deleting the accent re-derivation leaves sec≡acc
|
|
455
|
+
// on 224/4,096 grey shapes.
|
|
456
|
+
const p = deriveSemanticPalette({
|
|
457
|
+
primary: rgb(120, 120, 120),
|
|
458
|
+
background: rgb(0, 0, 0),
|
|
459
|
+
surface: rgb(120, 120, 120),
|
|
460
|
+
});
|
|
461
|
+
const sec = `${p.secondary.r},${p.secondary.g},${p.secondary.b}`;
|
|
462
|
+
const acc = `${p.accent.r},${p.accent.g},${p.accent.b}`;
|
|
463
|
+
expect(sec !== acc, `sec ${sec} vs acc ${acc}`).toBe(true);
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
it("the AA-preference retake keeps the maximum AA tones (r19 S1 pin)", () => {
|
|
467
|
+
// The r19 delta S1 shape: both members of the colliding pair were
|
|
468
|
+
// demoted below AA; one could keep its AA byte.
|
|
469
|
+
const p = deriveSemanticPalette({
|
|
470
|
+
primary: rgb(207, 9, 119),
|
|
471
|
+
background: rgb(6, 38, 233),
|
|
472
|
+
surface: rgb(88, 125, 126),
|
|
473
|
+
});
|
|
474
|
+
let aaCount = 0;
|
|
475
|
+
for (const slot of ["success", "error", "warning", "info"] as const) {
|
|
476
|
+
const w1 = contrastRatio(p[slot], { r: 6, g: 38, b: 233 });
|
|
477
|
+
const w2 = contrastRatio(p[slot], { r: 88, g: 125, b: 126 });
|
|
478
|
+
if (Math.min(w1, w2) >= 4.5) aaCount++;
|
|
479
|
+
}
|
|
480
|
+
expect(aaCount, "at least 3 of 4 tones keep AA (the retake's gain)").toBeGreaterThanOrEqual(3);
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
it("the window-band differentiate is PINNED (dropping it reds here)", () => {
|
|
484
|
+
// The r20 class: on no-win fills the AA band is empty; the
|
|
485
|
+
// window-band fallback splits the pair (3/22,814 shapes fixed).
|
|
486
|
+
const p = deriveSemanticPalette({
|
|
487
|
+
primary: rgb(121, 109, 121),
|
|
488
|
+
background: rgb(113, 121, 113),
|
|
489
|
+
surface: rgb(117, 109, 109),
|
|
490
|
+
});
|
|
491
|
+
const bytes = (["success", "error", "warning", "info"] as const).map(
|
|
492
|
+
(slot) => `${p[slot].r},${p[slot].g},${p[slot].b}`,
|
|
493
|
+
);
|
|
494
|
+
expect(new Set(bytes).size, "no-win tones stay distinct").toBe(4);
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
it("the resting border uses the FILLS' lightness (the low-emphasis bias)", () => {
|
|
498
|
+
// fillLight = mean(l): replacing it with 0.7 moved the border on
|
|
499
|
+
// 884/5,063 shapes (r22 adversarial M40).
|
|
500
|
+
const p = deriveSemanticPalette({
|
|
501
|
+
primary: rgb(0, 0, 0),
|
|
502
|
+
background: rgb(0, 0, 0),
|
|
503
|
+
surface: rgb(117, 117, 117),
|
|
504
|
+
});
|
|
505
|
+
// With fillLight the border sits near the fills (dark); with 0.7 it
|
|
506
|
+
// would sit at the mid-light. Pin the DARK position.
|
|
507
|
+
expect(p.border.r, "resting border follows the fills' lightness").toBeLessThan(100);
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
it("oklchToRgb sanitises a NaN HUE (never NaN into a cssvar)", () => {
|
|
511
|
+
// r22 adversarial M49: the hue guard had no pin.
|
|
512
|
+
const rgb = oklchToRgb({ l: -1, c: 0.11, h: NaN });
|
|
513
|
+
expect(Number.isFinite(rgb.r)).toBe(true);
|
|
514
|
+
expect(Number.isFinite(rgb.g)).toBe(true);
|
|
515
|
+
expect(Number.isFinite(rgb.b)).toBe(true);
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
it("the retake differs from EVERY tone's bytes (the all-tone guard)", () => {
|
|
519
|
+
// r23 adversarial M03: the partner-only check collapses warning
|
|
520
|
+
// onto success's byte on this fixture.
|
|
521
|
+
const p = deriveSemanticPalette({
|
|
522
|
+
primary: rgb(133, 84, 104),
|
|
523
|
+
background: rgb(23, 127, 149),
|
|
524
|
+
surface: rgb(96, 201, 47),
|
|
525
|
+
});
|
|
526
|
+
const bytes = (["success", "error", "warning", "info"] as const).map(
|
|
527
|
+
(slot) => `${p[slot].r},${p[slot].g},${p[slot].b}`,
|
|
528
|
+
);
|
|
529
|
+
expect(new Set(bytes).size, "all four tones pairwise distinct").toBe(4);
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
it("the no-win WINDOW width is a load-bearing value (0.35)", () => {
|
|
533
|
+
// r22 adversarial M27: window 0.35 → 0.05 moved 407/5,063 shapes.
|
|
534
|
+
// On this fixture the wider window keeps the tone readable.
|
|
535
|
+
const p = deriveSemanticPalette({
|
|
536
|
+
primary: rgb(0, 0, 0),
|
|
537
|
+
background: rgb(0, 0, 0),
|
|
538
|
+
surface: rgb(128, 128, 128),
|
|
539
|
+
});
|
|
540
|
+
// The success tone with the WIDE window keeps chroma (213,255,218);
|
|
541
|
+
// a narrow 0.05 window drops to near-white (250,255,250). Pin the
|
|
542
|
+
// exact byte.
|
|
543
|
+
expect(`${p.success.r},${p.success.g},${p.success.b}`, "the window keeps the chromatic pick").toBe("213,255,218");
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
it("hueDistance wraps at 360° (the public helper)", () => {
|
|
547
|
+
// r22 adversarial M17: the wrap had no pin.
|
|
548
|
+
expect(hueDistance(350, 10), "wraps to the short arc").toBe(20);
|
|
549
|
+
expect(hueDistance(10, 350), "symmetric").toBe(20);
|
|
550
|
+
expect(hueDistance(-720, -400), "wraps past zero").toBe(40);
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it("the accent split maximises the worst anchor (not chroma)", () => {
|
|
554
|
+
// r23 adversarial M06/M20: the chroma-max pick returned
|
|
555
|
+
// 159,132,214@1.44; the worst-anchor maximisation returns
|
|
556
|
+
// 255,254,255@4.46 (a +3.02 gain).
|
|
557
|
+
const p = deriveSemanticPalette({
|
|
558
|
+
primary: rgb(254, 162, 228),
|
|
559
|
+
background: rgb(98, 41, 180),
|
|
560
|
+
surface: rgb(181, 97, 3),
|
|
561
|
+
});
|
|
562
|
+
const accWorst = Math.min(
|
|
563
|
+
contrastRatio(p.accent, { r: 98, g: 41, b: 180 }),
|
|
564
|
+
contrastRatio(p.accent, { r: 181, g: 97, b: 3 }),
|
|
565
|
+
);
|
|
566
|
+
expect(accWorst, "the accent's worst anchor is maximised (near the 4.4 bar)").toBeGreaterThanOrEqual(4.4);
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
it("the resting border chroma is 0.25× the focused (subdued, not equal)", () => {
|
|
570
|
+
// r24 adversarial M13: the 0.25 factor had no pin. With 0.95 the
|
|
571
|
+
// resting border would match the focused border's intensity.
|
|
572
|
+
const p = deriveSemanticPalette({
|
|
573
|
+
primary: rgb(100, 50, 150),
|
|
574
|
+
background: rgb(20, 20, 30),
|
|
575
|
+
surface: rgb(40, 40, 55),
|
|
576
|
+
});
|
|
577
|
+
const bc = rgbToOklch(p.border).c;
|
|
578
|
+
const fc = rgbToOklch(p.focusedBorder).c;
|
|
579
|
+
// The resting border's chroma should be distinctly below the focused.
|
|
580
|
+
expect(bc, "resting chroma < focused").toBeLessThan(fc * 0.6);
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
it("the window-band tone retake maximises the worst anchor (byte snapshot)", () => {
|
|
584
|
+
// r27 journeys S2 repro: chroma-first landed warning at 4.17;
|
|
585
|
+
// worst-first at 4.48 (the max free distinct in-window byte).
|
|
586
|
+
const p = deriveSemanticPalette({
|
|
587
|
+
primary: rgb(12, 113, 254),
|
|
588
|
+
background: rgb(220, 51, 87),
|
|
589
|
+
surface: rgb(27, 56, 82),
|
|
590
|
+
});
|
|
591
|
+
// The warning tone's worst anchor (the best free distinct byte).
|
|
592
|
+
const worst = Math.min(
|
|
593
|
+
contrastRatio(p.warning, { r: 220, g: 51, b: 87 }),
|
|
594
|
+
contrastRatio(p.warning, { r: 27, g: 56, b: 82 }),
|
|
595
|
+
);
|
|
596
|
+
expect(worst, "warning's worst anchor is maximised").toBeGreaterThanOrEqual(4.4);
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
it("the AA-band retake takes the CHROMATIC candidate (byte snapshot)", () => {
|
|
600
|
+
// r28 adversarial M24: replacing the chroma criterion with worst
|
|
601
|
+
// turns the chromatic warning (4,0,0 @C=0.0275) into pure white
|
|
602
|
+
// (255,255,255 @C=0) — grey-instead-of-hue on 14/1,024 shapes.
|
|
603
|
+
const p = deriveSemanticPalette({
|
|
604
|
+
primary: rgb(130, 189, 67),
|
|
605
|
+
background: rgb(61, 131, 85),
|
|
606
|
+
surface: rgb(203, 43, 179),
|
|
607
|
+
});
|
|
608
|
+
expect(`${p.warning.r},${p.warning.g},${p.warning.b}`).toBe("4,0,0");
|
|
609
|
+
});
|
|
610
|
+
|
|
611
|
+
it("the near-bar bound: one rounding step short of AA keeps the near-best", () => {
|
|
612
|
+
// The r14/r15 S1 class: bg(0,0,0)/sf(252,252,234) — bestWorst
|
|
613
|
+
// 4.4995 was returned at 4.15 (the window took a 0.35 drop); the
|
|
614
|
+
// near-bar bound returns the pick-biased near-best (~4.48-4.50).
|
|
615
|
+
const p = deriveSemanticPalette({
|
|
616
|
+
primary: rgb(60, 60, 65),
|
|
617
|
+
background: rgb(0, 0, 0),
|
|
618
|
+
surface: rgb(252, 252, 234),
|
|
619
|
+
});
|
|
620
|
+
for (const slot of ["success", "error", "warning", "info"] as const) {
|
|
621
|
+
const worst = Math.min(
|
|
622
|
+
contrastRatio(p[slot], { r: 0, g: 0, b: 0 }),
|
|
623
|
+
contrastRatio(p[slot], { r: 252, g: 252, b: 234 }),
|
|
624
|
+
);
|
|
625
|
+
// The near-best keeps within 0.1 of the best worst-anchor (4.4995
|
|
626
|
+
// for the S1 shape), not the window's 0.35 drop.
|
|
627
|
+
expect(worst, `${slot} near-bar`).toBeGreaterThan(4.4);
|
|
628
|
+
}
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
it("the seam at 1504 catches the near-white sub-grid window", () => {
|
|
632
|
+
// The r13 S1 shape: bg(0,0,0)/sf(252,252,254) — the AA band is
|
|
633
|
+
// 0.00066 wide; the 376 grid straddled it (4.20 sub-AA) while 1504
|
|
634
|
+
// hits it (4.50).
|
|
635
|
+
const p = deriveSemanticPalette({
|
|
636
|
+
primary: rgb(60, 60, 65),
|
|
637
|
+
background: rgb(0, 0, 0),
|
|
638
|
+
surface: rgb(252, 252, 254),
|
|
639
|
+
});
|
|
640
|
+
for (const slot of ["success", "error", "warning", "info"] as const) {
|
|
641
|
+
const worst = Math.min(
|
|
642
|
+
contrastRatio(p[slot], { r: 0, g: 0, b: 0 }),
|
|
643
|
+
contrastRatio(p[slot], { r: 252, g: 252, b: 254 }),
|
|
644
|
+
);
|
|
645
|
+
expect(worst, `${slot} AA at the 1504 resolution`).toBeGreaterThanOrEqual(4.5);
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
it("the 4.55/AA SEAM: winnable shapes never fall to the sub-AA window", () => {
|
|
650
|
+
// surface 118 over a near-black bg: pure white clears AA (4.5422)
|
|
651
|
+
// but not the 4.55 margin — the shape is WINNABLE and must not land
|
|
652
|
+
// on the fallback's 4.22 near-white.
|
|
653
|
+
const fills = {
|
|
654
|
+
primary: rgb(58, 58, 66),
|
|
655
|
+
background: rgb(8, 8, 10),
|
|
656
|
+
surface: rgb(118, 118, 118),
|
|
657
|
+
};
|
|
658
|
+
const p = deriveSemanticPalette(fills);
|
|
659
|
+
for (const slot of ["success", "error", "warning", "info"] as const) {
|
|
660
|
+
expect(contrastRatio(p[slot], fills.surface), `${slot} AA`).toBeGreaterThanOrEqual(4.5);
|
|
661
|
+
}
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
it("the white-unique BOUNDARY: near-white AA AND distinctness", () => {
|
|
665
|
+
// surface rgb(117,117,117) over a near-black bg: only near-white
|
|
666
|
+
// candidates clear 4.55 — and the fine grid lands each hue's own,
|
|
667
|
+
// so the boundary holds BOTH readability and distinctness (measured
|
|
668
|
+
// at 1504: 250,255,250 / 255,254,253 / 255,252,249 / 252,254,255).
|
|
669
|
+
// A few steps down the tones gain full chroma.
|
|
670
|
+
const boundary = {
|
|
671
|
+
primary: rgb(58, 58, 66),
|
|
672
|
+
background: rgb(8, 8, 10),
|
|
673
|
+
surface: rgb(117, 117, 117),
|
|
674
|
+
};
|
|
675
|
+
const p = deriveSemanticPalette(boundary);
|
|
676
|
+
const bytes = (["success", "error", "warning", "info"] as const).map(
|
|
677
|
+
(slot) => `${p[slot].r},${p[slot].g},${p[slot].b}`,
|
|
678
|
+
);
|
|
679
|
+
expect(new Set(bytes).size, "the boundary tones are distinct").toBe(4);
|
|
680
|
+
for (const slot of ["success", "error", "warning", "info"] as const) {
|
|
681
|
+
expect(contrastRatio(p[slot], boundary.surface), `${slot} AA`).toBeGreaterThanOrEqual(4.5);
|
|
682
|
+
}
|
|
683
|
+
const distinct = {
|
|
684
|
+
primary: rgb(58, 58, 66),
|
|
685
|
+
background: rgb(8, 8, 10),
|
|
686
|
+
surface: rgb(110, 110, 110),
|
|
687
|
+
};
|
|
688
|
+
const q = deriveSemanticPalette(distinct);
|
|
689
|
+
const bytes2 = (["success", "error", "warning", "info"] as const).map(
|
|
690
|
+
(slot) => `${q[slot].r},${q[slot].g},${q[slot].b}`,
|
|
691
|
+
);
|
|
692
|
+
expect(new Set(bytes2).size, "a few steps down: still distinct").toBe(4);
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
it("the WHITE-UNIQUE class: AA reachable only at the near-white extreme", () => {
|
|
696
|
+
// A near-black bg + a ~117-grey surface: only near-white candidates
|
|
697
|
+
// clear AA — and the FINE grid finds each hue's OWN near-white, so
|
|
698
|
+
// the four tones stay DISTINCT at the boundary (the coarse grid
|
|
699
|
+
// collapsed them all to pure white). A grid capped short of the
|
|
700
|
+
// endpoints declared this class "no-win" inside a truncated domain.
|
|
701
|
+
const fills = {
|
|
702
|
+
primary: rgb(60, 60, 65),
|
|
703
|
+
background: rgb(0, 0, 0),
|
|
704
|
+
surface: rgb(117, 117, 120),
|
|
705
|
+
};
|
|
706
|
+
const p = deriveSemanticPalette(fills);
|
|
707
|
+
const bytes = (["success", "error", "warning", "info"] as const).map(
|
|
708
|
+
(slot) => `${p[slot].r},${p[slot].g},${p[slot].b}`,
|
|
709
|
+
);
|
|
710
|
+
expect(new Set(bytes).size, "the four near-whites are distinct").toBe(4);
|
|
711
|
+
for (const slot of ["success", "error", "warning", "info"] as const) {
|
|
712
|
+
expect(contrastRatio(p[slot], fills.surface), `${slot} AA on the surface`).toBeGreaterThanOrEqual(4.5);
|
|
713
|
+
}
|
|
714
|
+
});
|
|
715
|
+
|
|
716
|
+
it("the achromatic RAZOR: coincidence is physics, kept and disclosed", () => {
|
|
717
|
+
// bg #757575 / surface #787878: the 4.55 band is a single grey
|
|
718
|
+
// (black) for secondary, but the AA band holds two — the retake
|
|
719
|
+
// flips the pick and splits the pair (0,0,0 vs 2,2,2).
|
|
720
|
+
const p = deriveSemanticPalette({
|
|
721
|
+
primary: rgb(120, 120, 120),
|
|
722
|
+
background: rgb(117, 117, 117),
|
|
723
|
+
surface: rgb(120, 120, 120),
|
|
724
|
+
});
|
|
725
|
+
const worst = (c: { r: number; g: number; b: number }) =>
|
|
726
|
+
Math.min(
|
|
727
|
+
contrastRatio(c, { r: 117, g: 117, b: 117 }),
|
|
728
|
+
contrastRatio(c, { r: 120, g: 120, b: 120 }),
|
|
729
|
+
);
|
|
730
|
+
expect(worst(p.secondary)).toBeGreaterThanOrEqual(4.5);
|
|
731
|
+
expect(worst(p.accent), "the accent keeps AA (coinciding where physics forces it)").toBeGreaterThanOrEqual(4.5);
|
|
732
|
+
});
|
|
733
|
+
|
|
734
|
+
it("the differentiation itself is PINNED (deleting it reds here)", () => {
|
|
735
|
+
// The guard's effect on the razor fixture: without differentiation,
|
|
736
|
+
// error === warning on these fills.
|
|
737
|
+
const p = deriveSemanticPalette({
|
|
738
|
+
primary: rgb(120, 120, 120),
|
|
739
|
+
background: rgb(117, 117, 117),
|
|
740
|
+
surface: rgb(120, 120, 120),
|
|
741
|
+
});
|
|
742
|
+
const bytes = (["success", "error", "warning", "info"] as const).map(
|
|
743
|
+
(slot) => `${p[slot].r},${p[slot].g},${p[slot].b}`,
|
|
744
|
+
);
|
|
745
|
+
expect(new Set(bytes).size, "razor tones distinct").toBe(4);
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
it("BOTH rungs unreachable: the achromatic split still differentiates", () => {
|
|
749
|
+
// bg pure black + a 144-grey surface: NO grey clears 4.55 (secondary)
|
|
750
|
+
// or even 3.5 (accent) on both anchors — both slots take the
|
|
751
|
+
// fallback, whose window-end picks must still split them.
|
|
752
|
+
const fills = {
|
|
753
|
+
primary: rgb(120, 120, 120),
|
|
754
|
+
background: rgb(0, 0, 0),
|
|
755
|
+
surface: rgb(144, 144, 144),
|
|
756
|
+
};
|
|
757
|
+
const p = deriveSemanticPalette(fills);
|
|
758
|
+
const sec = `${p.secondary.r},${p.secondary.g},${p.secondary.b}`;
|
|
759
|
+
const acc = `${p.accent.r},${p.accent.g},${p.accent.b}`;
|
|
760
|
+
expect(sec !== acc, `sec ${sec} vs acc ${acc}`).toBe(true);
|
|
761
|
+
});
|
|
762
|
+
|
|
763
|
+
it("the gamut search survives a huge finite chroma (the 0.4 cap)", () => {
|
|
764
|
+
// c=1e6 must land byte-exactly where c=0.4 (the cap) does — a
|
|
765
|
+
// spread threshold let the uncapped search pass.
|
|
766
|
+
const capped = oklchToRgb({ l: 0.6, c: 0.4, h: 100 });
|
|
767
|
+
const huge = oklchToRgb({ l: 0.6, c: 1e6, h: 100 });
|
|
768
|
+
expect(huge).toEqual(capped);
|
|
769
|
+
// And the capped result is CHROMATIC (the largest in-gamut chroma,
|
|
770
|
+
// not the grey an unbounded first probe degenerates to).
|
|
771
|
+
expect(Math.max(huge.r, huge.g, huge.b) - Math.min(huge.r, huge.g, huge.b)).toBeGreaterThan(20);
|
|
772
|
+
});
|
|
773
|
+
|
|
774
|
+
it("a FLOAT surface still yields integer channels everywhere", () => {
|
|
775
|
+
const p = deriveSemanticPalette({
|
|
776
|
+
primary: rgb(30, 102, 245),
|
|
777
|
+
background: { r: 239.4, g: 241.2, b: 245.6 },
|
|
778
|
+
surface: { r: 254.5, g: 254.5, b: 254.5 },
|
|
779
|
+
});
|
|
780
|
+
for (const v of Object.values(p)) {
|
|
781
|
+
expect(Number.isInteger(v.r)).toBe(true);
|
|
782
|
+
expect(Number.isInteger(v.g)).toBe(true);
|
|
783
|
+
expect(Number.isInteger(v.b)).toBe(true);
|
|
784
|
+
}
|
|
785
|
+
});
|
|
786
|
+
|
|
787
|
+
it("the status bar is the surface (the definitional answer)", () => {
|
|
788
|
+
for (const fills of [DARK_FILLS, LIGHT_FILLS]) {
|
|
789
|
+
const p = deriveSemanticPalette(fills);
|
|
790
|
+
expect(p.statusBarBackground).toEqual(fills.surface);
|
|
791
|
+
// A COPY, not the caller's own object.
|
|
792
|
+
expect(p.statusBarBackground).not.toBe(fills.surface);
|
|
793
|
+
}
|
|
794
|
+
});
|
|
795
|
+
|
|
796
|
+
it("is deterministic and does not mutate its inputs", () => {
|
|
797
|
+
const fills = { ...DARK_FILLS, background: { ...DARK_FILLS.background } };
|
|
798
|
+
const before = JSON.stringify(fills);
|
|
799
|
+
const a = deriveSemanticPalette(fills);
|
|
800
|
+
const b = deriveSemanticPalette(fills);
|
|
801
|
+
expect(JSON.stringify(a)).toBe(JSON.stringify(b));
|
|
802
|
+
expect(JSON.stringify(fills)).toBe(before);
|
|
803
|
+
// Channels are integers in range.
|
|
804
|
+
for (const v of Object.values(a)) {
|
|
805
|
+
for (const ch of [v.r, v.g, v.b]) {
|
|
806
|
+
expect(Number.isInteger(ch)).toBe(true);
|
|
807
|
+
expect(ch).toBeGreaterThanOrEqual(0);
|
|
808
|
+
expect(ch).toBeLessThanOrEqual(255);
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
});
|
|
812
|
+
});
|