Site: contrast check counts element and ancestor opacity (R2.1)

This commit is contained in:
ZacharyZhang-NY committed 2026-10-09 01:08:06 -04:00
1 parent 9df123ff94
commit f5455ecd36
1 file changed
+10 -5
+10 -5
View File
@@ -79,14 +79,19 @@ for (const scheme of ["light", "dark"] as const) {
};
const ground = parse(getComputedStyle(document.documentElement).backgroundColor).rgb;
const page = lum(ground);
const shown = (c: string) => {
const { rgb, a } = parse(c);
return rgb.map((v, i) => v * a + ground[i] * (1 - a));
const alpha = (el: HTMLElement) => {
let a = parse(getComputedStyle(el).color).a;
for (let at: Element | null = el; at; at = at.parentElement) a *= Number(getComputedStyle(at).opacity);
return a;
};
const shown = (el: HTMLElement) => {
const a = alpha(el);
return parse(getComputedStyle(el).color).rgb.map((v, i) => v * a + ground[i] * (1 - a));
};
const ratios = [...document.querySelectorAll<HTMLElement>("h1, h2, h3, p, a, li, button")]
.filter((el) => el.textContent?.trim() && getComputedStyle(el).color !== "rgba(0, 0, 0, 0)")
.filter((el) => el.textContent?.trim() && alpha(el) > 0)
.map((el) => {
const text = lum(shown(getComputedStyle(el).color));
const text = lum(shown(el));
return { ratio: (Math.max(text, page) + 0.05) / (Math.min(text, page) + 0.05), text: el.textContent };
});
return ratios.sort((a, b) => a.ratio - b.ratio)[0];