Created
September 20, 2026 18:05
-
-
Save fbritoferreira/3670acde8ecd8df8838953fb393f2c7b to your computer and use it in GitHub Desktop.
Painted-pixel WCAG contrast check for Playwright: resolves oklch and alpha through a 1px canvas. From fbritoferreira.com/blog/axe-needs-review-is-a-to-do-list-not-a-verdict/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import type { Page } from "@playwright/test"; | |
| export interface ContrastFailure { | |
| selector: string; | |
| text: string; | |
| ratio: number; | |
| required: number; | |
| fontSize: number; | |
| } | |
| export interface ContrastExclusion { | |
| selector: string; | |
| reason: string; | |
| added: string; | |
| } | |
| export function collectContrastFailures( | |
| page: Page, | |
| exclusions: ContrastExclusion[] = [], | |
| ): Promise<ContrastFailure[]> { | |
| const excludedSelectors = exclusions.map((e) => e.selector); | |
| return page.evaluate((excluded: string[]) => { | |
| const canvas = document.createElement("canvas"); | |
| canvas.width = canvas.height = 1; | |
| const ctx = canvas.getContext("2d", { willReadFrequently: true }); | |
| if (!ctx) return []; | |
| const paint = ( | |
| css: string, | |
| over?: string, | |
| ): [number, number, number, number] => { | |
| ctx.clearRect(0, 0, 1, 1); | |
| if (over) { | |
| ctx.fillStyle = over; | |
| ctx.fillRect(0, 0, 1, 1); | |
| } | |
| ctx.fillStyle = css; | |
| ctx.fillRect(0, 0, 1, 1); | |
| const d = ctx.getImageData(0, 0, 1, 1).data; | |
| return [d[0], d[1], d[2], d[3] / 255]; | |
| }; | |
| const luminance = ([r, g, b]: number[]): number => { | |
| const channel = (c: number) => { | |
| const s = c / 255; | |
| return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4; | |
| }; | |
| return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b); | |
| }; | |
| const opaqueBackground = (start: Element): string => { | |
| let el: Element | null = start; | |
| while (el) { | |
| const css = getComputedStyle(el).backgroundColor; | |
| if (paint(css)[3] > 0.99) return css; | |
| el = el.parentElement; | |
| } | |
| return "rgb(255, 255, 255)"; | |
| }; | |
| const cssPath = (el: Element): string => { | |
| const parts: string[] = []; | |
| let node: Element | null = el; | |
| while (node && node !== document.body && parts.length < 4) { | |
| const tag = node.tagName.toLowerCase(); | |
| const cls = [...node.classList].slice(0, 2).join("."); | |
| parts.unshift(cls ? `${tag}.${cls}` : tag); | |
| node = node.parentElement; | |
| } | |
| return parts.join(" > "); | |
| }; | |
| const failures: ContrastFailure[] = []; | |
| for (const el of document.querySelectorAll("body *")) { | |
| if (el.children.length > 0) continue; | |
| const text = (el.textContent ?? "").trim(); | |
| if (text.length < 2) continue; | |
| if (!(el instanceof HTMLElement) || el.offsetParent === null) continue; | |
| if (excluded.some((selector) => el.closest(selector))) continue; | |
| const style = getComputedStyle(el); | |
| if (style.visibility === "hidden") continue; | |
| const bgCss = opaqueBackground(el); | |
| const bg = paint(bgCss); | |
| const fg = paint(style.color, bgCss); | |
| const l1 = luminance(fg); | |
| const l2 = luminance(bg); | |
| const ratio = (Math.max(l1, l2) + 0.05) / (Math.min(l1, l2) + 0.05); | |
| const fontSize = parseFloat(style.fontSize); | |
| const bold = parseInt(style.fontWeight, 10) >= 700; | |
| const large = fontSize >= 24 || (fontSize >= 18.66 && bold); | |
| const required = large ? 3 : 4.5; | |
| if (ratio < required) { | |
| failures.push({ | |
| selector: cssPath(el), | |
| text: text.slice(0, 40), | |
| ratio: Math.round(ratio * 100) / 100, | |
| required, | |
| fontSize, | |
| }); | |
| } | |
| } | |
| return failures; | |
| }, excludedSelectors); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment