Skip to content

Commit 348db6c

Browse files
fix(providers): choose readable badge foregrounds
1 parent b856ad9 commit 348db6c

2 files changed

Lines changed: 11 additions & 18 deletions

File tree

‎packages/client-runtime/src/state/providerInstanceDisplay.test.ts‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,12 @@ describe("normalizeProviderAccentColor", () => {
9191
});
9292

9393
describe("providerAccentForegroundColor", () => {
94-
it("uses dark text on light accents", () => {
95-
expect(providerAccentForegroundColor("#b5f23d")).toBe("#000000");
96-
expect(providerAccentForegroundColor("#FFFFFF")).toBe("#000000");
97-
});
98-
99-
it("uses light text on dark accents", () => {
100-
expect(providerAccentForegroundColor("#2563eb")).toBe("#ffffff");
101-
expect(providerAccentForegroundColor("#000000")).toBe("#ffffff");
102-
});
103-
104-
it("keeps light text when the accent is invalid", () => {
105-
expect(providerAccentForegroundColor("blue")).toBe("#ffffff");
94+
it.each([
95+
["#b5f23d", "#000000"],
96+
["#2563eb", "#ffffff"],
97+
["#777777", "#000000"],
98+
])("chooses the higher WCAG contrast foreground for %s", (accent, foreground) => {
99+
expect(providerAccentForegroundColor(accent)).toBe(foreground);
106100
});
107101
});
108102

‎packages/client-runtime/src/state/providerInstanceDisplay.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,16 @@ export function normalizeProviderAccentColor(value: string | undefined): string
4242
return /^#[0-9a-fA-F]{6}$/u.test(trimmed) ? trimmed : undefined;
4343
}
4444

45-
export function providerAccentForegroundColor(accentColor: string): "#000000" | "#ffffff" {
46-
const hex = normalizeProviderAccentColor(accentColor);
47-
if (!hex) return "#ffffff";
45+
export function providerAccentForegroundColor(accentColor: string) {
4846
const linearChannel = (offset: number) => {
49-
const value = Number.parseInt(hex.slice(offset, offset + 2), 16) / 255;
47+
const value = Number.parseInt(accentColor.slice(offset, offset + 2), 16) / 255;
5048
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
5149
};
5250
const luminance =
5351
0.2126 * linearChannel(1) + 0.7152 * linearChannel(3) + 0.0722 * linearChannel(5);
54-
// WCAG contrast: black wins when (L + 0.05) / 0.05 exceeds 1.05 / (L + 0.05).
55-
return (luminance + 0.05) ** 2 > 0.0525 ? "#000000" : "#ffffff";
52+
const blackContrast = (luminance + 0.05) / 0.05;
53+
const whiteContrast = 1.05 / (luminance + 0.05);
54+
return blackContrast >= whiteContrast ? "#000000" : "#ffffff";
5655
}
5756

5857
export function shouldShowInstanceBadge(

0 commit comments

Comments
 (0)