Repository navigation
fix(shared): keep HTML chart colors readable across themes #27346
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
fancyboi999
wants to merge
8
commits into
pingdotgg:main
Choose a base branch
from
fancyboi999:fix/26872-html-chart-contrast
base: main
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.
+112
−4
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ceddda4
fix(shared): keep HTML chart colors readable across themes
fancyboi999 6598517
test(shared): cover HTML graphic contrast mapping
fancyboi999 0e76517
Merge remote-tracking branch 'github/main' into maria/takeover-pr-27346
maria-rcks 9166353
test(shared): preserve readable HTML chart accents
maria-rcks 6b88c84
Merge remote-tracking branch 'github/main' into maria/takeover-pr-27346
maria-rcks 03fe127
chore: merge main and resolve shared lockfile conflict
fancyboi999 10feb8b
chore: merge main and refresh shared lockfile
fancyboi999 55b283c
test(shared): check chart contrast in both appearances
fancyboi999 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import "culori/css"; | ||
| import { clampChroma, converter, formatHex, wcagContrast } from "culori/fn"; | ||
|
|
||
| const toRgb = converter("rgb"); | ||
| const toOklch = converter("oklch"); | ||
|
|
||
| export function readableHtmlRenderColor(color: string, surface: string, minimum: number): string { | ||
| const foreground = toRgb(color); | ||
| const background = toRgb(surface); | ||
| // A translucent canvas needs the host's underlying surface to determine contrast. | ||
| if (!foreground || !background || (background.alpha ?? 1) < 1) return color; | ||
|
|
||
| const alpha = Math.max(0, Math.min(1, foreground.alpha ?? 1)); | ||
| const painted = { | ||
| mode: "rgb" as const, | ||
| r: foreground.r * alpha + background.r * (1 - alpha), | ||
| g: foreground.g * alpha + background.g * (1 - alpha), | ||
| b: foreground.b * alpha + background.b * (1 - alpha), | ||
| }; | ||
| if (wcagContrast(painted, background) >= minimum) return color; | ||
|
|
||
| const base = toOklch(painted); | ||
| const encode = (lightness: number) => | ||
| formatHex(clampChroma({ ...base, l: lightness }, "oklch", "rgb")); | ||
| const original = encode(base.l); | ||
| if (wcagContrast(original, background) >= minimum) return original; | ||
|
|
||
| const lighter = wcagContrast("#ffffff", background) >= wcagContrast("#000000", background); | ||
| let low = lighter ? base.l : 0; | ||
| let high = lighter ? 1 : base.l; | ||
| let result = encode(lighter ? 1 : 0); | ||
| for (let step = 0; step < 24; step += 1) { | ||
| const mid = (low + high) / 2; | ||
| const candidate = encode(mid); | ||
| // Measure the encoded sRGB paint so rounding cannot put it below the threshold. | ||
| if (wcagContrast(candidate, background) >= minimum) { | ||
| result = candidate; | ||
| if (lighter) high = mid; | ||
| else low = mid; | ||
| } else if (lighter) low = mid; | ||
| else high = mid; | ||
| } | ||
| return result; | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.