| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use crate::accent_palette::{BASE_DEFAULT, BASES, Base, DEFAULT, PALETTES, Palette}; |
| 7 | use crate::custom_colour; |
| 8 | |
| 9 | |
| 10 | |
| 11 | pub fn resolve(stored: &str) -> &'static Palette { |
| 12 | PALETTES |
| 13 | .iter() |
| 14 | .find(|p| p.id == stored) |
| 15 | .unwrap_or_else(default_palette) |
| 16 | } |
| 17 | |
| 18 | |
| 19 | |
| 20 | pub fn accent_id(stored: &str) -> &str { |
| 21 | if is_offered(stored) { stored } else { DEFAULT } |
| 22 | } |
| 23 | |
| 24 | |
| 25 | pub fn is_offered(id: &str) -> bool { |
| 26 | PALETTES.iter().any(|p| p.id == id) || hue_of(id).is_some() |
| 27 | } |
| 28 | |
| 29 | |
| 30 | |
| 31 | pub fn ids() -> impl Iterator<Item = &'static str> { |
| 32 | PALETTES.iter().map(|p| p.id) |
| 33 | } |
| 34 | |
| 35 | |
| 36 | |
| 37 | pub fn resolve_base(stored: &str) -> &'static Base { |
| 38 | BASES |
| 39 | .iter() |
| 40 | .find(|b| b.id == stored) |
| 41 | .unwrap_or_else(default_base) |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | pub fn base_id(stored: &str) -> &str { |
| 47 | if is_offered_base(stored) { |
| 48 | stored |
| 49 | } else { |
| 50 | BASE_DEFAULT |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | |
| 55 | pub fn is_offered_base(id: &str) -> bool { |
| 56 | BASES.iter().any(|b| b.id == id) || hue_of(id).is_some() |
| 57 | } |
| 58 | |
| 59 | |
| 60 | pub fn base_ids() -> impl Iterator<Item = &'static str> { |
| 61 | BASES.iter().map(|b| b.id) |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 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 | |
| 79 | pub fn custom_id(hue: u16, vivid: u8) -> String { |
| 80 | format!("hue-{hue}-v{vivid}") |
| 81 | } |
| 82 | |
| 83 | |
| 84 | pub fn hue_of(id: &str) -> Option<u16> { |
| 85 | custom_of(id, 50).map(|(hue, _)| hue) |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | fn canonical_number(digits: &str, max: u16) -> Option<u16> { |
| 91 | let canonical = digits == "0" || (!digits.is_empty() && !digits.starts_with('0')); |
| 92 | if !canonical || digits.len() > 3 || !digits.bytes().all(|b| b.is_ascii_digit()) { |
| 93 | return None; |
| 94 | } |
| 95 | digits.parse::<u16>().ok().filter(|n| *n <= max) |
| 96 | } |
| 97 | |
| 98 | |
| 99 | |
| 100 | #[allow(dead_code)] |
| 101 | pub fn preset_accent_hue(id: &str) -> Option<u16> { |
| 102 | PALETTES |
| 103 | .iter() |
| 104 | .find(|p| p.id == id) |
| 105 | .map(|p| custom_colour::hex_hue(p.light.accent)) |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | #[allow(dead_code)] |
| 111 | pub fn preset_base_hue(id: &str) -> Option<u16> { |
| 112 | BASES |
| 113 | .iter() |
| 114 | .find(|b| b.id == id) |
| 115 | .map(|b| custom_colour::hex_hue(b.light.bg)) |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | #[allow(dead_code)] |
| 121 | pub fn swatch(id: &str, dark: bool) -> String { |
| 122 | match custom_of(id, 50) { |
| 123 | Some((hue, vivid)) => custom_colour::accent_tokens(hue, vivid, dark).accent, |
| 124 | None => { |
| 125 | let palette = resolve(id); |
| 126 | let tokens = if dark { &palette.dark } else { &palette.light }; |
| 127 | tokens.accent.to_owned() |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | |
| 133 | |
| 134 | #[allow(dead_code)] |
| 135 | pub fn base_swatch(id: &str, dark: bool) -> (String, String) { |
| 136 | match custom_of(id, 30) { |
| 137 | Some((hue, vivid)) => { |
| 138 | let tokens = custom_colour::base_tokens(hue, vivid, dark); |
| 139 | (tokens.bg, tokens.text) |
| 140 | } |
| 141 | None => { |
| 142 | let base = resolve_base(id); |
| 143 | let tokens = if dark { &base.dark } else { &base.light }; |
| 144 | (tokens.bg.to_owned(), tokens.text.to_owned()) |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | pub fn custom_css_href( |
| 153 | accent_light: &str, |
| 154 | accent_dark: &str, |
| 155 | base_light: &str, |
| 156 | base_dark: &str, |
| 157 | ) -> Option<String> { |
| 158 | let any_hue = [accent_light, accent_dark, base_light, base_dark] |
| 159 | .iter() |
| 160 | .any(|id| hue_of(id).is_some()); |
| 161 | any_hue.then(|| { |
| 162 | format!( |
| 163 | "/theme/custom.css?al={accent_light}&ad={accent_dark}&bl={base_light}&bd={base_dark}" |
| 164 | ) |
| 165 | }) |
| 166 | } |
| 167 | |
| 168 | fn default_palette() -> &'static Palette { |
| 169 | PALETTES |
| 170 | .iter() |
| 171 | .find(|p| p.id == DEFAULT) |
| 172 | .expect("the default accent is one of the palettes") |
| 173 | } |
| 174 | |
| 175 | fn default_base() -> &'static Base { |
| 176 | BASES |
| 177 | .iter() |
| 178 | .find(|b| b.id == BASE_DEFAULT) |
| 179 | .expect("the default base is one of the bases") |
| 180 | } |
| 181 | |
| 182 | #[cfg(test)] |
| 183 | mod tests { |
| 184 | use std::collections::HashSet; |
| 185 | |
| 186 | use super::*; |
| 187 | use crate::accent_palette::{BASE_TOKEN_VARS, BaseTokens, TOKEN_VARS, Tokens}; |
| 188 | use crate::custom_colour::{ |
| 189 | OwnedBaseTokens, OwnedTokens, accent_tokens, base_tokens, contrast_ratio, |
| 190 | }; |
| 191 | |
| 192 | |
| 193 | |
| 194 | type AccentSet = (String, OwnedTokens, OwnedTokens); |
| 195 | type BaseSet = (String, OwnedBaseTokens, OwnedBaseTokens); |
| 196 | |
| 197 | fn is_hex_colour(value: &str) -> bool { |
| 198 | value.len() == 7 |
| 199 | && value.starts_with('#') |
| 200 | && value[1..].bytes().all(|b| b.is_ascii_hexdigit()) |
| 201 | } |
| 202 | |
| 203 | fn owned_tokens(t: &Tokens) -> OwnedTokens { |
| 204 | OwnedTokens { |
| 205 | accent: t.accent.to_owned(), |
| 206 | accent_ink: t.accent_ink.to_owned(), |
| 207 | accent_soft: t.accent_soft.to_owned(), |
| 208 | accent_text: t.accent_text.to_owned(), |
| 209 | focus: t.focus.to_owned(), |
| 210 | public_bg: t.public_bg.to_owned(), |
| 211 | public_text: t.public_text.to_owned(), |
| 212 | note: t.note.to_owned(), |
| 213 | hunk_bg: t.hunk_bg.to_owned(), |
| 214 | hunk_text: t.hunk_text.to_owned(), |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | fn owned_base(t: &BaseTokens) -> OwnedBaseTokens { |
| 219 | OwnedBaseTokens { |
| 220 | bg: t.bg.to_owned(), |
| 221 | surface: t.surface.to_owned(), |
| 222 | surface_2: t.surface_2.to_owned(), |
| 223 | text: t.text.to_owned(), |
| 224 | text_2: t.text_2.to_owned(), |
| 225 | muted: t.muted.to_owned(), |
| 226 | border: t.border.to_owned(), |
| 227 | border_strong: t.border_strong.to_owned(), |
| 228 | code_bg: t.code_bg.to_owned(), |
| 229 | private_bg: t.private_bg.to_owned(), |
| 230 | private_text: t.private_text.to_owned(), |
| 231 | ln_text: t.ln_text.to_owned(), |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | |
| 236 | fn all_accents() -> Vec<AccentSet> { |
| 237 | let presets = PALETTES.iter().map(|p| { |
| 238 | ( |
| 239 | p.id.to_owned(), |
| 240 | owned_tokens(&p.light), |
| 241 | owned_tokens(&p.dark), |
| 242 | ) |
| 243 | }); |
| 244 | let hues = (0..360u16).map(|hue| { |
| 245 | ( |
| 246 | format!("hue-{hue}"), |
| 247 | accent_tokens(hue, 50, false), |
| 248 | accent_tokens(hue, 50, true), |
| 249 | ) |
| 250 | }); |
| 251 | presets.chain(hues).collect() |
| 252 | } |
| 253 | |
| 254 | |
| 255 | fn all_bases() -> Vec<BaseSet> { |
| 256 | let presets = BASES |
| 257 | .iter() |
| 258 | .map(|b| (b.id.to_owned(), owned_base(&b.light), owned_base(&b.dark))); |
| 259 | let hues = (0..360u16).map(|hue| { |
| 260 | ( |
| 261 | format!("hue-{hue}"), |
| 262 | base_tokens(hue, 30, false), |
| 263 | base_tokens(hue, 30, true), |
| 264 | ) |
| 265 | }); |
| 266 | presets.chain(hues).collect() |
| 267 | } |
| 268 | |
| 269 | #[test] |
| 270 | fn ids_are_slugs_unique_and_the_default_comes_first() { |
| 271 | assert_eq!(PALETTES[0].id, DEFAULT); |
| 272 | assert_eq!(DEFAULT, forge_core::store::ui_prefs::DEFAULT_ACCENT); |
| 273 | assert_eq!(BASES[0].id, BASE_DEFAULT); |
| 274 | assert_eq!(BASE_DEFAULT, forge_core::store::ui_prefs::DEFAULT_BASE); |
| 275 | let lists = [ |
| 276 | ("accent", PALETTES.iter().map(|p| p.id).collect::<Vec<_>>()), |
| 277 | ("base", BASES.iter().map(|b| b.id).collect::<Vec<_>>()), |
| 278 | ]; |
| 279 | for (kind, ids) in lists { |
| 280 | let mut seen = HashSet::new(); |
| 281 | for id in ids { |
| 282 | assert!( |
| 283 | forge_core::store::ui_prefs::valid_accent(id), |
| 284 | "{kind} {id} is not a slug" |
| 285 | ); |
| 286 | assert!(seen.insert(id), "{kind} {id} is listed twice"); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | #[test] |
| 292 | fn every_token_is_a_hex_colour() { |
| 293 | for p in PALETTES { |
| 294 | for token in TOKEN_VARS { |
| 295 | for t in [&p.light, &p.dark] { |
| 296 | assert!(is_hex_colour((token.value)(t)), "{} {}", p.id, token.name); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | for b in BASES { |
| 301 | for token in BASE_TOKEN_VARS { |
| 302 | for t in [&b.light, &b.dark] { |
| 303 | assert!(is_hex_colour((token.value)(t)), "{} {}", b.id, token.name); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | #[test] |
| 310 | fn the_presets_are_offered_in_picker_order_and_removed_ids_are_not() { |
| 311 | assert_eq!( |
| 312 | ids().collect::<Vec<_>>(), |
| 313 | ["sage", "ember", "ocean", "plum", "rose"] |
| 314 | ); |
| 315 | assert_eq!( |
| 316 | base_ids().collect::<Vec<_>>(), |
| 317 | ["ink", "anvil", "dusk", "heather", "blush"] |
| 318 | ); |
| 319 | for id in [ |
| 320 | "ochre", |
| 321 | "teal", |
| 322 | "Sage", |
| 323 | "sage ", |
| 324 | "terracotta", |
| 325 | "spark", |
| 326 | "gold", |
| 327 | ] { |
| 328 | assert!(!is_offered(id), "{id}"); |
| 329 | } |
| 330 | for id in [ |
| 331 | "stone", |
| 332 | "mist", |
| 333 | "fern", |
| 334 | "graphite", |
| 335 | "dune", |
| 336 | "slate-blue", |
| 337 | "spark", |
| 338 | ] { |
| 339 | assert!(!is_offered_base(id), "{id}"); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | #[test] |
| 344 | fn unknown_and_removed_ids_fall_back_to_the_default() { |
| 345 | assert_eq!(resolve("ocean").id, "ocean"); |
| 346 | assert_eq!(resolve("removed-palette").id, DEFAULT); |
| 347 | assert_eq!(resolve("").id, DEFAULT); |
| 348 | assert_eq!(accent_id("ocean"), "ocean"); |
| 349 | assert_eq!(accent_id("terracotta"), DEFAULT); |
| 350 | assert_eq!(accent_id(""), DEFAULT); |
| 351 | assert!(is_offered("plum")); |
| 352 | assert!(!is_offered("removed-palette")); |
| 353 | assert_eq!(resolve_base("ink").id, "ink"); |
| 354 | assert_eq!(resolve_base("removed-base").id, BASE_DEFAULT); |
| 355 | assert_eq!(base_id("mist"), BASE_DEFAULT); |
| 356 | assert_eq!(base_id("heather"), "heather"); |
| 357 | assert_eq!(base_id("graphite"), BASE_DEFAULT); |
| 358 | } |
| 359 | |
| 360 | #[test] |
| 361 | fn hue_ids_are_canonical_numbers_below_360() { |
| 362 | for (id, hue) in [ |
| 363 | ("hue-0", 0), |
| 364 | ("hue-7", 7), |
| 365 | ("hue-42", 42), |
| 366 | ("hue-215", 215), |
| 367 | ("hue-359", 359), |
| 368 | ] { |
| 369 | assert_eq!(hue_of(id), Some(hue), "{id}"); |
| 370 | assert!(is_offered(id), "{id}"); |
| 371 | assert!(is_offered_base(id), "{id}"); |
| 372 | assert_eq!(accent_id(id), id); |
| 373 | assert_eq!(base_id(id), id); |
| 374 | } |
| 375 | for id in [ |
| 376 | "hue-", "hue-360", "hue-01", "hue-00", "hue-1000", "hue--1", "hue-+5", "hue-5 ", |
| 377 | "Hue-5", "hue-5.0", "hue5", "hue_5", "hue-0x1", |
| 378 | ] { |
| 379 | assert_eq!(hue_of(id), None, "{id}"); |
| 380 | assert!(!is_offered(id), "{id}"); |
| 381 | assert!(!is_offered_base(id), "{id}"); |
| 382 | } |
| 383 | |
| 384 | assert_eq!(resolve("hue-215").id, DEFAULT); |
| 385 | assert_eq!(resolve_base("hue-215").id, BASE_DEFAULT); |
| 386 | } |
| 387 | |
| 388 | #[test] |
| 389 | fn preset_hues_come_from_the_colour() { |
| 390 | assert_eq!(preset_accent_hue("sage"), Some(149)); |
| 391 | assert_eq!(preset_accent_hue("ocean"), Some(240)); |
| 392 | assert_eq!(preset_accent_hue("plum"), Some(300)); |
| 393 | assert_eq!(preset_accent_hue("fern"), None); |
| 394 | assert_eq!(preset_accent_hue("hue-5"), None); |
| 395 | assert!(preset_base_hue("blush").is_some()); |
| 396 | assert!(preset_base_hue("dusk").is_some()); |
| 397 | assert_eq!(preset_base_hue("sage"), None); |
| 398 | } |
| 399 | |
| 400 | #[test] |
| 401 | fn swatches_show_the_accent_and_the_base_for_presets_and_hues() { |
| 402 | assert_eq!(swatch("sage", false), "#5E7D63"); |
| 403 | assert_eq!(swatch("sage", true), "#93A663"); |
| 404 | assert_eq!( |
| 405 | swatch("hue-215", false), |
| 406 | accent_tokens(215, 50, false).accent |
| 407 | ); |
| 408 | assert_eq!(swatch("hue-215", true), accent_tokens(215, 50, true).accent); |
| 409 | |
| 410 | assert_eq!(swatch("terracotta", false), swatch(DEFAULT, false)); |
| 411 | let (bg, text) = base_swatch("blush", false); |
| 412 | assert_eq!((bg.as_str(), text.as_str()), ("#F5EBEC", "#2B191C")); |
| 413 | let (bg, text) = base_swatch("hue-215", true); |
| 414 | assert_eq!(bg, base_tokens(215, 30, true).bg); |
| 415 | assert_eq!(text, base_tokens(215, 30, true).text); |
| 416 | } |
| 417 | |
| 418 | #[test] |
| 419 | fn the_custom_stylesheet_is_linked_only_when_a_hue_is_chosen() { |
| 420 | assert_eq!(custom_css_href("sage", "plum", "blush", "ink"), None); |
| 421 | assert_eq!( |
| 422 | custom_css_href("hue-215", "plum", "blush", "hue-7").as_deref(), |
| 423 | Some("/theme/custom.css?al=hue-215&ad=plum&bl=blush&bd=hue-7") |
| 424 | ); |
| 425 | assert_eq!( |
| 426 | custom_css_href("sage", "sage", "ink", "hue-0").as_deref(), |
| 427 | Some("/theme/custom.css?al=sage&ad=sage&bl=ink&bd=hue-0") |
| 428 | ); |
| 429 | } |
| 430 | |
| 431 | #[test] |
| 432 | fn every_accent_keeps_its_own_text_readable() { |
| 433 | for (id, light, dark) in all_accents() { |
| 434 | for (theme, t) in [("light", &light), ("dark", &dark)] { |
| 435 | let check = |what: &str, ratio: f64, min: f64| { |
| 436 | assert!(ratio >= min, "{id} {theme} {what}: {ratio:.2} < {min}"); |
| 437 | }; |
| 438 | check( |
| 439 | "accent_text on accent", |
| 440 | contrast_ratio(&t.accent_text, &t.accent), |
| 441 | 4.5, |
| 442 | ); |
| 443 | check( |
| 444 | "public_text on public_bg", |
| 445 | contrast_ratio(&t.public_text, &t.public_bg), |
| 446 | 4.5, |
| 447 | ); |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | #[test] |
| 453 | fn every_base_with_every_accent_meets_the_contrast_targets() { |
| 454 | let accents = all_accents(); |
| 455 | for (base_id, base_light, base_dark) in all_bases() { |
| 456 | for (accent_id, accent_light, accent_dark) in &accents { |
| 457 | let themes = [ |
| 458 | ("light", &base_light, accent_light), |
| 459 | ("dark", &base_dark, accent_dark), |
| 460 | ]; |
| 461 | for (theme, b, t) in themes { |
| 462 | let check = |what: &str, ratio: f64, min: f64| { |
| 463 | assert!( |
| 464 | ratio >= min, |
| 465 | "{base_id} with {accent_id} ({theme}) {what}: {ratio:.2} < {min}" |
| 466 | ); |
| 467 | }; |
| 468 | check("text on bg", contrast_ratio(&b.text, &b.bg), 7.0); |
| 469 | check("text on surface", contrast_ratio(&b.text, &b.surface), 7.0); |
| 470 | check("text_2 on bg", contrast_ratio(&b.text_2, &b.bg), 4.5); |
| 471 | check( |
| 472 | "text_2 on surface", |
| 473 | contrast_ratio(&b.text_2, &b.surface), |
| 474 | 4.5, |
| 475 | ); |
| 476 | check("muted on bg", contrast_ratio(&b.muted, &b.bg), 4.5); |
| 477 | check( |
| 478 | "muted on surface", |
| 479 | contrast_ratio(&b.muted, &b.surface), |
| 480 | 4.5, |
| 481 | ); |
| 482 | check( |
| 483 | "ln_text on code_bg", |
| 484 | contrast_ratio(&b.ln_text, &b.code_bg), |
| 485 | 4.5, |
| 486 | ); |
| 487 | check( |
| 488 | "private_text on private_bg", |
| 489 | contrast_ratio(&b.private_text, &b.private_bg), |
| 490 | 4.5, |
| 491 | ); |
| 492 | check( |
| 493 | "accent_ink on bg", |
| 494 | contrast_ratio(&t.accent_ink, &b.bg), |
| 495 | 4.5, |
| 496 | ); |
| 497 | check( |
| 498 | "accent_ink on surface", |
| 499 | contrast_ratio(&t.accent_ink, &b.surface), |
| 500 | 4.5, |
| 501 | ); |
| 502 | check( |
| 503 | "accent_ink on accent_soft", |
| 504 | contrast_ratio(&t.accent_ink, &t.accent_soft), |
| 505 | 4.5, |
| 506 | ); |
| 507 | check("focus on bg", contrast_ratio(&t.focus, &b.bg), 3.0); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |