Custom accent and background colours carry a vividness, with background tint backed off until text stays readable
Browse files at this commitfb9a4266d5d48f4779a7aa120680c04ef527e3ccParent: a651055- feat
- custom accent and background colours carry a vividness, with background tint backed off until text stays readable
3 changed files +190 -71
- crates/forge-web/src/accent.rs +44 -17
- crates/forge-web/src/custom_colour.rs +122 -44
- crates/forge-web/src/pages/assets.rs +24 -10
crates/forge-web/src/accent.rsmodified+44 -17
| @@ -61,15 +61,39 @@ pub fn base_ids() -> impl Iterator<Item = &'static str> { | |||
| 61 | BASES.iter().map(|b| b.id) | 61 | BASES.iter().map(|b| b.id) |
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | -/// The hue N of a `hue-N` id: an integer `0..=359` with no leading zeros (except `hue-0`). Any | 64 | +/// The hue and vividness of a custom id. `hue-H` (legacy) takes `default_vivid`; `hue-H-vV` takes V. |
| 65 | -/// other id is None. | 65 | +/// H is `0..=359` and V is `0..=100`, both plain decimal numbers with no sign, spaces or leading |
| 66 | +/// zeros (except `0` itself). Any other id is None. | ||
| 67 | +pub fn custom_of(id: &str, default_vivid: u8) -> Option<(u16, u8)> { | ||
| 68 | + let rest = id.strip_prefix("hue-")?; | ||
| 69 | + match rest.split_once("-v") { | ||
| 70 | + Some((hue, vivid)) => Some(( | ||
| 71 | + canonical_number(hue, 359)?, | ||
| 72 | + canonical_number(vivid, 100)? as u8, | ||
| 73 | + )), | ||
| 74 | + None => Some((canonical_number(rest, 359)?, default_vivid)), | ||
| 75 | + } | ||
| 76 | +} | ||
| 77 | + | ||
| 78 | +/// The stored id of a custom accent or base in the `hue-H-vV` form, which `custom_of` reads back. | ||
| 79 | +#[allow(dead_code)] // The settings picker writes its custom ids with this. | ||
| 80 | +pub fn custom_id(hue: u16, vivid: u8) -> String { | ||
| 81 | + format!("hue-{hue}-v{vivid}") | ||
| 82 | +} | ||
| 83 | + | ||
| 84 | +/// The hue N of a legacy or `hue-H-vV` id, whatever its vividness. Any other id is None. | ||
| 66 | pub fn hue_of(id: &str) -> Option<u16> { | 85 | pub fn hue_of(id: &str) -> Option<u16> { |
| 67 | - let digits = id.strip_prefix("hue-")?; | 86 | + custom_of(id, 50).map(|(hue, _)| hue) |
| 87 | +} | ||
| 88 | + | ||
| 89 | +/// A decimal number of at most three digits, no sign, no leading zeros (except `0`), and at most | ||
| 90 | +/// `max`. | ||
| 91 | +fn canonical_number(digits: &str, max: u16) -> Option<u16> { | ||
| 68 | let canonical = digits == "0" || (!digits.is_empty() && !digits.starts_with('0')); | 92 | let canonical = digits == "0" || (!digits.is_empty() && !digits.starts_with('0')); |
| 69 | if !canonical || digits.len() > 3 || !digits.bytes().all(|b| b.is_ascii_digit()) { | 93 | if !canonical || digits.len() > 3 || !digits.bytes().all(|b| b.is_ascii_digit()) { |
| 70 | return None; | 94 | return None; |
| 71 | } | 95 | } |
| 72 | - digits.parse::<u16>().ok().filter(|hue| *hue < 360) | 96 | + digits.parse::<u16>().ok().filter(|n| *n <= max) |
| 73 | } | 97 | } |
| 74 | 98 | ||
| 75 | /// The OKLCH hue, in degrees, of a preset accent's light colour. None for an id that is not a | 99 | /// The OKLCH hue, in degrees, of a preset accent's light colour. None for an id that is not a |
| @@ -96,8 +120,8 @@ pub fn preset_base_hue(id: &str) -> Option<u16> { | |||
| 96 | /// gets the accent built for its hue; an id forge does not offer gets the default's. | 120 | /// gets the accent built for its hue; an id forge does not offer gets the default's. |
| 97 | #[allow(dead_code)] // The settings picker shows its chips with this. | 121 | #[allow(dead_code)] // The settings picker shows its chips with this. |
| 98 | pub fn swatch(id: &str, dark: bool) -> String { | 122 | pub fn swatch(id: &str, dark: bool) -> String { |
| 99 | - match hue_of(id) { | 123 | + match custom_of(id, 50) { |
| 100 | - Some(hue) => custom_colour::accent_tokens(hue, dark).accent, | 124 | + Some((hue, vivid)) => custom_colour::accent_tokens(hue, vivid, dark).accent, |
| 101 | None => { | 125 | None => { |
| 102 | let palette = resolve(id); | 126 | let palette = resolve(id); |
| 103 | let tokens = if dark { &palette.dark } else { &palette.light }; | 127 | let tokens = if dark { &palette.dark } else { &palette.light }; |
| @@ -110,9 +134,9 @@ pub fn swatch(id: &str, dark: bool) -> String { | |||
| 110 | /// the base built for its hue; an id forge does not offer gets the default's. | 134 | /// the base built for its hue; an id forge does not offer gets the default's. |
| 111 | #[allow(dead_code)] // The settings picker shows its chips with this. | 135 | #[allow(dead_code)] // The settings picker shows its chips with this. |
| 112 | pub fn base_swatch(id: &str, dark: bool) -> (String, String) { | 136 | pub fn base_swatch(id: &str, dark: bool) -> (String, String) { |
| 113 | - match hue_of(id) { | 137 | + match custom_of(id, 30) { |
| 114 | - Some(hue) => { | 138 | + Some((hue, vivid)) => { |
| 115 | - let tokens = custom_colour::base_tokens(hue, dark); | 139 | + let tokens = custom_colour::base_tokens(hue, vivid, dark); |
| 116 | (tokens.bg, tokens.text) | 140 | (tokens.bg, tokens.text) |
| 117 | } | 141 | } |
| 118 | None => { | 142 | None => { |
| @@ -221,8 +245,8 @@ mod tests { | |||
| 221 | let hues = (0..360u16).map(|hue| { | 245 | let hues = (0..360u16).map(|hue| { |
| 222 | ( | 246 | ( |
| 223 | format!("hue-{hue}"), | 247 | format!("hue-{hue}"), |
| 224 | - accent_tokens(hue, false), | 248 | + accent_tokens(hue, 50, false), |
| 225 | - accent_tokens(hue, true), | 249 | + accent_tokens(hue, 50, true), |
| 226 | ) | 250 | ) |
| 227 | }); | 251 | }); |
| 228 | presets.chain(hues).collect() | 252 | presets.chain(hues).collect() |
| @@ -236,8 +260,8 @@ mod tests { | |||
| 236 | let hues = (0..360u16).map(|hue| { | 260 | let hues = (0..360u16).map(|hue| { |
| 237 | ( | 261 | ( |
| 238 | format!("hue-{hue}"), | 262 | format!("hue-{hue}"), |
| 239 | - base_tokens(hue, false), | 263 | + base_tokens(hue, 30, false), |
| 240 | - base_tokens(hue, true), | 264 | + base_tokens(hue, 30, true), |
| 241 | ) | 265 | ) |
| 242 | }); | 266 | }); |
| 243 | presets.chain(hues).collect() | 267 | presets.chain(hues).collect() |
| @@ -361,14 +385,17 @@ mod tests { | |||
| 361 | fn swatches_show_the_accent_and_the_base_for_presets_and_hues() { | 385 | fn swatches_show_the_accent_and_the_base_for_presets_and_hues() { |
| 362 | assert_eq!(swatch("sage", false), "#5E7D63"); | 386 | assert_eq!(swatch("sage", false), "#5E7D63"); |
| 363 | assert_eq!(swatch("sage", true), "#9DBEA2"); | 387 | assert_eq!(swatch("sage", true), "#9DBEA2"); |
| 364 | - assert_eq!(swatch("hue-215", false), accent_tokens(215, false).accent); | 388 | + assert_eq!( |
| 365 | - assert_eq!(swatch("hue-215", true), accent_tokens(215, true).accent); | 389 | + swatch("hue-215", false), |
| 390 | + accent_tokens(215, 50, false).accent | ||
| 391 | + ); | ||
| 392 | + assert_eq!(swatch("hue-215", true), accent_tokens(215, 50, true).accent); | ||
| 366 | assert_eq!(swatch("ember", false), swatch("sage", false)); | 393 | assert_eq!(swatch("ember", false), swatch("sage", false)); |
| 367 | let (bg, text) = base_swatch("fern", false); | 394 | let (bg, text) = base_swatch("fern", false); |
| 368 | assert_eq!((bg.as_str(), text.as_str()), ("#F6F8F5", "#1E2420")); | 395 | assert_eq!((bg.as_str(), text.as_str()), ("#F6F8F5", "#1E2420")); |
| 369 | let (bg, text) = base_swatch("hue-215", true); | 396 | let (bg, text) = base_swatch("hue-215", true); |
| 370 | - assert_eq!(bg, base_tokens(215, true).bg); | 397 | + assert_eq!(bg, base_tokens(215, 30, true).bg); |
| 371 | - assert_eq!(text, base_tokens(215, true).text); | 398 | + assert_eq!(text, base_tokens(215, 30, true).text); |
| 372 | } | 399 | } |
| 373 | 400 | ||
| 374 | #[test] | 401 | #[test] |
crates/forge-web/src/custom_colour.rsmodified+122 -44
| @@ -81,33 +81,37 @@ const NEUTRAL_CHROMA: f64 = 0.012; | |||
| 81 | /// Slack for float error when testing whether a colour is inside sRGB. | 81 | /// Slack for float error when testing whether a colour is inside sRGB. |
| 82 | const GAMUT_EPS: f64 = 1e-6; | 82 | const GAMUT_EPS: f64 = 1e-6; |
| 83 | 83 | ||
| 84 | -/// The accent colours for a hue, in one theme. Light: a mid accent with its ink darker than it. | 84 | +/// The chroma factor of an accent at vividness `vivid` (0..=100). The default, 50, gives exactly 1.0 |
| 85 | -/// Dark: a light accent with its ink lighter than it, on a dark soft tint. | 85 | +/// so the legacy `hue-N` colours stay byte-identical. |
| 86 | -pub fn accent_tokens(hue: u16, dark: bool) -> OwnedTokens { | 86 | +fn accent_factor(vivid: u8) -> f64 { |
| 87 | + if vivid == 50 { | ||
| 88 | + return 1.0; | ||
| 89 | + } | ||
| 90 | + (0.03 + 0.16 * f64::from(vivid) / 100.0) / 0.11 | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +/// The accent colours for a hue, in one theme, at a vividness (0..=100; 50 is the default). Light: | ||
| 94 | +/// a mid accent with its ink darker than it. Dark: a light accent with its ink lighter than it, on | ||
| 95 | +/// a dark soft tint. Every chroma is scaled by the vividness; the gamut clamp in `colour` holds the | ||
| 96 | +/// lightness and hue. | ||
| 97 | +pub fn accent_tokens(hue: u16, vivid: u8, dark: bool) -> OwnedTokens { | ||
| 87 | let h = f64::from(hue); | 98 | let h = f64::from(hue); |
| 99 | + let f = accent_factor(vivid); | ||
| 100 | + let c = |lightness: f64, chroma: f64| colour(lightness, chroma * f, h); | ||
| 88 | let (accent, ink, soft, hunk_bg) = if dark { | 101 | let (accent, ink, soft, hunk_bg) = if dark { |
| 89 | - ( | 102 | + (c(0.75, 0.11), c(0.84, 0.09), c(0.27, 0.04), c(0.22, 0.03)) |
| 90 | - colour(0.75, 0.11, h), | ||
| 91 | - colour(0.84, 0.09, h), | ||
| 92 | - colour(0.27, 0.04, h), | ||
| 93 | - colour(0.22, 0.03, h), | ||
| 94 | - ) | ||
| 95 | } else { | 103 | } else { |
| 96 | ( | 104 | ( |
| 97 | - colour(0.52, 0.12, h), | 105 | + c(0.52, 0.12), |
| 98 | - colour(0.46, 0.12, h), | 106 | + c(0.46, 0.12), |
| 99 | - colour(0.95, 0.025, h), | 107 | + c(0.95, 0.025), |
| 100 | - colour(0.975, 0.012, h), | 108 | + c(0.975, 0.012), |
| 101 | ) | 109 | ) |
| 102 | }; | 110 | }; |
| 103 | OwnedTokens { | 111 | OwnedTokens { |
| 104 | accent_text: text_on(&accent), | 112 | accent_text: text_on(&accent), |
| 105 | focus: if dark { accent.clone() } else { ink.clone() }, | 113 | focus: if dark { accent.clone() } else { ink.clone() }, |
| 106 | - hunk_text: if dark { | 114 | + hunk_text: if dark { accent.clone() } else { c(0.44, 0.10) }, |
| 107 | - accent.clone() | ||
| 108 | - } else { | ||
| 109 | - colour(0.44, 0.10, h) | ||
| 110 | - }, | ||
| 111 | public_bg: soft.clone(), | 115 | public_bg: soft.clone(), |
| 112 | public_text: ink.clone(), | 116 | public_text: ink.clone(), |
| 113 | note: ink.clone(), | 117 | note: ink.clone(), |
| @@ -118,11 +122,35 @@ pub fn accent_tokens(hue: u16, dark: bool) -> OwnedTokens { | |||
| 118 | } | 122 | } |
| 119 | } | 123 | } |
| 120 | 124 | ||
| 121 | -/// The neutral colours for a hue, in one theme. Each is a tint of the hue at `NEUTRAL_CHROMA`, at | 125 | +/// The neutral colours for a hue, in one theme, at a vividness (0..=100; 30 is the default). Each is |
| 122 | -/// the lightness the presets use for that role. | 126 | +/// a tint of the hue at `NEUTRAL_CHROMA` scaled by the vividness, at the lightness the presets use |
| 123 | -pub fn base_tokens(hue: u16, dark: bool) -> OwnedBaseTokens { | 127 | +/// for that role. Vividness 0 is a grey. If the text, surface or muted colour reads below WCAG 4.5:1, |
| 128 | +/// the chroma steps down by 0.8, at most ten times, until it passes. | ||
| 129 | +pub fn base_tokens(hue: u16, vivid: u8, dark: bool) -> OwnedBaseTokens { | ||
| 130 | + let mut factor = f64::from(vivid) / 30.0; | ||
| 131 | + let mut tokens = base_tokens_at(hue, factor, dark); | ||
| 132 | + for _ in 0..10 { | ||
| 133 | + if base_is_readable(&tokens) { | ||
| 134 | + break; | ||
| 135 | + } | ||
| 136 | + factor *= 0.8; | ||
| 137 | + tokens = base_tokens_at(hue, factor, dark); | ||
| 138 | + } | ||
| 139 | + tokens | ||
| 140 | +} | ||
| 141 | + | ||
| 142 | +/// True when the text, the surface and the muted colour all read at 4.5:1 or more on the background. | ||
| 143 | +fn base_is_readable(tokens: &OwnedBaseTokens) -> bool { | ||
| 144 | + const MIN: f64 = 4.5; | ||
| 145 | + contrast_ratio(&tokens.text, &tokens.bg) >= MIN | ||
| 146 | + && contrast_ratio(&tokens.text, &tokens.surface) >= MIN | ||
| 147 | + && contrast_ratio(&tokens.muted, &tokens.bg) >= MIN | ||
| 148 | +} | ||
| 149 | + | ||
| 150 | +/// The neutral colours of a base at one chroma factor (1.0 is the default vividness). | ||
| 151 | +fn base_tokens_at(hue: u16, factor: f64, dark: bool) -> OwnedBaseTokens { | ||
| 124 | let h = f64::from(hue); | 152 | let h = f64::from(hue); |
| 125 | - let n = |l: f64| colour(l, NEUTRAL_CHROMA, h); | 153 | + let n = |l: f64| colour(l, NEUTRAL_CHROMA * factor, h); |
| 126 | if dark { | 154 | if dark { |
| 127 | let surface_2 = n(0.26); | 155 | let surface_2 = n(0.26); |
| 128 | let text_2 = n(0.82); | 156 | let text_2 = n(0.82); |
| @@ -182,31 +210,49 @@ pub fn contrast_ratio(a: &str, b: &str) -> f64 { | |||
| 182 | (x.max(y) + 0.05) / (x.min(y) + 0.05) | 210 | (x.max(y) + 0.05) / (x.min(y) + 0.05) |
| 183 | } | 211 | } |
| 184 | 212 | ||
| 185 | -/// The rules for the custom ids of one stylesheet: each argument is the hue of an accent or base | 213 | +/// One custom choice of `stylesheet`: the id as stored, its hue and its vividness. The id is the |
| 186 | -/// id (`None` for a preset, which `build.rs` already styles). They mirror `build.rs`: the light | 214 | +/// stored string, so the selector matches the `<html>` attribute (a legacy `hue-N` stays `hue-N`). |
| 187 | -/// tokens go on the light attribute, the dark tokens on the dark attribute in both dark states. | 215 | +pub type CustomSlot<'a> = Option<(&'a str, u16, u8)>; |
| 216 | + | ||
| 217 | +/// The rules for the custom ids of one stylesheet: each slot is an accent or base id with its hue | ||
| 218 | +/// and vividness (`None` for a preset, which `build.rs` already styles). They mirror `build.rs`: the | ||
| 219 | +/// light tokens go on the light attribute, the dark tokens on the dark attribute in both dark states. | ||
| 188 | pub fn stylesheet( | 220 | pub fn stylesheet( |
| 189 | - accent_light: Option<u16>, | 221 | + accent_light: CustomSlot, |
| 190 | - accent_dark: Option<u16>, | 222 | + accent_dark: CustomSlot, |
| 191 | - base_light: Option<u16>, | 223 | + base_light: CustomSlot, |
| 192 | - base_dark: Option<u16>, | 224 | + base_dark: CustomSlot, |
| 193 | ) -> String { | 225 | ) -> String { |
| 194 | let mut css = String::new(); | 226 | let mut css = String::new(); |
| 195 | - if let Some(hue) = accent_light { | 227 | + if let Some((id, hue, vivid)) = accent_light { |
| 196 | - let selector = format!(":root[data-accent-light=\"hue-{hue}\"]"); | 228 | + let selector = format!(":root[data-accent-light=\"{id}\"]"); |
| 197 | - rule(&mut css, "", &selector, &accent_tokens(hue, false).decls()); | 229 | + rule( |
| 230 | + &mut css, | ||
| 231 | + "", | ||
| 232 | + &selector, | ||
| 233 | + &accent_tokens(hue, vivid, false).decls(), | ||
| 234 | + ); | ||
| 198 | } | 235 | } |
| 199 | - if let Some(hue) = accent_dark { | 236 | + if let Some((id, hue, vivid)) = accent_dark { |
| 200 | - let selector = format!(":root[data-accent-dark=\"hue-{hue}\"]"); | 237 | + let selector = format!(":root[data-accent-dark=\"{id}\"]"); |
| 201 | - dark_rules(&mut css, &selector, &accent_tokens(hue, true).decls()); | 238 | + dark_rules( |
| 239 | + &mut css, | ||
| 240 | + &selector, | ||
| 241 | + &accent_tokens(hue, vivid, true).decls(), | ||
| 242 | + ); | ||
| 202 | } | 243 | } |
| 203 | - if let Some(hue) = base_light { | 244 | + if let Some((id, hue, vivid)) = base_light { |
| 204 | - let selector = format!(":root[data-base-light=\"hue-{hue}\"]"); | 245 | + let selector = format!(":root[data-base-light=\"{id}\"]"); |
| 205 | - rule(&mut css, "", &selector, &base_tokens(hue, false).decls()); | 246 | + rule( |
| 247 | + &mut css, | ||
| 248 | + "", | ||
| 249 | + &selector, | ||
| 250 | + &base_tokens(hue, vivid, false).decls(), | ||
| 251 | + ); | ||
| 206 | } | 252 | } |
| 207 | - if let Some(hue) = base_dark { | 253 | + if let Some((id, hue, vivid)) = base_dark { |
| 208 | - let selector = format!(":root[data-base-dark=\"hue-{hue}\"]"); | 254 | + let selector = format!(":root[data-base-dark=\"{id}\"]"); |
| 209 | - dark_rules(&mut css, &selector, &base_tokens(hue, true).decls()); | 255 | + dark_rules(&mut css, &selector, &base_tokens(hue, vivid, true).decls()); |
| 210 | } | 256 | } |
| 211 | css | 257 | css |
| 212 | } | 258 | } |
| @@ -356,14 +402,14 @@ mod tests { | |||
| 356 | fn every_hue_gives_hex_colours_with_the_right_token_names_and_keyed_rules() { | 402 | fn every_hue_gives_hex_colours_with_the_right_token_names_and_keyed_rules() { |
| 357 | for hue in 0..360 { | 403 | for hue in 0..360 { |
| 358 | for dark in [false, true] { | 404 | for dark in [false, true] { |
| 359 | - let accent = accent_tokens(hue, dark); | 405 | + let accent = accent_tokens(hue, 50, dark); |
| 360 | let names: Vec<&str> = accent.decls().iter().map(|(n, _)| *n).collect(); | 406 | let names: Vec<&str> = accent.decls().iter().map(|(n, _)| *n).collect(); |
| 361 | let expected: Vec<&str> = TOKEN_VARS.iter().map(|t| t.name).collect(); | 407 | let expected: Vec<&str> = TOKEN_VARS.iter().map(|t| t.name).collect(); |
| 362 | assert_eq!(names, expected); | 408 | assert_eq!(names, expected); |
| 363 | for (name, value) in accent.decls() { | 409 | for (name, value) in accent.decls() { |
| 364 | assert!(is_hex_colour(value), "{hue} {dark} {name}: {value}"); | 410 | assert!(is_hex_colour(value), "{hue} {dark} {name}: {value}"); |
| 365 | } | 411 | } |
| 366 | - let base = base_tokens(hue, dark); | 412 | + let base = base_tokens(hue, 30, dark); |
| 367 | let names: Vec<&str> = base.decls().iter().map(|(n, _)| *n).collect(); | 413 | let names: Vec<&str> = base.decls().iter().map(|(n, _)| *n).collect(); |
| 368 | let expected: Vec<&str> = BASE_TOKEN_VARS.iter().map(|t| t.name).collect(); | 414 | let expected: Vec<&str> = BASE_TOKEN_VARS.iter().map(|t| t.name).collect(); |
| 369 | assert_eq!(names, expected); | 415 | assert_eq!(names, expected); |
| @@ -372,7 +418,12 @@ mod tests { | |||
| 372 | } | 418 | } |
| 373 | } | 419 | } |
| 374 | } | 420 | } |
| 375 | - let css = stylesheet(Some(215), Some(215), None, Some(7)); | 421 | + let css = stylesheet( |
| 422 | + Some(("hue-215", 215, 50)), | ||
| 423 | + Some(("hue-215", 215, 50)), | ||
| 424 | + None, | ||
| 425 | + Some(("hue-7", 7, 30)), | ||
| 426 | + ); | ||
| 376 | assert!( | 427 | assert!( |
| 377 | css.contains(":root[data-accent-light=\"hue-215\"] {\n --accent: #"), | 428 | css.contains(":root[data-accent-light=\"hue-215\"] {\n --accent: #"), |
| 378 | "{css}" | 429 | "{css}" |
| @@ -394,4 +445,31 @@ mod tests { | |||
| 394 | assert!(!css.contains("data-base-light"), "{css}"); | 445 | assert!(!css.contains("data-base-light"), "{css}"); |
| 395 | assert!(stylesheet(None, None, None, None).is_empty()); | 446 | assert!(stylesheet(None, None, None, None).is_empty()); |
| 396 | } | 447 | } |
| 448 | + | ||
| 449 | + #[test] | ||
| 450 | + fn legacy_and_explicit_default_vividness_give_the_same_accent() { | ||
| 451 | + let legacy = crate::accent::custom_of("hue-215", 50).unwrap(); | ||
| 452 | + let explicit = crate::accent::custom_of("hue-215-v50", 50).unwrap(); | ||
| 453 | + assert_eq!(legacy, explicit); | ||
| 454 | + for dark in [false, true] { | ||
| 455 | + let a = accent_tokens(legacy.0, legacy.1, dark); | ||
| 456 | + let b = accent_tokens(explicit.0, explicit.1, dark); | ||
| 457 | + assert_eq!(a.decls(), b.decls()); | ||
| 458 | + } | ||
| 459 | + } | ||
| 460 | + | ||
| 461 | + #[test] | ||
| 462 | + fn full_vividness_bases_keep_text_readable() { | ||
| 463 | + for hue in (0..360).step_by(30) { | ||
| 464 | + for dark in [false, true] { | ||
| 465 | + let base = base_tokens(hue, 100, dark); | ||
| 466 | + assert!( | ||
| 467 | + contrast_ratio(&base.text, &base.bg) >= 4.5, | ||
| 468 | + "{hue} {dark}: {} on {}", | ||
| 469 | + base.text, | ||
| 470 | + base.bg | ||
| 471 | + ); | ||
| 472 | + } | ||
| 473 | + } | ||
| 474 | + } | ||
| 397 | } | 475 | } |
crates/forge-web/src/pages/assets.rsmodified+24 -10
| @@ -91,20 +91,26 @@ async fn highlight_css_route() -> Result<(StatusCode, HeaderMap, &'static str)> | |||
| 91 | /// so the response is immutable like the others. | 91 | /// so the response is immutable like the others. |
| 92 | #[route(GET "/theme/custom.css")] | 92 | #[route(GET "/theme/custom.css")] |
| 93 | async fn custom_css_route(cx: &Cx) -> Result<(StatusCode, HeaderMap, String)> { | 93 | async fn custom_css_route(cx: &Cx) -> Result<(StatusCode, HeaderMap, String)> { |
| 94 | - let mut hues: [Option<u16>; 4] = [None; 4]; | 94 | + // Each slot: the id as given, its hue and its vividness. A legacy `hue-N` gets the default. |
| 95 | + let mut slots: [Option<(String, u16, u8)>; 4] = Default::default(); | ||
| 95 | if let Some(query) = uri(cx).query() { | 96 | if let Some(query) = uri(cx).query() { |
| 96 | for (key, value) in form_urlencoded::parse(query.as_bytes()) { | 97 | for (key, value) in form_urlencoded::parse(query.as_bytes()) { |
| 97 | - let slot = match key.as_ref() { | 98 | + let (slot, default_vivid) = match key.as_ref() { |
| 98 | - "al" => 0, | 99 | + "al" => (0, 50), |
| 99 | - "ad" => 1, | 100 | + "ad" => (1, 50), |
| 100 | - "bl" => 2, | 101 | + "bl" => (2, 30), |
| 101 | - "bd" => 3, | 102 | + "bd" => (3, 30), |
| 102 | _ => continue, | 103 | _ => continue, |
| 103 | }; | 104 | }; |
| 104 | - hues[slot] = accent::hue_of(&value); | 105 | + if let Some((hue, vivid)) = accent::custom_of(&value, default_vivid) { |
| 106 | + slots[slot] = Some((value.into_owned(), hue, vivid)); | ||
| 107 | + } | ||
| 105 | } | 108 | } |
| 106 | } | 109 | } |
| 107 | - let [accent_light, accent_dark, base_light, base_dark] = hues; | 110 | + let [accent_light, accent_dark, base_light, base_dark] = slots.each_ref().map(|slot| { |
| 111 | + slot.as_ref() | ||
| 112 | + .map(|(id, hue, vivid)| (id.as_str(), *hue, *vivid)) | ||
| 113 | + }); | ||
| 108 | let css = custom_colour::stylesheet(accent_light, accent_dark, base_light, base_dark); | 114 | let css = custom_colour::stylesheet(accent_light, accent_dark, base_light, base_dark); |
| 109 | let mut headers = HeaderMap::new(); | 115 | let mut headers = HeaderMap::new(); |
| 110 | headers.insert( | 116 | headers.insert( |
| @@ -342,8 +348,16 @@ mod tests { | |||
| 342 | // Each bundle is immutable under its own type. Its body holds the marker, when one is given. | 348 | // Each bundle is immutable under its own type. Its body holds the marker, when one is given. |
| 343 | for (uri, kind, marker) in [ | 349 | for (uri, kind, marker) in [ |
| 344 | ("/assets/main.css", "text/css; charset=utf-8", ""), | 350 | ("/assets/main.css", "text/css; charset=utf-8", ""), |
| 345 | - (crate::views::NAV_SCRIPT_HREF, "text/javascript; charset=utf-8", "__forgeSoft"), | 351 | + ( |
| 346 | - (crate::views::highlight_href(), "text/css; charset=utf-8", ""), | 352 | + crate::views::NAV_SCRIPT_HREF, |
| 353 | + "text/javascript; charset=utf-8", | ||
| 354 | + "__forgeSoft", | ||
| 355 | + ), | ||
| 356 | + ( | ||
| 357 | + crate::views::highlight_href(), | ||
| 358 | + "text/css; charset=utf-8", | ||
| 359 | + "", | ||
| 360 | + ), | ||
| 347 | ] { | 361 | ] { |
| 348 | let req = Request::builder() | 362 | let req = Request::builder() |
| 349 | .method("GET") | 363 | .method("GET") |