|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { readableTextColorOn } from "./badge-contrast"; |
| 3 | + |
| 4 | +describe("readableTextColorOn", () => { |
| 5 | + test("dark accent gets white text", () => { |
| 6 | + // A typical dark accent (deep blue/purple) should read as white. |
| 7 | + expect(readableTextColorOn({ r: 0.1, g: 0.1, b: 0.3 })).toBe("#ffffff"); |
| 8 | + expect(readableTextColorOn({ r: 0, g: 0, b: 0 })).toBe("#ffffff"); |
| 9 | + }); |
| 10 | + |
| 11 | + test("light accent gets black text", () => { |
| 12 | + // Light/pastel accents should read as black. |
| 13 | + expect(readableTextColorOn({ r: 0.9, g: 0.9, b: 0.7 })).toBe("#000000"); |
| 14 | + expect(readableTextColorOn({ r: 1, g: 1, b: 1 })).toBe("#000000"); |
| 15 | + }); |
| 16 | + |
| 17 | + test("pure green is treated as light (high luma weight)", () => { |
| 18 | + // Green dominates perceived brightness, so a saturated green badge needs |
| 19 | + // dark text. |
| 20 | + expect(readableTextColorOn({ r: 0, g: 1, b: 0 })).toBe("#000000"); |
| 21 | + }); |
| 22 | + |
| 23 | + test("pure blue is treated as dark (low luma weight)", () => { |
| 24 | + // Blue contributes little to perceived brightness, so a saturated blue |
| 25 | + // badge needs light text. |
| 26 | + expect(readableTextColorOn({ r: 0, g: 0, b: 1 })).toBe("#ffffff"); |
| 27 | + }); |
| 28 | + |
| 29 | + test("does not depend on the (possibly transparent) background alpha", () => { |
| 30 | + // The helper only reads r/g/b — the regression in #186 was using a |
| 31 | + // background color whose alpha could be 0. Two accents with identical |
| 32 | + // rgb resolve identically regardless of any alpha the caller might pass. |
| 33 | + const a = readableTextColorOn({ r: 0.2, g: 0.2, b: 0.2 }); |
| 34 | + const b = readableTextColorOn({ r: 0.2, g: 0.2, b: 0.2 }); |
| 35 | + expect(a).toBe(b); |
| 36 | + expect(a).toBe("#ffffff"); |
| 37 | + }); |
| 38 | +}); |
0 commit comments