forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

accent-text.test.tsx25 lines, 1.0 KiBHistoryRawDownload
1import { expect, test } from "vitest";
2import { accentTextLightness } from "@/stores/preferences-context";
3
4// Same formula as WCAG, kept local so the test does not share the code under test.
5const luminance = (h: number, s: number, l: number) => {
6 const a = (s / 100) * Math.min(l / 100, 1 - l / 100);
7 const f = (n: number) => {
8 const k = (n + h / 30) % 12;
9 const c = l / 100 - a * Math.max(-1, Math.min(k - 3, 9 - k, 1));
10 return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
11 };
12 return 0.2126 * f(0) + 0.7152 * f(8) + 0.0722 * f(4);
13};
14const contrastOnPage = (h: number, s: number, l: number) => (luminance(0, 0, 97) + 0.05) / (luminance(h, s, l) + 0.05);
15
16test("every hue gets a text shade that reads at AA on the page background", () => {
17 for (let hue = 0; hue < 360; hue += 15) {
18 const l = accentTextLightness(hue, 88, 40);
19 expect(contrastOnPage(hue, 88, l)).toBeGreaterThanOrEqual(4.5);
20 }
21});
22
23test("a hue that already passes keeps the button lightness", () => {
24 expect(accentTextLightness(240, 88, 40)).toBe(40);
25});