fix: enforce public contrast and audit rank errors

This commit is contained in:
Simo committed 2026-07-11 22:06:45 +02:00
1 parent e1381c0f40
commit b695a33ead
12 files changed
+184 -29

No files matched your search

+35
View File
@@ -1,5 +1,40 @@
type Rgb = readonly [number, number, number];
export type PublicThemeColors = Record<string, string>;
export interface ContrastPair { name: string; foreground: string; background: string }
export function derivePublicForegrounds(c: PublicThemeColors) {
const value = (key: string, fallback: string) => c[key] ?? fallback;
const background = value("color_background", "#f8fafc");
const surface = value("color_surface", "#ffffff");
const dropdown = value("color_dropdown", surface);
const navbar = value("color_navbar", surface);
const primary = value("color_primary", "#f59e0b");
const accent = value("color_accent", "#10b981");
const make = (name: string, preferred: string, bg: string): ContrastPair => ({
name, foreground: readableColor(preferred, [bg]), background: bg,
});
const pairs = [
make("body", value("color_text", "#0f172a"), background),
make("surface", value("color_text", "#0f172a"), surface),
make("muted", value("color_text_muted", "#64748b"), surface),
make("link", value("link_color", primary), surface),
make("link-hover", value("link_hover_color", primary), surface),
make("navbar", value("color_navbar_text", "#1e293b"), navbar),
make("dropdown", value("color_text", "#0f172a"), dropdown),
make("primary", value("button_text_color", "#1e293b"), primary),
make("accent", "#ffffff", accent),
make("button-primary", value("button_text_color", "#1e293b"), value("button_primary_color", primary)),
make("button-secondary", value("button_secondary_text_color", "#ffffff"), value("button_secondary_color", "#22c55e")),
make("button-danger", value("button_danger_text_color", "#ffffff"), value("button_danger_color", "#ef4444")),
make("button-outline", value("button_outline_text_color", "#1a1a2e"), surface),
make("pill", value("color_text", "#0f172a"), surface),
make("badge", value("button_text_color", "#1e293b"), primary),
make("overlay", "#ffffff", "#111827"),
];
return { pairs, byName: Object.fromEntries(pairs.map((pair) => [pair.name, pair.foreground])) };
}
export function contrastRatio(foreground: string, background: string): number | null {
const fg = parseHex(foreground);
const bg = parseHex(background);