mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-05 14:19:56 -05:00
review: measure what can be measured, and say what is not
Acting on the review of #621. No bugs were found; these are the four suggestions and two nits. The real one: measurement accepted only hex. `relativeLuminance` returned 0 for anything else, which is black's luminance and indistinguishable from it -- so a token written `rgb(92 79 168)` measured as pure black, the "best" foreground was chosen against a colour the theme does not contain, and the wrong hex was then frozen into an inline style where CSS could no longer correct it. Silent, and worse than not measuring. `parseColor` now reads `rgb()`/`rgba()` in both the comma and space forms, percentages, and `#rgba`/`#rrggbbaa`; `isMeasurable` lets a caller tell "unreadable" from "black"; and navColorsFrom falls back to the tokens when the background or every candidate is unreadable. Unreadable candidates are filtered rather than the whole measurement abandoned, so one oddly written token does not cost the other two. This matters because the header claimed custom YAML themes get correct foregrounds for free, and a hand-written palette is exactly where a non-hex value will first appear. The Cypress inverse hardcoded night's `--btn-text` as `#0a0000`. The unit tests parse tokens.css precisely so a palette edit cannot leave a stale claim behind, and that literal was the one place the rule was broken. It reads the token from the theme now, asserts it resolved, and additionally pins that red still takes `--btn-text` while brown no longer does. Documented rather than changed: the warning indicators still pick their colour from the `conflictingColors` name list. It is the same defect as the one fixed here and measurably worse -- `--danger` and `--warning` are 1.0-1.8:1 against the bar in every theme, light and dark included, because a red icon on a red bar is a red icon on a red bar. The fix is not a better colour: severity has to stay distinguishable from the bar's text as well as from itself, and a monochrome theme has no third brightness, so it has to stop being a hue. That is a redesign, and the note now sits beside the residue in navColors.ts rather than only in a PR description. The gallery draws the indicators as the bar draws them when nothing is wrong; it does not stage a fake overheating Pi to exhibit a fault nobody has agreed how to fix. Also documented: re-measurement is triggered by `data-theme`, so a future custom-theme editor patching custom properties in place would leave the bar on the values it last measured. There is no event for "a custom property was assigned" to observe instead, so the note says what such an editor must do. Nits: "Eleven" in a comment where the assertion locks ten (and a stale 2.91:1 beside it, now 3.27:1), and a missing space in `const wrolpiIcon =<img`. Six new unit cases, planted: dropping the fallback reddens two. 1121 jest / 62 suites, 196 in ui-layout.cy.js, clean npm run build.
This commit is contained in:
parent
8aa9725feb
commit
5fdaf6868a
5 changed files with 203 additions and 21 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import React from 'react';
|
||||
import {bestForeground, contrastRatio} from './contrast';
|
||||
import {bestForeground, contrastRatio, isMeasurable} from './contrast';
|
||||
|
||||
|
||||
/*
|
||||
|
|
@ -23,8 +23,31 @@ import {bestForeground, contrastRatio} from './contrast';
|
|||
* night's "white" is #ff8a8a, a bright red, and amber's is #ffc875.
|
||||
*
|
||||
* Measuring at runtime rather than shipping a paired `--on-red`/`--on-olive` token for every
|
||||
* colour also means the custom YAML themes still ahead of us get this for free: a theme
|
||||
* supplies a palette, and the correct foreground for each entry falls out of it.
|
||||
* colour also means a theme only has to supply a palette: the correct foreground for each
|
||||
* entry falls out of it, with nothing extra to declare and nothing to keep in step.
|
||||
*
|
||||
* What is NOT measured, and should be: the warning indicators. NavBar still picks their
|
||||
* colour from `conflictingColors`, a hardcoded list of navbar names that swaps `yellow`/`red`
|
||||
* for `null`/`black` on the five bars those hues would blend into. It is the same defect
|
||||
* this replaced -- a name list standing in for a measurement -- and it is worse than the one
|
||||
* fixed here rather than better: `--danger` and `--warning` measure 1.0-1.8:1 against the bar
|
||||
* in EVERY theme, light and dark included, because a red icon on a red bar is a red icon on a
|
||||
* red bar. It is left alone deliberately, because the fix is not a better colour. Those
|
||||
* icons have to stay distinguishable from each other AND from the bar's own text, and in a
|
||||
* monochrome theme there is no third brightness to spend, so severity has to stop being a
|
||||
* hue -- a filled badge, or a shape. That is a redesign of how severity reads, not a
|
||||
* substitution, and it is not this change.
|
||||
*
|
||||
* /theme-sample draws the indicators as they are, taking the measured bar colour, which is
|
||||
* what the bar does when nothing is wrong. It does not stage a fake overheating Pi to
|
||||
* exhibit a fault nobody has agreed how to fix.
|
||||
*
|
||||
* Two limits on the measuring that IS done here, both of which the custom YAML themes will
|
||||
* meet before anything else does. A token has to be a colour these measurements can read -- hex or `rgb()`; anything
|
||||
* else falls back to CSS rather than being measured (see navColorsFrom). And re-measurement
|
||||
* is triggered by `data-theme` changing, which is how a theme is applied today; a future
|
||||
* editor that patches custom properties in place without touching the attribute would leave
|
||||
* the bar on the values it last measured (see useNavColors).
|
||||
*/
|
||||
|
||||
/**
|
||||
|
|
@ -73,17 +96,28 @@ export function navColorsFrom(read: (name: string) => string, navColor: string):
|
|||
*/
|
||||
const candidates = [read('--btn-text'), read('--black'), read('--white')].filter(Boolean);
|
||||
|
||||
if (!background || candidates.length === 0) {
|
||||
const measurable = candidates.filter(isMeasurable);
|
||||
|
||||
if (!isMeasurable(background) || measurable.length === 0) {
|
||||
/*
|
||||
* No stylesheet to read: jsdom, or a very early render. Fall back to the tokens
|
||||
* themselves so the bar is still painted by CSS and still changes with the theme --
|
||||
* this is the pre-measurement behaviour, and it is only ever wrong about the
|
||||
* foreground.
|
||||
* Nothing readable to measure, so measure nothing and let CSS do its job.
|
||||
*
|
||||
* Two cases reach here. The ordinary one is that there is no stylesheet yet -- jsdom,
|
||||
* or a very early render -- and the reader returns empty strings. The other is a
|
||||
* token this cannot parse: a hand-written theme writing `rgb(90 79 168)` or
|
||||
* `color-mix(...)` rather than a hex. That one used to be silent and worse than
|
||||
* useless: an unparseable colour measured as luminance zero, indistinguishable from
|
||||
* black, so the bar picked its foreground against a colour the theme does not contain
|
||||
* and then froze the wrong hex into an inline style, where CSS could not correct it.
|
||||
*
|
||||
* Falling back to the tokens themselves is the pre-measurement behaviour: the bar is
|
||||
* painted by CSS, follows the theme, and is only ever wrong about the foreground --
|
||||
* which is a great deal better than being confidently wrong about both.
|
||||
*/
|
||||
return {background: `var(--${name})`, color: 'var(--btn-text)', ratio: null};
|
||||
}
|
||||
|
||||
const color = bestForeground(background, candidates) as string;
|
||||
const color = bestForeground(background, measurable) as string;
|
||||
return {background, color, ratio: contrastRatio(color, background)};
|
||||
}
|
||||
|
||||
|
|
@ -126,6 +160,13 @@ export function navBarStyle(colors: NavColors): React.CSSProperties {
|
|||
*
|
||||
* The attribute is also stamped by the inline script in index.html before React loads, and
|
||||
* that is a change no context can report at all.
|
||||
*
|
||||
* This assumes token VALUES only change when `data-theme` does, which holds for every way a
|
||||
* theme is applied today. It will not hold for a custom-theme editor that rewrites custom
|
||||
* properties in place: the measured pair is a snapshot of hexes, so the bar would keep the
|
||||
* old one until the theme name or the chosen colour changed. When that editor exists it
|
||||
* should either re-stamp the attribute or announce the change for this to observe -- there
|
||||
* is no event for "a custom property was assigned" to watch instead.
|
||||
*/
|
||||
export function useNavColors(navColor: string): NavColors {
|
||||
const [colors, setColors] = React.useState<NavColors>(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue