Open History to see the last commit
1import { converter } from "culori/fn";
2import { describe, expect, it } from "vite-plus/test";
3
4import "~/index.css";
5import { getStandardPaintedThemeColors } from "./themePalette";
6
7const toRgb = converter("rgb");
8
9function paintedChannels(variable: string, canvas: readonly number[] = [0, 0, 0]): number[] {
10 const probe = document.createElement("div");
11 probe.style.backgroundColor = `var(${variable})`;
12 document.body.append(probe);
13 try {
14 const color = toRgb(getComputedStyle(probe).backgroundColor)!;
15 const alpha = color.alpha ?? 1;
16 return [color.r, color.g, color.b].map(
17 (channel, index) => (channel * alpha + (canvas[index]! / 255) * (1 - alpha)) * 255,
18 );
19 } finally {
20 probe.remove();
21 }
22}
23
24describe("standard painted theme colors", () => {
25 it("match the chip surfaces index.css paints", () => {
26 const root = document.documentElement;
27 try {
28 for (const redesign of [false, true, false]) {
29 root.toggleAttribute("data-redesign", redesign);
30 for (const appearance of ["light", "dark"] as const) {
31 root.classList.toggle("dark", appearance === "dark");
32 const colors = getStandardPaintedThemeColors(appearance, redesign);
33 const canvas = paintedChannels("--background");
34 for (const [variable, role] of [
35 ["--background", "canvas"],
36 ["--card", "surface"],
37 ["--surface-raised", "surfaceRaised"],
38 ["--message-surface", "messageSurface"],
39 ] as const) {
40 const expected = toRgb(colors[role])!;
41 const painted = paintedChannels(variable, canvas);
42 [expected.r, expected.g, expected.b].forEach((channel, index) => {
43 expect(
44 Math.abs(channel * 255 - painted[index]!),
45 `${appearance} ${role}`,
46 ).toBeLessThan(1.5);
47 });
48 }
49 }
50 }
51 } finally {
52 root.removeAttribute("data-redesign");
53 root.classList.remove("dark");
54 }
55 });
56});