use crate::accent_palette::{BASE_DEFAULT, BASES, Base, DEFAULT, PALETTES, Palette}; use crate::custom_colour; pub const ACCENT_VIVID: u8 = 50; pub const BASE_VIVID: u8 = 30; pub fn resolve(stored: &str) -> &'static Palette { PALETTES .iter() .find(|p| p.id == stored) .unwrap_or(&PALETTES[0]) } pub fn accent_id(stored: &str) -> &str { if is_offered(stored) { stored } else { DEFAULT } } pub fn is_offered(id: &str) -> bool { PALETTES.iter().any(|p| p.id == id) || hue_of(id).is_some() } pub fn ids() -> impl Iterator { PALETTES.iter().map(|p| p.id) } pub fn resolve_base(stored: &str) -> &'static Base { BASES.iter().find(|b| b.id == stored).unwrap_or(&BASES[0]) } pub fn base_id(stored: &str) -> &str { if is_offered_base(stored) { stored } else { BASE_DEFAULT } } pub fn is_offered_base(id: &str) -> bool { BASES.iter().any(|b| b.id == id) || hue_of(id).is_some() } pub fn base_ids() -> impl Iterator { BASES.iter().map(|b| b.id) } pub fn custom_of(id: &str, default_vivid: u8) -> Option<(u16, u8)> { let rest = id.strip_prefix("hue-")?; match rest.split_once("-v") { Some((hue, vivid)) => Some(( canonical_number(hue, 359)?, canonical_number(vivid, 100)? as u8, )), None => Some((canonical_number(rest, 359)?, default_vivid)), } } pub fn custom_id(hue: u16, vivid: u8) -> String { format!("hue-{hue}-v{vivid}") } pub fn hue_of(id: &str) -> Option { custom_of(id, ACCENT_VIVID).map(|(hue, _)| hue) } fn canonical_number(digits: &str, max: u16) -> Option { let canonical = digits == "0" || (!digits.is_empty() && !digits.starts_with('0')); if !canonical || digits.len() > 3 || !digits.bytes().all(|b| b.is_ascii_digit()) { return None; } digits.parse::().ok().filter(|n| *n <= max) } #[allow(dead_code)] pub fn preset_accent_hue(id: &str) -> Option { PALETTES .iter() .find(|p| p.id == id) .map(|p| custom_colour::hex_hue(p.light.accent)) } #[allow(dead_code)] pub fn preset_base_hue(id: &str) -> Option { BASES .iter() .find(|b| b.id == id) .map(|b| custom_colour::hex_hue(b.light.bg)) } pub fn custom_css_href( accent_light: &str, accent_dark: &str, base_light: &str, base_dark: &str, ) -> Option { let any_hue = [accent_light, accent_dark, base_light, base_dark] .iter() .any(|id| hue_of(id).is_some()); any_hue.then(|| { format!( "/theme/custom.css?al={accent_light}&ad={accent_dark}&bl={base_light}&bd={base_dark}" ) }) } #[cfg(test)] mod tests { use std::collections::HashSet; use super::*; use crate::accent_palette::{BASE_TOKEN_VARS, BaseTokens, TOKEN_VARS, Tokens}; use crate::custom_colour::{ OwnedBaseTokens, OwnedTokens, accent_tokens, base_tokens, contrast_ratio, }; type AccentSet = (String, OwnedTokens, OwnedTokens); type BaseSet = (String, OwnedBaseTokens, OwnedBaseTokens); fn is_hex_colour(value: &str) -> bool { value.len() == 7 && value.starts_with('#') && value[1..].bytes().all(|b| b.is_ascii_hexdigit()) } fn owned_tokens(t: &Tokens) -> OwnedTokens { OwnedTokens { accent: t.accent.to_owned(), accent_ink: t.accent_ink.to_owned(), accent_soft: t.accent_soft.to_owned(), accent_text: t.accent_text.to_owned(), focus: t.focus.to_owned(), public_bg: t.public_bg.to_owned(), public_text: t.public_text.to_owned(), note: t.note.to_owned(), hunk_bg: t.hunk_bg.to_owned(), hunk_text: t.hunk_text.to_owned(), } } fn owned_base(t: &BaseTokens) -> OwnedBaseTokens { OwnedBaseTokens { bg: t.bg.to_owned(), surface: t.surface.to_owned(), surface_2: t.surface_2.to_owned(), text: t.text.to_owned(), text_2: t.text_2.to_owned(), muted: t.muted.to_owned(), border: t.border.to_owned(), border_strong: t.border_strong.to_owned(), code_bg: t.code_bg.to_owned(), private_bg: t.private_bg.to_owned(), private_text: t.private_text.to_owned(), ln_text: t.ln_text.to_owned(), } } fn all_accents() -> Vec { let presets = PALETTES.iter().map(|p| { ( p.id.to_owned(), owned_tokens(&p.light), owned_tokens(&p.dark), ) }); let hues = (0..360u16).map(|hue| { ( format!("hue-{hue}"), accent_tokens(hue, ACCENT_VIVID, false), accent_tokens(hue, ACCENT_VIVID, true), ) }); presets.chain(hues).collect() } fn all_bases() -> Vec { let presets = BASES .iter() .map(|b| (b.id.to_owned(), owned_base(&b.light), owned_base(&b.dark))); let hues = (0..360u16).map(|hue| { ( format!("hue-{hue}"), base_tokens(hue, BASE_VIVID, false), base_tokens(hue, BASE_VIVID, true), ) }); presets.chain(hues).collect() } #[test] fn ids_are_slugs_unique_and_the_default_comes_first() { assert_eq!(PALETTES[0].id, DEFAULT); assert_eq!(DEFAULT, forge_core::store::ui_prefs::DEFAULT_ACCENT); assert_eq!(BASES[0].id, BASE_DEFAULT); assert_eq!(BASE_DEFAULT, forge_core::store::ui_prefs::DEFAULT_BASE); let lists = [ ("accent", PALETTES.iter().map(|p| p.id).collect::>()), ("base", BASES.iter().map(|b| b.id).collect::>()), ]; for (kind, ids) in lists { let mut seen = HashSet::new(); for id in ids { assert!( forge_core::store::ui_prefs::valid_slug(id), "{kind} {id} is not a slug" ); assert!(seen.insert(id), "{kind} {id} is listed twice"); } } } #[test] fn every_token_is_a_hex_colour() { for p in PALETTES { for token in TOKEN_VARS { for t in [&p.light, &p.dark] { assert!(is_hex_colour((token.value)(t)), "{} {}", p.id, token.name); } } } for b in BASES { for token in BASE_TOKEN_VARS { for t in [&b.light, &b.dark] { assert!(is_hex_colour((token.value)(t)), "{} {}", b.id, token.name); } } } } #[test] fn the_presets_are_offered_in_picker_order_and_removed_ids_are_not() { assert_eq!( ids().collect::>(), ["sage", "ember", "ocean", "plum", "rose"] ); assert_eq!( base_ids().collect::>(), ["ink", "anvil", "dusk", "heather", "blush"] ); for id in [ "ochre", "teal", "Sage", "sage ", "terracotta", "spark", "gold", ] { assert!(!is_offered(id), "{id}"); } for id in [ "stone", "mist", "fern", "graphite", "dune", "slate-blue", "spark", ] { assert!(!is_offered_base(id), "{id}"); } } #[test] fn unknown_and_removed_ids_fall_back_to_the_default() { assert_eq!(resolve("ocean").id, "ocean"); assert_eq!(resolve("removed-palette").id, DEFAULT); assert_eq!(resolve("").id, DEFAULT); assert_eq!(accent_id("ocean"), "ocean"); assert_eq!(accent_id("terracotta"), DEFAULT); assert_eq!(accent_id(""), DEFAULT); assert!(is_offered("plum")); assert!(!is_offered("removed-palette")); assert_eq!(resolve_base("ink").id, "ink"); assert_eq!(resolve_base("removed-base").id, BASE_DEFAULT); assert_eq!(base_id("mist"), BASE_DEFAULT); assert_eq!(base_id("heather"), "heather"); assert_eq!(base_id("graphite"), BASE_DEFAULT); } #[test] fn hue_ids_are_canonical_numbers_below_360() { for (id, hue) in [ ("hue-0", 0), ("hue-7", 7), ("hue-42", 42), ("hue-215", 215), ("hue-359", 359), ] { assert_eq!(hue_of(id), Some(hue), "{id}"); assert!(is_offered(id), "{id}"); assert!(is_offered_base(id), "{id}"); assert_eq!(accent_id(id), id); assert_eq!(base_id(id), id); } for id in [ "hue-", "hue-360", "hue-01", "hue-00", "hue-1000", "hue--1", "hue-+5", "hue-5 ", "Hue-5", "hue-5.0", "hue5", "hue_5", "hue-0x1", ] { assert_eq!(hue_of(id), None, "{id}"); assert!(!is_offered(id), "{id}"); assert!(!is_offered_base(id), "{id}"); } assert_eq!(resolve("hue-215").id, DEFAULT); assert_eq!(resolve_base("hue-215").id, BASE_DEFAULT); } #[test] fn preset_hues_come_from_the_colour() { assert_eq!(preset_accent_hue("sage"), Some(149)); assert_eq!(preset_accent_hue("ocean"), Some(240)); assert_eq!(preset_accent_hue("plum"), Some(300)); assert_eq!(preset_accent_hue("fern"), None); assert_eq!(preset_accent_hue("hue-5"), None); assert!(preset_base_hue("blush").is_some()); assert!(preset_base_hue("dusk").is_some()); assert_eq!(preset_base_hue("sage"), None); } #[test] fn the_custom_stylesheet_is_linked_only_when_a_hue_is_chosen() { assert_eq!(custom_css_href("sage", "plum", "blush", "ink"), None); assert_eq!( custom_css_href("hue-215", "plum", "blush", "hue-7").as_deref(), Some("/theme/custom.css?al=hue-215&ad=plum&bl=blush&bd=hue-7") ); assert_eq!( custom_css_href("sage", "sage", "ink", "hue-0").as_deref(), Some("/theme/custom.css?al=sage&ad=sage&bl=ink&bd=hue-0") ); } #[test] fn every_accent_keeps_its_own_text_readable() { for (id, light, dark) in all_accents() { for (theme, t) in [("light", &light), ("dark", &dark)] { let check = |what: &str, ratio: f64, min: f64| { assert!(ratio >= min, "{id} {theme} {what}: {ratio:.2} < {min}"); }; check( "accent_text on accent", contrast_ratio(&t.accent_text, &t.accent), 4.5, ); check( "public_text on public_bg", contrast_ratio(&t.public_text, &t.public_bg), 4.5, ); } } } #[test] fn every_base_with_every_accent_meets_the_contrast_targets() { let accents = all_accents(); for (base_id, base_light, base_dark) in all_bases() { for (accent_id, accent_light, accent_dark) in &accents { let themes = [ ("light", &base_light, accent_light), ("dark", &base_dark, accent_dark), ]; for (theme, b, t) in themes { let check = |what: &str, ratio: f64, min: f64| { assert!( ratio >= min, "{base_id} with {accent_id} ({theme}) {what}: {ratio:.2} < {min}" ); }; check("text on bg", contrast_ratio(&b.text, &b.bg), 7.0); check("text on surface", contrast_ratio(&b.text, &b.surface), 7.0); check("text_2 on bg", contrast_ratio(&b.text_2, &b.bg), 4.5); check( "text_2 on surface", contrast_ratio(&b.text_2, &b.surface), 4.5, ); check("muted on bg", contrast_ratio(&b.muted, &b.bg), 4.5); check( "muted on surface", contrast_ratio(&b.muted, &b.surface), 4.5, ); check( "ln_text on code_bg", contrast_ratio(&b.ln_text, &b.code_bg), 4.5, ); check( "private_text on private_bg", contrast_ratio(&b.private_text, &b.private_bg), 4.5, ); check( "accent_ink on bg", contrast_ratio(&t.accent_ink, &b.bg), 4.5, ); check( "accent_ink on surface", contrast_ratio(&t.accent_ink, &b.surface), 4.5, ); check( "accent_ink on accent_soft", contrast_ratio(&t.accent_ink, &t.accent_soft), 4.5, ); check("focus on bg", contrast_ratio(&t.focus, &b.bg), 3.0); } } } } }