hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { expect, test } from "vitest"; |
| 2 | import { accentTextLightness } from "@/stores/preferences-context"; |
| 3 | |
| 4 | // Same formula as WCAG, kept local so the test does not share the code under test. |
| 5 | const 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 | }; |
| 14 | const contrastOnPage = (h: number, s: number, l: number) => (luminance(0, 0, 97) + 0.05) / (luminance(h, s, l) + 0.05); |
| 15 | |
| 16 | test("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 | |
| 23 | test("a hue that already passes keeps the button lightness", () => { |
| 24 | expect(accentTextLightness(240, 88, 40)).toBe(40); |
| 25 | }); |