Site: contrast check counts element and ancestor opacity (R2.1)
This commit is contained in:
1 file changed
+10
-5
+10
-5
@@ -79,14 +79,19 @@ for (const scheme of ["light", "dark"] as const) {
|
|||||||
};
|
};
|
||||||
const ground = parse(getComputedStyle(document.documentElement).backgroundColor).rgb;
|
const ground = parse(getComputedStyle(document.documentElement).backgroundColor).rgb;
|
||||||
const page = lum(ground);
|
const page = lum(ground);
|
||||||
const shown = (c: string) => {
|
const alpha = (el: HTMLElement) => {
|
||||||
const { rgb, a } = parse(c);
|
let a = parse(getComputedStyle(el).color).a;
|
||||||
return rgb.map((v, i) => v * a + ground[i] * (1 - 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")]
|
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) => {
|
.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 { 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];
|
return ratios.sort((a, b) => a.ratio - b.ratio)[0];
|
||||||
|
|||||||
Reference in new issue
Block a user