-
-
Notifications
You must be signed in to change notification settings - Fork 3k
fix(colors): pick WCAG-higher-contrast text for author colors #7565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JohnMcLear
wants to merge
5
commits into
ether:develop
Choose a base branch
from
JohnMcLear:feat/wcag-author-color-7377
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+144
−2
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0109a69
feat(colors): clamp author backgrounds to WCAG 2.1 AA on render
JohnMcLear ce0c5c2
refactor(7377): simplify — just pick higher-contrast text, drop bg clamp
JohnMcLear 534428d
test(7377): assert relative-contrast invariant, not absolute AA
JohnMcLear 8602145
fix(7377): compare against rendered #222/#fff, not pure black/white
JohnMcLear fdda347
test(7377): pick unambiguous colibris test bgs
JohnMcLear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| 'use strict'; | ||
|
|
||
| const assert = require('assert').strict; | ||
| const {colorutils} = require('../../../static/js/colorutils'); | ||
|
|
||
| // Unit coverage for the WCAG helpers added in #7377. | ||
| // Kept backend-side so it runs in plain mocha without a browser; colorutils | ||
| // is pure and has no DOM deps. | ||
| describe(__filename, function () { | ||
| describe('relativeLuminance', function () { | ||
| it('returns 0 for pure black and 1 for pure white', function () { | ||
| assert.strictEqual(colorutils.relativeLuminance([0, 0, 0]), 0); | ||
| assert.strictEqual(colorutils.relativeLuminance([1, 1, 1]), 1); | ||
| }); | ||
|
|
||
| it('matches the WCAG 2.1 reference values (within 1e-3)', function () { | ||
| // Spot-check against published examples from the WCAG spec: | ||
| // #808080 (mid grey) → ~0.2159 | ||
| // #ff0000 (pure red) → ~0.2126 (red coefficient) | ||
| const grey = colorutils.relativeLuminance([0x80 / 255, 0x80 / 255, 0x80 / 255]); | ||
| const red = colorutils.relativeLuminance([1, 0, 0]); | ||
| assert.ok(Math.abs(grey - 0.2159) < 1e-3, `grey luminance: ${grey}`); | ||
| assert.ok(Math.abs(red - 0.2126) < 1e-3, `red luminance: ${red}`); | ||
| }); | ||
| }); | ||
|
|
||
| describe('contrastRatio', function () { | ||
| it('is 21 between black and white', function () { | ||
| assert.strictEqual(colorutils.contrastRatio([0, 0, 0], [1, 1, 1]), 21); | ||
| }); | ||
|
|
||
| it('is 1 between identical colors', function () { | ||
| assert.strictEqual(colorutils.contrastRatio([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]), 1); | ||
| }); | ||
|
|
||
| it('fails WCAG AA for mid-tone red on black (<4.5)', function () { | ||
| // #cc0000-ish — a common "author color" range. | ||
| const ratio = colorutils.contrastRatio([0.8, 0, 0], [0, 0, 0]); | ||
| assert.ok(ratio < 4.5, `expected <4.5, got ${ratio}`); | ||
| }); | ||
| }); | ||
|
|
||
| describe('ensureReadableBackground', function () { | ||
| it('leaves light enough backgrounds unchanged', function () { | ||
| // Pastel blue: already has adequate contrast with black text. | ||
| const light = '#aaccff'; | ||
| assert.strictEqual( | ||
| colorutils.ensureReadableBackground(light), light, | ||
| 'a bg that already satisfies 4.5:1 must be returned verbatim'); | ||
| }); | ||
|
|
||
| it('leaves very dark backgrounds unchanged (white text handles it)', function () { | ||
| // Near-black bg pairs with white text for contrast >> 4.5 — leave it. | ||
| const dark = '#111111'; | ||
| assert.strictEqual( | ||
| colorutils.ensureReadableBackground(dark), dark, | ||
| 'a bg that works with white text must be returned verbatim'); | ||
| }); | ||
|
|
||
| it('lightens mid-tone backgrounds until they pass WCAG AA with black text', function () { | ||
| // #cc0000 is the exact failure case from the issue screenshot — dark | ||
| // enough that black text is hard to read, but not dark enough for | ||
| // white text to hit 4.5:1 either. | ||
| const result = colorutils.ensureReadableBackground('#cc0000'); | ||
| assert.notStrictEqual(result, '#cc0000', 'expected the bg to change'); | ||
| const triple = colorutils.css2triple(result); | ||
| const ratio = colorutils.contrastRatio(triple, [0, 0, 0]); | ||
| assert.ok(ratio >= 4.5, `post-clamp contrast must be >=4.5, got ${ratio}`); | ||
| }); | ||
|
|
||
| it('respects a custom minContrast target', function () { | ||
| const result = colorutils.ensureReadableBackground('#888888', 7.0); | ||
| const triple = colorutils.css2triple(result); | ||
| const ratio = colorutils.contrastRatio(triple, [0, 0, 0]); | ||
| assert.ok(ratio >= 7.0, `AAA contrast target not met: ${ratio}`); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.