mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-03 05:09:58 -05:00
ui: two more labels were painting text with an inline colour, and shorthand hex
Both from review, and both correct. * The tag edit preview and the Flasher's chip badges still wrote the calculated text colour to `color`. That is the same defect the last commit fixed on the tag chip itself: an inline declaration no stylesheet rule can outrank, so night keeps the colour calculated for the fill it has just discarded and a bright fill leaves black text on near-black. The preview is the worse of the two -- it is the preview of the colour being chosen, so it was misrepresenting what the tag would look like at the moment the user picks it. It now shows the tag shape as well. Fixing the first site did not fix these, and the stylesheet comment explaining the night and amber override already named the Flasher. So there is a source guard now: no element carrying `wrolpi-label` may set `color` in its style object. Verified against a planted violation, which it reports by file. * `hexToRGBArray` rejected the shorthand `#fff`, which left the luminance at zero -- a near-white tag was read as black and handed light text. Reachable because tag colours are not only picked in the colour picker: they are stored in a config, and a config is a file a user edits by hand. Three tests were red first, including one asserting the shorthand and the six-digit form of the same colour agree.
This commit is contained in:
parent
d117d92a85
commit
672e7973f6
5 changed files with 115 additions and 7 deletions
|
|
@ -1554,16 +1554,26 @@ export function contrastRatio(a, b) {
|
|||
return (luminances[0] + 0.05) / (luminances[1] + 0.05);
|
||||
}
|
||||
|
||||
/**
|
||||
* A hex colour as [r, g, b], accepting both `#rrggbb` and the shorthand `#rgb`.
|
||||
*
|
||||
* The shorthand matters because tag colours are not only chosen in the colour picker: they
|
||||
* live in the database and in a config file, and a config is something a user edits by hand
|
||||
* -- configs are the source of truth in WROLPi. Rejecting `#fff` left the luminance at zero,
|
||||
* so a near-white tag was treated as black and handed light text.
|
||||
*/
|
||||
function hexToRGBArray(color) {
|
||||
if (color.length === 7) {
|
||||
color = color.slice(1);
|
||||
let hex = (typeof color === 'string' ? color : '').replace(/^#/, '');
|
||||
if (hex.length === 3) {
|
||||
// #abc means #aabbcc, not #0a0b0c.
|
||||
hex = hex.split('').map(digit => digit + digit).join('');
|
||||
}
|
||||
if (color.length !== 6) {
|
||||
if (!/^[0-9a-fA-F]{6}$/.test(hex)) {
|
||||
console.error('Invalid hex color: ' + color);
|
||||
return;
|
||||
}
|
||||
let rgb = [];
|
||||
for (let i = 0; i <= 2; i++) rgb[i] = parseInt(color.substr(i * 2, 2), 16);
|
||||
for (let i = 0; i <= 2; i++) rgb[i] = parseInt(hex.substr(i * 2, 2), 16);
|
||||
return rgb;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue