8000 fix: do not round contrast up to the WCAG threshold by jangtrinh · Pull Request #462 · jangtrinh/design-os · GitHub
[go: up one dir, main page]

Skip to content

fix: do not round contrast up to the WCAG threshold - #462

Open
jangtrinh wants to merge 1 commit into
mainfrom
cursor/contrast-threshold-rounding-bb72
Open

jangtrinh wants to merge 1 commit into
mainfrom
cursor/contrast-threshold-rounding-bb72

Conversation

@jangtrinh
Copy link
Copy Markdown
Owner

Summary

pairFor in src/core/ds-a11y.ts rounded the contrast ratio to two decimals and then compared that rounded number with 4.5. A pair at about 4.499:1 printed as 4.50 and was reported as passing AA for normal text. WCAG 2.2 Understanding SC 1.4.3 treats 3:1 and 4.5:1 as thresholds and says the computed ratio is not rounded when it is compared (4.499:1 does not meet 4.5:1).

The same round-then-decide path was in the other contrast decisions, not only pairFor:

  • classifyContrast itself was already a raw comparison. Callers were handing it a pre-rounded ratio (ui color contrast, ui color scale text and JSON, including an inline copy of the bands in the scale table).
  • Rendered contrast (checkComputedContrast) rounded before >= required, so the same 4.499:1 pair skipped the finding. The 3:1 large-text floor had the same hole.
  • DESIGN.md accessibility audit rounded before the 3:1 / 4.5:1 split.
  • Persona foreground picking rounded before >= 4.5 on purpose, so it would agree with the audit. It now uses the same unrounded decision, so it will not select a pair the audit then fails.

Pass/fail goes through measureContrast, which classifies the unrounded ratio. Two-decimal display is kept, and it is not an input to the decision. When half-up rounding would cross 3:1, 4.5:1, or 7:1 and the true ratio is below that threshold, the displayed value stays below it (4.499 displays as 4.49, not 4.50).

Test

tests/ds-a11y.test.ts checks a real pair, #5A7C87 on #FFFFFF (about 4.499:1, which half-up rounding turns into 4.50), and #5A7E73 on #FFFFFF (just over 4.5:1):

  • the sub-4.5 pair does not pass normal-text AA (passesNormalText === false, level AA-large, included in failures)
  • the pair at or above 4.5 does pass (passesNormalText === true, level AA)

The same boundary is covered for measureContrast, rendered contrast, the DESIGN.md audit, and ui color contrast.

Gates

npm run typecheck, npm run lint, and npm run build passed. The contrast tests passed. A full npm test before the build reported missing-dist/cli.js failures; those passed after npm run build. Two other local failures are outside this change (ui gflow parallel exit codes, and a CDP snapshot assertion on <img id="c" attribute order).

Open in Web Open in Cursor 

pairFor rounded to two decimals before the 4.5:1 check, so a pair at
about 4.499:1 passed AA. Pass/fail now uses the unrounded ratio on every
contrast decision; display rounding cannot cross 3:1, 4.5:1, or 7:1.

Co-authored-by: Jang Trịnh <trinhnguyengiang@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0