mirror of
https://github.com/db9979/tidefront.git
synced 2026-12-06 12:54:09 +01:00
- Zentrale Tokens (theme.css, hell/dunkel) und Abbildung aller Farbklassen per @theme inline; Zeichnungen, Diagramme und Kurstafel auf Tokens statt fester Hexwerte - Modus: Systemeinstellung als Standard, Einstellungen → Spiel → Darstellung (Wie System/Hell/Dunkel), ohne Flackern (Inline-Skript in index.html), theme-color passend - Dunkle Kopfleiste mit Füllstandsbalken (orange ab 85 %), Rang-Chip, Desktop mit Kopfleiste über volle Breite - Lage (Rangzeile, Alarmkarte, Fraunces-Titel, Umrissknopf), Gebäudeblatt (Jetzt/Stufe n, Kostenbalken, feste Bauleiste) - Schriften selbst ausgeliefert (+ Fraunces 700); Kartenfarben als lib/theme-map.ts (noch nicht verdrahtet) - Tests: Kontrast-Test der Tokens, E2E Moduswechsel und Bildschirmfotos hell/dunkel bei 390/1440 px
83 lines
4.1 KiB
TypeScript
83 lines
4.1 KiB
TypeScript
// Kontrastprüfung im Browser (Seekarte 2.0, hell/dunkel): jedes sichtbare Textelement gegen seinen tatsächlichen
|
|
// Hintergrund (nächster Vorfahre mit deckender Hintergrundfarbe). Grenze 4,5:1, große Schrift (≥ 24 px bzw.
|
|
// ≥ 18,66 px fett) 3:1. Ausgenommen: gesperrte/abgeblendete Elemente (opacity < 1 in der Kette, disabled),
|
|
// Text über Bildern/Verläufen und die Karte (Canvas). Liefert die Verstöße mit Text, Farben und Verhältnis.
|
|
import type { Page } from "@playwright/test";
|
|
|
|
export interface ContrastIssue { text: string; ratio: number; fg: string; bg: string; size: number }
|
|
|
|
export async function contrastIssues(page: Page, root = "body"): Promise<ContrastIssue[]> {
|
|
return page.evaluate((sel) => {
|
|
const parse = (c: string): [number, number, number, number] | null => {
|
|
const m = c.match(/rgba?\(([^)]+)\)/);
|
|
if (!m) {
|
|
const o = c.match(/color\(srgb ([^)]+)\)/);
|
|
if (!o) return null;
|
|
const p = o[1].split(/[\s/]+/).map(Number);
|
|
return [p[0] * 255, p[1] * 255, p[2] * 255, p[3] ?? 1];
|
|
}
|
|
const p = m[1].split(/[\s,/]+/).filter(Boolean).map(Number);
|
|
return [p[0], p[1], p[2], p[3] ?? 1];
|
|
};
|
|
const lum = ([r, g, b]: number[]) => {
|
|
const f = (v: number) => { const s = v / 255; return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4; };
|
|
return 0.2126 * f(r) + 0.7152 * f(g) + 0.0722 * f(b);
|
|
};
|
|
const mix = (top: number[], bottom: number[]) => {
|
|
const a = top[3];
|
|
return [top[0] * a + bottom[0] * (1 - a), top[1] * a + bottom[1] * (1 - a), top[2] * a + bottom[2] * (1 - a), 1];
|
|
};
|
|
/** Deckende Hintergrundfarbe unter dem Element (halbtransparente Schichten werden gemischt); null = Bild/Verlauf. */
|
|
const bgOf = (el: Element): number[] | null => {
|
|
const layers: number[][] = [];
|
|
for (let e: Element | null = el; e; e = e.parentElement) {
|
|
const cs = getComputedStyle(e);
|
|
if (cs.backgroundImage !== "none" && !cs.backgroundImage.startsWith("repeating-linear-gradient")) return null;
|
|
const c = parse(cs.backgroundColor);
|
|
if (c && c[3] > 0) {
|
|
layers.push(c);
|
|
if (c[3] >= 0.99) break;
|
|
}
|
|
}
|
|
let base = layers.pop() ?? [255, 255, 255, 1];
|
|
while (layers.length) base = mix(layers.pop()!, base);
|
|
return base;
|
|
};
|
|
const out: { text: string; ratio: number; fg: string; bg: string; size: number }[] = [];
|
|
const seen = new Set<string>();
|
|
const rootEl = document.querySelector(sel) ?? document.body;
|
|
const walker = document.createTreeWalker(rootEl, NodeFilter.SHOW_TEXT);
|
|
for (let n = walker.nextNode(); n; n = walker.nextNode()) {
|
|
const text = (n.textContent ?? "").trim();
|
|
if (!text || !/[\p{L}\p{N}]/u.test(text)) continue;
|
|
const el = n.parentElement;
|
|
if (!el || el.closest("canvas, svg, [aria-hidden=true], .sr-only, [data-contrast-skip]")) continue;
|
|
const r = el.getBoundingClientRect();
|
|
if (r.width === 0 || r.height === 0 || r.bottom < 0 || r.top > innerHeight) continue;
|
|
let dim = false;
|
|
for (let e: Element | null = el; e; e = e.parentElement) {
|
|
const cs = getComputedStyle(e);
|
|
if (Number(cs.opacity) < 1 || cs.visibility === "hidden" || (e as HTMLButtonElement).disabled || e.getAttribute("aria-disabled") === "true") { dim = true; break; }
|
|
}
|
|
if (dim) continue;
|
|
const cs = getComputedStyle(el);
|
|
const fg = parse(cs.color);
|
|
const bg = bgOf(el);
|
|
if (!fg || !bg) continue;
|
|
const fgMixed = fg[3] < 1 ? mix(fg, bg) : fg;
|
|
const l1 = lum(fgMixed), l2 = lum(bg);
|
|
const ratio = (Math.max(l1, l2) + 0.05) / (Math.min(l1, l2) + 0.05);
|
|
const size = parseFloat(cs.fontSize);
|
|
const bold = Number(cs.fontWeight) >= 700;
|
|
const need = size >= 24 || (bold && size >= 18.66) ? 3 : 4.5;
|
|
if (ratio + 0.01 < need) {
|
|
const key = `${text.slice(0, 30)}|${cs.color}|${bg.join(",")}`;
|
|
if (seen.has(key)) continue;
|
|
seen.add(key);
|
|
out.push({ text: text.slice(0, 40), ratio: Math.round(ratio * 100) / 100, fg: cs.color, bg: `rgb(${bg.slice(0, 3).map(Math.round).join(",")})`, size });
|
|
}
|
|
}
|
|
return out;
|
|
}, root);
|
|
}
|