From cd71b21b17e3e466995b98e0398eab6d44edbdbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 22 Oct 2026 13:16:35 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20feat:=20colour=20presets=20trimmed?= =?UTF-8?q?=20to=20a=20few,=20plus=20any=20hue=20for=20accent=20and=20back?= =?UTF-8?q?ground?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0065_hue_colours.sql | 79 ++ crates/forge-core/src/store/migrate.rs | 1 + crates/forge-core/src/store/ui_prefs.rs | 14 +- crates/forge-web/src/accent.rs | 407 +++++++-- crates/forge-web/src/accent_palette.rs | 856 +++--------------- crates/forge-web/src/custom_colour.rs | 405 +++++++++ crates/forge-web/src/i18n/accents.rs | 52 +- crates/forge-web/src/main.rs | 1 + crates/forge-web/src/pages/assets.rs | 89 +- crates/forge-web/src/pages/settings_live.rs | 18 +- crates/forge-web/src/views/mod.rs | 34 +- 11 files changed, 1047 insertions(+), 909 deletions(-) create mode 100644 crates/forge-core/migrations/0065_hue_colours.sql create mode 100644 crates/forge-web/src/custom_colour.rs diff --git a/crates/forge-core/migrations/0065_hue_colours.sql b/crates/forge-core/migrations/0065_hue_colours.sql new file mode 100644 index 0000000..4db4e69 --- /dev/null +++ b/crates/forge-core/migrations/0065_hue_colours.sql @@ -0,0 +1,79 @@ +-- The colour presets are trimmed to a few, and a free hue takes their place. `ember` is renamed +-- `spark`, which keeps its colours. Every other removed accent or base becomes a `hue-N` id, N +-- being the rounded OKLCH hue of the colour it had, so a user keeps roughly the same colour. +-- The ids are literal, since the web layer no longer carries the removed palettes. Applied by +-- store/migrate.rs. + +UPDATE users SET accent = CASE accent + WHEN 'ember' THEN 'spark' + WHEN 'ochre' THEN 'hue-55' + WHEN 'slate' THEN 'hue-252' + WHEN 'teal' THEN 'hue-196' + WHEN 'moss' THEN 'hue-123' + WHEN 'indigo' THEN 'hue-275' + WHEN 'amber' THEN 'hue-80' + WHEN 'clay' THEN 'hue-38' + WHEN 'berry' THEN 'hue-347' + WHEN 'crimson' THEN 'hue-19' + WHEN 'coral' THEN 'hue-30' + WHEN 'lime' THEN 'hue-125' + WHEN 'mint' THEN 'hue-167' + WHEN 'cyan' THEN 'hue-219' + WHEN 'sky' THEN 'hue-247' + WHEN 'violet' THEN 'hue-293' + WHEN 'fuchsia' THEN 'hue-324' + ELSE accent +END +WHERE accent IN ( + 'ember', 'ochre', 'slate', 'teal', 'moss', 'indigo', 'amber', 'clay', 'berry', 'crimson', + 'coral', 'lime', 'mint', 'cyan', 'sky', 'violet', 'fuchsia' +); + +UPDATE users SET accent_dark = CASE accent_dark + WHEN 'ember' THEN 'spark' + WHEN 'ochre' THEN 'hue-55' + WHEN 'slate' THEN 'hue-252' + WHEN 'teal' THEN 'hue-196' + WHEN 'moss' THEN 'hue-123' + WHEN 'indigo' THEN 'hue-275' + WHEN 'amber' THEN 'hue-80' + WHEN 'clay' THEN 'hue-38' + WHEN 'berry' THEN 'hue-347' + WHEN 'crimson' THEN 'hue-19' + WHEN 'coral' THEN 'hue-30' + WHEN 'lime' THEN 'hue-125' + WHEN 'mint' THEN 'hue-167' + WHEN 'cyan' THEN 'hue-219' + WHEN 'sky' THEN 'hue-247' + WHEN 'violet' THEN 'hue-293' + WHEN 'fuchsia' THEN 'hue-324' + ELSE accent_dark +END +WHERE accent_dark IN ( + 'ember', 'ochre', 'slate', 'teal', 'moss', 'indigo', 'amber', 'clay', 'berry', 'crimson', + 'coral', 'lime', 'mint', 'cyan', 'sky', 'violet', 'fuchsia' +); + +UPDATE users SET base_light = CASE base_light + WHEN 'stone' THEN 'hue-106' + WHEN 'sand' THEN 'hue-85' + WHEN 'mist' THEN 'hue-248' + WHEN 'dune' THEN 'hue-81' + WHEN 'lilac' THEN 'hue-299' + WHEN 'blush' THEN 'hue-2' + WHEN 'pine' THEN 'hue-178' + ELSE base_light +END +WHERE base_light IN ('stone', 'sand', 'mist', 'dune', 'lilac', 'blush', 'pine'); + +UPDATE users SET base_dark = CASE base_dark + WHEN 'stone' THEN 'hue-106' + WHEN 'sand' THEN 'hue-85' + WHEN 'mist' THEN 'hue-248' + WHEN 'dune' THEN 'hue-81' + WHEN 'lilac' THEN 'hue-299' + WHEN 'blush' THEN 'hue-2' + WHEN 'pine' THEN 'hue-178' + ELSE base_dark +END +WHERE base_dark IN ('stone', 'sand', 'mist', 'dune', 'lilac', 'blush', 'pine'); diff --git a/crates/forge-core/src/store/migrate.rs b/crates/forge-core/src/store/migrate.rs index 134dc0a..a067c9d 100644 --- a/crates/forge-core/src/store/migrate.rs +++ b/crates/forge-core/src/store/migrate.rs @@ -104,6 +104,7 @@ pub const MIGRATIONS: &[(u32, &str)] = &[ (62, include_str!("../../migrations/0062_oci.sql")), (63, include_str!("../../migrations/0063_web_push.sql")), (64, include_str!("../../migrations/0064_merge_queue.sql")), + (65, include_str!("../../migrations/0065_hue_colours.sql")), ]; /// The highest migration version this binary knows about. diff --git a/crates/forge-core/src/store/ui_prefs.rs b/crates/forge-core/src/store/ui_prefs.rs index 7235889..a26c9ec 100644 --- a/crates/forge-core/src/store/ui_prefs.rs +++ b/crates/forge-core/src/store/ui_prefs.rs @@ -250,7 +250,7 @@ mod tests { assert_eq!(user.base_dark, DEFAULT_BASE); store - .set_colours(&user.id, "ocean", "rose", "stone", "ink") + .set_colours(&user.id, "ocean", "rose", "graphite", "ink") .await .unwrap(); let stored = store.user_by_id(&user.id).await.unwrap().unwrap(); @@ -261,12 +261,12 @@ mod tests { stored.base_light.as_str(), stored.base_dark.as_str() ), - ("ocean", "rose", "stone", "ink") + ("ocean", "rose", "graphite", "ink") ); assert!( store - .set_colours(&user.id, "ocean", "Rose", "stone", "ink") + .set_colours(&user.id, "ocean", "Rose", "graphite", "ink") .await .is_err() ); @@ -278,7 +278,7 @@ mod tests { ); assert!( store - .set_colours(&user.id, "ocean", "rose", "stone", "") + .set_colours(&user.id, "ocean", "rose", "graphite", "") .await .is_err() ); @@ -290,7 +290,7 @@ mod tests { still.base_light.as_str(), still.base_dark.as_str() ), - ("ocean", "rose", "stone", "ink") + ("ocean", "rose", "graphite", "ink") ); } @@ -348,7 +348,7 @@ mod tests { .await .unwrap(); store - .set_colours(&user.id, "sage", "sage", "mist", "ink") + .set_colours(&user.id, "sage", "sage", "dusk", "ink") .await .unwrap(); @@ -356,7 +356,7 @@ mod tests { let stored = store.user_by_id(&user.id).await.unwrap().unwrap(); assert_eq!(stored.accent, "ocean"); assert_eq!(stored.accent_dark, "ocean"); - assert_eq!(stored.base_light, "mist"); + assert_eq!(stored.base_light, "dusk"); assert_eq!(stored.base_dark, "ink"); } diff --git a/crates/forge-web/src/accent.rs b/crates/forge-web/src/accent.rs index 5941d8b..f920c7a 100644 --- a/crates/forge-web/src/accent.rs +++ b/crates/forge-web/src/accent.rs @@ -1,10 +1,13 @@ -//! Accent colour and base: which palette and which neutral set a viewer sees. The values live in -//! `accent_palette.rs`, which `build.rs` also turns into the stylesheet. This module only resolves -//! stored ids and checks which ids forge offers. +//! Accent colour and base: which palette and which neutral set a viewer sees. The presets live in +//! `accent_palette.rs`, which `build.rs` also turns into the stylesheet. A `hue-N` id stands for a +//! colour of any hue, built by `custom_colour.rs`. This module resolves stored ids and checks which +//! ids forge offers. use crate::accent_palette::{BASE_DEFAULT, BASES, Base, DEFAULT, PALETTES, Palette}; +use crate::custom_colour; -/// The palette for a stored id. An unknown or removed id falls back to the default. +/// The preset palette for a stored id. An unknown or removed id, and a `hue-N` id, fall back to the +/// default. A page shows `accent_id` instead, which keeps a hue. pub fn resolve(stored: &str) -> &'static Palette { PALETTES .iter() @@ -12,17 +15,25 @@ pub fn resolve(stored: &str) -> &'static Palette { .unwrap_or_else(default_palette) } -/// True when forge offers this id. +/// The accent id a page shows for a stored one: the id itself when forge offers it (a preset or a +/// hue), otherwise the default. +pub fn accent_id(stored: &str) -> &str { + if is_offered(stored) { stored } else { DEFAULT } +} + +/// True when forge offers this accent id: a preset, or a `hue-N` id. pub fn is_offered(id: &str) -> bool { - PALETTES.iter().any(|p| p.id == id) + PALETTES.iter().any(|p| p.id == id) || hue_of(id).is_some() } -/// Every offered id, in the order the picker lists them. +/// Every preset accent id, in the order the picker lists them. Hues are not listed: the picker +/// offers a free choice for them. pub fn ids() -> impl Iterator { PALETTES.iter().map(|p| p.id) } -/// The base for a stored id. An unknown or removed id falls back to the default. +/// The preset base for a stored id. An unknown or removed id, and a `hue-N` id, fall back to the +/// default. A page shows `base_id` instead, which keeps a hue. pub fn resolve_base(stored: &str) -> &'static Base { BASES .iter() @@ -30,16 +41,107 @@ pub fn resolve_base(stored: &str) -> &'static Base { .unwrap_or_else(default_base) } -/// True when forge offers this base id. +/// The base id a page shows for a stored one: the id itself when forge offers it (a preset or a +/// hue), otherwise the default. +pub fn base_id(stored: &str) -> &str { + if is_offered_base(stored) { + stored + } else { + BASE_DEFAULT + } +} + +/// True when forge offers this base id: a preset, or a `hue-N` id. pub fn is_offered_base(id: &str) -> bool { - BASES.iter().any(|b| b.id == id) + BASES.iter().any(|b| b.id == id) || hue_of(id).is_some() } -/// Every offered base id, in the order the picker lists them. +/// Every preset base id, in the order the picker lists them. Hues are not listed. pub fn base_ids() -> impl Iterator { BASES.iter().map(|b| b.id) } +/// The hue N of a `hue-N` id: an integer `0..=359` with no leading zeros (except `hue-0`). Any +/// other id is None. +pub fn hue_of(id: &str) -> Option { + let digits = id.strip_prefix("hue-")?; + 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(|hue| *hue < 360) +} + +/// The OKLCH hue, in degrees, of a preset accent's light colour. None for an id that is not a +/// preset. +#[allow(dead_code)] // The settings picker places its knob with this. +pub fn preset_accent_hue(id: &str) -> Option { + PALETTES + .iter() + .find(|p| p.id == id) + .map(|p| custom_colour::hex_hue(p.light.accent)) +} + +/// The OKLCH hue, in degrees, of a preset base's light background. A near-neutral base has no +/// strong hue, so this only places the picker's knob. None for an id that is not a preset. +#[allow(dead_code)] // The settings picker places its knob with this. +pub fn preset_base_hue(id: &str) -> Option { + BASES + .iter() + .find(|b| b.id == id) + .map(|b| custom_colour::hex_hue(b.light.bg)) +} + +/// The `#RRGGBB` the picker shows for an accent id in one theme: the accent itself. A `hue-N` id +/// gets the accent built for its hue; an id forge does not offer gets the default's. +#[allow(dead_code)] // The settings picker shows its chips with this. +pub fn swatch(id: &str, dark: bool) -> String { + match hue_of(id) { + Some(hue) => custom_colour::accent_tokens(hue, dark).accent, + None => { + let palette = resolve(id); + let tokens = if dark { &palette.dark } else { &palette.light }; + tokens.accent.to_owned() + } + } +} + +/// The background and text colours the picker shows for a base id in one theme. A `hue-N` id gets +/// the base built for its hue; an id forge does not offer gets the default's. +#[allow(dead_code)] // The settings picker shows its chips with this. +pub fn base_swatch(id: &str, dark: bool) -> (String, String) { + match hue_of(id) { + Some(hue) => { + let tokens = custom_colour::base_tokens(hue, dark); + (tokens.bg, tokens.text) + } + None => { + let base = resolve_base(id); + let tokens = if dark { &base.dark } else { &base.light }; + (tokens.bg.to_owned(), tokens.text.to_owned()) + } + } +} + +/// The link to the stylesheet that styles the custom ids of one page, or None when none of the four +/// is a hue (the presets are styled by the main stylesheet). The ids go in as given, so pass the +/// ids a page shows (see `accent_id` and `base_id`). +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}" + ) + }) +} + fn default_palette() -> &'static Palette { PALETTES .iter() @@ -59,26 +161,15 @@ mod tests { use std::collections::HashSet; use super::*; - use crate::accent_palette::{BASE_TOKEN_VARS, TOKEN_VARS}; - - /// WCAG relative luminance of a `#RRGGBB` colour. - fn luminance(hex: &str) -> f64 { - let channel = |i: usize| { - let v = f64::from(u8::from_str_radix(&hex[i..i + 2], 16).expect("hex")) / 255.0; - if v <= 0.03928 { - v / 12.92 - } else { - ((v + 0.055) / 1.055).powf(2.4) - } - }; - 0.2126 * channel(1) + 0.7152 * channel(3) + 0.0722 * channel(5) - } + use crate::accent_palette::{BASE_TOKEN_VARS, BaseTokens, TOKEN_VARS, Tokens}; + use crate::custom_colour::{ + OwnedBaseTokens, OwnedTokens, accent_tokens, base_tokens, contrast_ratio, + }; - /// WCAG contrast ratio between two colours. - fn contrast(a: &str, b: &str) -> f64 { - let (x, y) = (luminance(a), luminance(b)); - (x.max(y) + 0.05) / (x.min(y) + 0.05) - } + /// One accent's light and dark colours, as owned strings. Presets and hues go through the same + /// checks this way. + type AccentSet = (String, OwnedTokens, OwnedTokens); + type BaseSet = (String, OwnedBaseTokens, OwnedBaseTokens); fn is_hex_colour(value: &str) -> bool { value.len() == 7 @@ -86,6 +177,72 @@ mod tests { && 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(), + } + } + + /// Every accent the picker can offer: the presets, then the 360 hues. + 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, false), + accent_tokens(hue, true), + ) + }); + presets.chain(hues).collect() + } + + /// Every base the picker can offer: the presets, then the 360 hues. + 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, false), + base_tokens(hue, true), + ) + }); + presets.chain(hues).collect() + } + #[test] fn ids_are_slugs_unique_and_the_default_comes_first() { assert_eq!(PALETTES[0].id, DEFAULT); @@ -134,79 +291,126 @@ mod tests { } } + #[test] + fn the_presets_are_offered_in_picker_order() { + assert_eq!( + ids().collect::>(), + ["sage", "spark", "ocean", "plum", "rose", "gold"] + ); + assert_eq!( + base_ids().collect::>(), + ["fern", "spark", "graphite", "dusk", "ink"] + ); + } + + #[test] + fn removed_ids_are_not_offered() { + for id in ["ember", "ochre", "teal", "Sage", "sage ", "terracotta"] { + assert!(!is_offered(id), "{id}"); + } + for id in ["stone", "mist", "Fern", "dune", "slate-blue"] { + 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("ember"), 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("graphite"), "graphite"); } #[test] - fn the_newer_accents_are_offered_by_their_exact_id() { - for id in [ - "teal", "moss", "indigo", "amber", "clay", "berry", "ember", "crimson", "coral", - "gold", "lime", "mint", "cyan", "sky", "violet", "fuchsia", + 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_eq!(resolve(id).id, id); + assert!(is_offered_base(id), "{id}"); + assert_eq!(accent_id(id), id); + assert_eq!(base_id(id), id); } for id in [ - "Teal", - "teal ", - "terracotta", - "clay-2", - "Ember", - "Crimson", - "rust", + "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!(ids().count(), 22); + // A hue is not a preset, so the preset lookup still falls back to the default. + assert_eq!(resolve("hue-215").id, DEFAULT); + assert_eq!(resolve_base("hue-215").id, BASE_DEFAULT); } #[test] - fn the_newer_backgrounds_are_offered_by_their_exact_id() { - for id in ["dune", "lilac", "dusk", "blush", "graphite", "pine"] { - assert!(is_offered_base(id), "{id}"); - assert_eq!(resolve_base(id).id, id); - } - for id in ["Dune", "dusk ", "slate-blue", "Pine"] { - assert!(!is_offered_base(id), "{id}"); - } - assert_eq!(base_ids().count(), 11); + 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("spark"), Some(38)); + assert_eq!(preset_accent_hue("fern"), None); + assert_eq!(preset_accent_hue("hue-5"), None); + assert!(preset_base_hue("fern").is_some()); + assert!(preset_base_hue("spark").is_some()); + assert_eq!(preset_base_hue("sage"), None); } #[test] - fn unknown_and_removed_base_ids_fall_back_to_the_default() { - assert_eq!(resolve_base("ink").id, "ink"); - assert_eq!(resolve_base("removed-base").id, BASE_DEFAULT); - assert_eq!(resolve_base("").id, BASE_DEFAULT); - assert!(is_offered_base("mist")); - assert!(!is_offered_base("removed-base")); + fn swatches_show_the_accent_and_the_base_for_presets_and_hues() { + assert_eq!(swatch("sage", false), "#5E7D63"); + assert_eq!(swatch("sage", true), "#9DBEA2"); + assert_eq!(swatch("hue-215", false), accent_tokens(215, false).accent); + assert_eq!(swatch("hue-215", true), accent_tokens(215, true).accent); + assert_eq!(swatch("ember", false), swatch("sage", false)); + let (bg, text) = base_swatch("fern", false); + assert_eq!((bg.as_str(), text.as_str()), ("#F6F8F5", "#1E2420")); + let (bg, text) = base_swatch("hue-215", true); + assert_eq!(bg, base_tokens(215, true).bg); + assert_eq!(text, base_tokens(215, true).text); + } + + #[test] + fn the_custom_stylesheet_is_linked_only_when_a_hue_is_chosen() { + assert_eq!(custom_css_href("sage", "plum", "fern", "ink"), None); + assert_eq!( + custom_css_href("hue-215", "plum", "fern", "hue-7").as_deref(), + Some("/theme/custom.css?al=hue-215&ad=plum&bl=fern&bd=hue-7") + ); + assert_eq!( + custom_css_href("sage", "sage", "spark", "hue-0").as_deref(), + Some("/theme/custom.css?al=sage&ad=sage&bl=spark&bd=hue-0") + ); } #[test] fn every_accent_keeps_its_own_text_readable() { - for p in PALETTES { - let check = |what: &str, ratio: f64, min: f64| { - assert!(ratio >= min, "{} {what}: {ratio:.2} < {min}", p.id); - }; - check( - "accent_text on accent (light)", - contrast(p.light.accent_text, p.light.accent), - 4.5, - ); - check( - "accent_text on accent (dark)", - contrast(p.dark.accent_text, p.dark.accent), - 4.5, - ); - for t in [&p.light, &p.dark] { + 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(t.public_text, t.public_bg), + contrast_ratio(&t.public_text, &t.public_bg), 4.5, ); } @@ -215,41 +419,60 @@ mod tests { #[test] fn every_base_with_every_accent_meets_the_contrast_targets() { - for base in BASES { - for p in PALETTES { + 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, &p.light), - ("dark", &base.dark, &p.dark), + ("light", &base_light, accent_light), + ("dark", &base_dark, accent_dark), ]; for (theme, b, t) in themes { - let who = format!("{} with {} ({theme})", base.id, p.id); let check = |what: &str, ratio: f64, min: f64| { - assert!(ratio >= min, "{who} {what}: {ratio:.2} < {min}"); + assert!( + ratio >= min, + "{base_id} with {accent_id} ({theme}) {what}: {ratio:.2} < {min}" + ); }; - check("text on bg", contrast(b.text, b.bg), 7.0); - check("text on surface", contrast(b.text, b.surface), 7.0); - check("text_2 on bg", contrast(b.text_2, b.bg), 4.5); - check("text_2 on surface", contrast(b.text_2, b.surface), 4.5); - check("muted on bg", contrast(b.muted, b.bg), 4.5); - check("muted on surface", contrast(b.muted, b.surface), 4.5); - check("ln_text on code_bg", contrast(b.ln_text, b.code_bg), 4.5); + 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(b.private_text, b.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 bg", contrast(t.accent_ink, b.bg), 4.5); check( "accent_ink on surface", - contrast(t.accent_ink, b.surface), + contrast_ratio(&t.accent_ink, &b.surface), 4.5, ); check( "accent_ink on accent_soft", - contrast(t.accent_ink, t.accent_soft), + contrast_ratio(&t.accent_ink, &t.accent_soft), 4.5, ); - check("focus on bg", contrast(t.focus, b.bg), 3.0); + check("focus on bg", contrast_ratio(&t.focus, &b.bg), 3.0); } } } diff --git a/crates/forge-web/src/accent_palette.rs b/crates/forge-web/src/accent_palette.rs index 8981692..e09371e 100644 --- a/crates/forge-web/src/accent_palette.rs +++ b/crates/forge-web/src/accent_palette.rs @@ -111,6 +111,34 @@ pub const PALETTES: &[Palette] = &[ hunk_text: "#9DBEA2", }, }, + // Forge-spark orange: hot metal, the most saturated accent on offer. + Palette { + id: "spark", + light: Tokens { + accent: "#C2410C", + accent_ink: "#9A3412", + accent_soft: "#FCEBE0", + accent_text: "#FFFFFF", + focus: "#9A3412", + public_bg: "#FCEBE0", + public_text: "#9A3412", + note: "#9A3412", + hunk_bg: "#FDF4EF", + hunk_text: "#8F3110", + }, + dark: Tokens { + accent: "#FB923C", + accent_ink: "#FDBA74", + accent_soft: "#3A2010", + accent_text: "#1C0D04", + focus: "#FB923C", + public_bg: "#3A2010", + public_text: "#FDBA74", + note: "#FDBA74", + hunk_bg: "#2A1A0F", + hunk_text: "#FB923C", + }, + }, // Blue, clear and cool. Palette { id: "ocean", @@ -195,315 +223,7 @@ pub const PALETTES: &[Palette] = &[ hunk_text: "#E79DAD", }, }, - // Clay brown, warmer and earthier than the warning amber. - Palette { - id: "ochre", - light: Tokens { - accent: "#93684B", - accent_ink: "#7A5338", - accent_soft: "#F5E6DD", - accent_text: "#FFFFFF", - focus: "#7A5338", - public_bg: "#F5E6DD", - public_text: "#7A5338", - note: "#7A5338", - hunk_bg: "#F6EEE8", - hunk_text: "#724B30", - }, - dark: Tokens { - accent: "#D8A98A", - accent_ink: "#E4BCA1", - accent_soft: "#39281D", - accent_text: "#170D07", - focus: "#D8A98A", - public_bg: "#39281D", - public_text: "#E4BCA1", - note: "#E4BCA1", - hunk_bg: "#2C211A", - hunk_text: "#D8A98A", - }, - }, - // Blue-grey graphite, nearly neutral. - Palette { - id: "slate", - light: Tokens { - accent: "#657383", - accent_ink: "#505E6C", - accent_soft: "#E0EBF7", - accent_text: "#FFFFFF", - focus: "#505E6C", - public_bg: "#E0EBF7", - public_text: "#505E6C", - note: "#505E6C", - hunk_bg: "#EAF0F7", - hunk_text: "#485563", - }, - dark: Tokens { - accent: "#A4B4C4", - accent_ink: "#B8C6D5", - accent_soft: "#272D33", - accent_text: "#0C1013", - focus: "#A4B4C4", - public_bg: "#272D33", - public_text: "#B8C6D5", - note: "#B8C6D5", - hunk_bg: "#212428", - hunk_text: "#A4B4C4", - }, - }, - // Teal, a muted blue-green. - Palette { - id: "teal", - light: Tokens { - accent: "#437C7C", - accent_ink: "#396A6A", - accent_soft: "#E7F0F0", - accent_text: "#FFFFFF", - focus: "#396A6A", - public_bg: "#E7F0F0", - public_text: "#396A6A", - note: "#396A6A", - hunk_bg: "#F1F6F6", - hunk_text: "#366464", - }, - dark: Tokens { - accent: "#4B8B8B", - accent_ink: "#58A4A4", - accent_soft: "#213030", - accent_text: "#0F1515", - focus: "#4B8B8B", - public_bg: "#213030", - public_text: "#58A4A4", - note: "#58A4A4", - hunk_bg: "#1A2323", - hunk_text: "#58A4A4", - }, - }, - // Olive green, earthier than Sage. - Palette { - id: "moss", - light: Tokens { - accent: "#697947", - accent_ink: "#59673D", - accent_soft: "#EDF0E8", - accent_text: "#FFFFFF", - focus: "#59673D", - public_bg: "#EDF0E8", - public_text: "#59673D", - note: "#59673D", - hunk_bg: "#F4F5F2", - hunk_text: "#546139", - }, - dark: Tokens { - accent: "#758850", - accent_ink: "#8AA05E", - accent_soft: "#2B2F22", - accent_text: "#131410", - focus: "#758850", - public_bg: "#2B2F22", - public_text: "#8AA05E", - note: "#8AA05E", - hunk_bg: "#20231B", - hunk_text: "#8AA05E", - }, - }, - // Soft indigo, cooler than Plum. - Palette { - id: "indigo", - light: Tokens { - accent: "#646FB3", - accent_ink: "#515DA5", - accent_soft: "#E7E8F1", - accent_text: "#FFFFFF", - focus: "#515DA5", - public_bg: "#E7E8F1", - public_text: "#515DA5", - note: "#515DA5", - hunk_bg: "#F1F2F6", - hunk_text: "#4E599F", - }, - dark: Tokens { - accent: "#717BB9", - accent_ink: "#858EC3", - accent_soft: "#202331", - accent_text: "#0F1015", - focus: "#717BB9", - public_bg: "#202331", - public_text: "#858EC3", - note: "#858EC3", - hunk_bg: "#191B24", - hunk_text: "#858EC3", - }, - }, - // Golden amber, warm and muted. - Palette { - id: "amber", - light: Tokens { - accent: "#936D22", - accent_ink: "#7D5D1D", - accent_soft: "#F5EFE2", - accent_text: "#FFFFFF", - focus: "#7D5D1D", - public_bg: "#F5EFE2", - public_text: "#7D5D1D", - note: "#7D5D1D", - hunk_bg: "#F8F5EF", - hunk_text: "#75571B", - }, - dark: Tokens { - accent: "#A37A26", - accent_ink: "#C3912E", - accent_soft: "#382E1A", - accent_text: "#17140C", - focus: "#A37A26", - public_bg: "#382E1A", - public_text: "#C3912E", - note: "#C3912E", - hunk_bg: "#282215", - hunk_text: "#C3912E", - }, - }, - // Terracotta, a warm red-orange clay. - Palette { - id: "clay", - light: Tokens { - accent: "#AC5E46", - accent_ink: "#93503C", - accent_soft: "#F2E8E5", - accent_text: "#FFFFFF", - focus: "#93503C", - public_bg: "#F2E8E5", - public_text: "#93503C", - note: "#93503C", - hunk_bg: "#F6F2F1", - hunk_text: "#8C4C39", - }, - dark: Tokens { - accent: "#B96A53", - accent_ink: "#C3806B", - accent_soft: "#33231F", - accent_text: "#16100E", - focus: "#B96A53", - public_bg: "#33231F", - public_text: "#C3806B", - note: "#C3806B", - hunk_bg: "#251B18", - hunk_text: "#C3806B", - }, - }, - // Deep berry, richer than Rose. - Palette { - id: "berry", - light: Tokens { - accent: "#AC5787", - accent_ink: "#944973", - accent_soft: "#F1E7ED", - accent_text: "#FFFFFF", - focus: "#944973", - public_bg: "#F1E7ED", - public_text: "#944973", - note: "#944973", - hunk_bg: "#F6F1F4", - hunk_text: "#8D466E", - }, - dark: Tokens { - accent: "#B36692", - accent_ink: "#BF7CA2", - accent_soft: "#31202A", - accent_text: "#150F12", - focus: "#B36692", - public_bg: "#31202A", - public_text: "#BF7CA2", - note: "#BF7CA2", - hunk_bg: "#24191F", - hunk_text: "#BF7CA2", - }, - }, - // Forge-spark orange: hot metal, the most saturated accent on offer. - Palette { - id: "ember", - light: Tokens { - accent: "#C2410C", - accent_ink: "#9A3412", - accent_soft: "#FCEBE0", - accent_text: "#FFFFFF", - focus: "#9A3412", - public_bg: "#FCEBE0", - public_text: "#9A3412", - note: "#9A3412", - hunk_bg: "#FDF4EF", - hunk_text: "#8F3110", - }, - dark: Tokens { - accent: "#FB923C", - accent_ink: "#FDBA74", - accent_soft: "#3A2010", - accent_text: "#1C0D04", - focus: "#FB923C", - public_bg: "#3A2010", - public_text: "#FDBA74", - note: "#FDBA74", - hunk_bg: "#2A1A0F", - hunk_text: "#FB923C", - }, - }, - // Red, pure and saturated, a clear step past Rose and Berry. - Palette { - id: "crimson", - light: Tokens { - accent: "#B3263A", - accent_ink: "#9E1C2E", - accent_soft: "#F8E3E6", - accent_text: "#FFFFFF", - focus: "#9E1C2E", - public_bg: "#F8E3E6", - public_text: "#9E1C2E", - note: "#9E1C2E", - hunk_bg: "#FBF1F2", - hunk_text: "#9E1C2E", - }, - dark: Tokens { - accent: "#F0707F", - accent_ink: "#FFA3AE", - accent_soft: "#3A1419", - accent_text: "#2A0A0E", - focus: "#F0707F", - public_bg: "#3A1419", - public_text: "#FFA3AE", - note: "#FFA3AE", - hunk_bg: "#2A1417", - hunk_text: "#F0707F", - }, - }, - // Salmon-orange, brighter and lighter than Clay. - Palette { - id: "coral", - light: Tokens { - accent: "#C4493A", - accent_ink: "#B0402F", - accent_soft: "#FBE6E1", - accent_text: "#FFFFFF", - focus: "#B0402F", - public_bg: "#FBE6E1", - public_text: "#B0402F", - note: "#B0402F", - hunk_bg: "#FDF3F0", - hunk_text: "#B0402F", - }, - dark: Tokens { - accent: "#FF8A7A", - accent_ink: "#FFADA0", - accent_soft: "#3A1B15", - accent_text: "#2A0C08", - focus: "#FF8A7A", - public_bg: "#3A1B15", - public_text: "#FFADA0", - note: "#FFADA0", - hunk_bg: "#2C1511", - hunk_text: "#FF8A7A", - }, - }, - // Bright yellow, lighter and more lemon than Amber, so it takes dark text. + // Bright yellow, leaning to lemon, so it takes dark text. Palette { id: "gold", light: Tokens { @@ -531,174 +251,6 @@ pub const PALETTES: &[Palette] = &[ hunk_text: "#E8BC4A", }, }, - // Yellow-green, a fresh step brighter than Moss. - Palette { - id: "lime", - light: Tokens { - accent: "#5C7A00", - accent_ink: "#4F6A00", - accent_soft: "#EEF5DC", - accent_text: "#FFFFFF", - focus: "#4F6A00", - public_bg: "#EEF5DC", - public_text: "#4F6A00", - note: "#4F6A00", - hunk_bg: "#F6FAEC", - hunk_text: "#4F6A00", - }, - dark: Tokens { - accent: "#A3D130", - accent_ink: "#C2E664", - accent_soft: "#263311", - accent_text: "#141A03", - focus: "#A3D130", - public_bg: "#263311", - public_text: "#C2E664", - note: "#C2E664", - hunk_bg: "#1C2510", - hunk_text: "#A3D130", - }, - }, - // Soft green-cyan, lighter and cooler than Sage. - Palette { - id: "mint", - light: Tokens { - accent: "#2E7D62", - accent_ink: "#22664F", - accent_soft: "#E0F4EC", - accent_text: "#FFFFFF", - focus: "#22664F", - public_bg: "#E0F4EC", - public_text: "#22664F", - note: "#22664F", - hunk_bg: "#EFF9F5", - hunk_text: "#22664F", - }, - dark: Tokens { - accent: "#6FD3AE", - accent_ink: "#9BE6CA", - accent_soft: "#14332A", - accent_text: "#0A1F17", - focus: "#6FD3AE", - public_bg: "#14332A", - public_text: "#9BE6CA", - note: "#9BE6CA", - hunk_bg: "#10251F", - hunk_text: "#6FD3AE", - }, - }, - // Bright blue-green, clearer than Teal. - Palette { - id: "cyan", - light: Tokens { - accent: "#00728A", - accent_ink: "#00677D", - accent_soft: "#DDF3F6", - accent_text: "#FFFFFF", - focus: "#00677D", - public_bg: "#DDF3F6", - public_text: "#00677D", - note: "#00677D", - hunk_bg: "#EEF9FB", - hunk_text: "#00677D", - }, - dark: Tokens { - accent: "#4DD0E1", - accent_ink: "#8BE4F0", - accent_soft: "#0E3036", - accent_text: "#03181B", - focus: "#4DD0E1", - public_bg: "#0E3036", - public_text: "#8BE4F0", - note: "#8BE4F0", - hunk_bg: "#0B2327", - hunk_text: "#4DD0E1", - }, - }, - // Clear sky blue, brighter than Ocean. - Palette { - id: "sky", - light: Tokens { - accent: "#1A6FB0", - accent_ink: "#1A64A0", - accent_soft: "#E1EFFA", - accent_text: "#FFFFFF", - focus: "#1A64A0", - public_bg: "#E1EFFA", - public_text: "#1A64A0", - note: "#1A64A0", - hunk_bg: "#EFF6FC", - hunk_text: "#1A64A0", - }, - dark: Tokens { - accent: "#7CC4F0", - accent_ink: "#A6D8F7", - accent_soft: "#13293A", - accent_text: "#061826", - focus: "#7CC4F0", - public_bg: "#13293A", - public_text: "#A6D8F7", - note: "#A6D8F7", - hunk_bg: "#0F202D", - hunk_text: "#7CC4F0", - }, - }, - // Vivid blue-purple, more saturated than Indigo. - Palette { - id: "violet", - light: Tokens { - accent: "#7C3AED", - accent_ink: "#6D34C9", - accent_soft: "#EEE7FC", - accent_text: "#FFFFFF", - focus: "#6D34C9", - public_bg: "#EEE7FC", - public_text: "#6D34C9", - note: "#6D34C9", - hunk_bg: "#F6F2FD", - hunk_text: "#6D34C9", - }, - dark: Tokens { - accent: "#B794F6", - accent_ink: "#CDB5FA", - accent_soft: "#2A1F45", - accent_text: "#15092E", - focus: "#B794F6", - public_bg: "#2A1F45", - public_text: "#CDB5FA", - note: "#CDB5FA", - hunk_bg: "#201832", - hunk_text: "#B794F6", - }, - }, - // Hot magenta-pink, the brightest purple-pink on offer. - Palette { - id: "fuchsia", - light: Tokens { - accent: "#A21CAF", - accent_ink: "#8F1A9A", - accent_soft: "#F8E3F5", - accent_text: "#FFFFFF", - focus: "#8F1A9A", - public_bg: "#F8E3F5", - public_text: "#8F1A9A", - note: "#8F1A9A", - hunk_bg: "#FBF1FA", - hunk_text: "#8F1A9A", - }, - dark: Tokens { - accent: "#E879F9", - accent_ink: "#F2A6FB", - accent_soft: "#3A1638", - accent_text: "#2A0530", - focus: "#E879F9", - public_bg: "#3A1638", - public_text: "#F2A6FB", - note: "#F2A6FB", - hunk_bg: "#2C1229", - hunk_text: "#E879F9", - }, - }, ]; /// The neutral colour tokens one base sets in one theme. The values are CSS colours; @@ -823,199 +375,71 @@ pub const BASES: &[Base] = &[ ln_text: "#97A198", }, }, - // Stone: pure neutral grey. + // Spark: warm neutrals tinted toward the Forge-spark orange, hue about 45 degrees. Base { - id: "stone", + id: "spark", light: BaseTokens { - bg: "#F5F5F4", + bg: "#FDF3EF", surface: "#FFFFFF", - surface_2: "#EDEDEB", - text: "#1C1C1B", - text_2: "#3D3D3B", - muted: "#6B6B68", - border: "#DEDEDB", - border_strong: "#C6C6C2", - code_bg: "#EDEDEB", - private_bg: "#EDEDEB", - private_text: "#3D3D3B", - ln_text: "#6B6B68", - }, - dark: BaseTokens { - bg: "#131313", - surface: "#1A1A1A", - surface_2: "#212121", - text: "#E6E6E4", - text_2: "#C7C7C4", - muted: "#9A9A96", - border: "#2C2C2B", - border_strong: "#3C3C3A", - code_bg: "#212121", - private_bg: "#212121", - private_text: "#C7C7C4", - ln_text: "#9A9A96", - }, - }, - // Sand: warm paper and warm ink. - Base { - id: "sand", - light: BaseTokens { - bg: "#F8F5EF", - surface: "#FFFDF9", - surface_2: "#F0EBE1", - text: "#2A2620", - text_2: "#4A443B", - muted: "#716A5E", - border: "#E2DBCF", - border_strong: "#CBC2B3", - code_bg: "#F0EBE1", - private_bg: "#F0EBE1", - private_text: "#4A443B", - ln_text: "#6B6457", + surface_2: "#F4EAE6", + text: "#221B18", + text_2: "#3B3430", + muted: "#6F6763", + border: "#E2D8D4", + border_strong: "#CBC2BE", + code_bg: "#F4EAE6", + private_bg: "#F4EAE6", + private_text: "#3B3430", + ln_text: "#69615E", }, dark: BaseTokens { - bg: "#171511", - surface: "#1E1B16", - surface_2: "#26221C", - text: "#E9E4DA", - text_2: "#CBC5B9", - muted: "#A29B8D", - border: "#342F27", - border_strong: "#463F35", - code_bg: "#26221C", - private_bg: "#26221C", - private_text: "#CBC5B9", - ln_text: "#A29B8D", - }, - }, - // Mist: cool blue-grey. + bg: "#19120F", + surface: "#211A17", + surface_2: "#29221F", + text: "#EFE6E1", + text_2: "#CBC2BE", + muted: "#A59C98", + border: "#332C29", + border_strong: "#48403D", + code_bg: "#29221F", + private_bg: "#29221F", + private_text: "#CBC2BE", + ln_text: "#A59C98", + }, + }, + // Graphite: a cool neutral grey, flatter than the warmer neutrals. Base { - id: "mist", - light: BaseTokens { - bg: "#F3F6F9", - surface: "#FFFFFF", - surface_2: "#E9EEF3", - text: "#1B2430", - text_2: "#384352", - muted: "#66717F", - border: "#D9E0E8", - border_strong: "#C2CCD8", - code_bg: "#E9EEF3", - private_bg: "#E9EEF3", - private_text: "#384352", - ln_text: "#5E6977", - }, - dark: BaseTokens { - bg: "#11161C", - surface: "#182029", - surface_2: "#1E2834", - text: "#E2E8EF", - text_2: "#C3CCD7", - muted: "#949FAD", - border: "#28323E", - border_strong: "#37424F", - code_bg: "#1E2834", - private_bg: "#1E2834", - private_text: "#C3CCD7", - ln_text: "#949FAD", - }, - }, - // Ink: crisp white in light, near-black in dark. - Base { - id: "ink", - light: BaseTokens { - bg: "#FCFCFB", - surface: "#FFFFFF", - surface_2: "#F2F2F0", - text: "#111111", - text_2: "#333333", - muted: "#5F5F5C", - border: "#E3E3E0", - border_strong: "#C9C9C5", - code_bg: "#F2F2F0", - private_bg: "#F2F2F0", - private_text: "#333333", - ln_text: "#5F5F5C", - }, - dark: BaseTokens { - bg: "#000000", - surface: "#0B0B0B", - surface_2: "#141414", - text: "#F2F2F0", - text_2: "#CFCFCC", - muted: "#9C9C98", - border: "#242424", - border_strong: "#353535", - code_bg: "#141414", - private_bg: "#141414", - private_text: "#CFCFCC", - ln_text: "#9C9C98", - }, - }, - // Dune: warm beige, a step warmer and browner than Sand. - Base { - id: "dune", - light: BaseTokens { - bg: "#F4EBDC", - surface: "#FCF8F1", - surface_2: "#EDE3D1", - text: "#2B2318", - text_2: "#473F31", - muted: "#6F6550", - border: "#E0D5C1", - border_strong: "#C9BBA2", - code_bg: "#EDE3D1", - private_bg: "#EDE3D1", - private_text: "#473F31", - ln_text: "#6A5F4C", - }, - dark: BaseTokens { - bg: "#17120B", - surface: "#1F1912", - surface_2: "#28211A", - text: "#EEE6D8", - text_2: "#D0C7B6", - muted: "#A39A88", - border: "#362E24", - border_strong: "#483E31", - code_bg: "#28211A", - private_bg: "#28211A", - private_text: "#D0C7B6", - ln_text: "#A39A88", - }, - }, - // Lilac: a soft cool violet tint. - Base { - id: "lilac", + id: "graphite", light: BaseTokens { - bg: "#F5F3FA", - surface: "#FFFFFF", - surface_2: "#ECE9F5", - text: "#1F1B2C", - text_2: "#3B3650", - muted: "#686180", - border: "#DCD7EA", - border_strong: "#C3BCD9", - code_bg: "#ECE9F5", - private_bg: "#ECE9F5", - private_text: "#3B3650", - ln_text: "#625B77", + bg: "#E9EAEC", + surface: "#F9FAFB", + surface_2: "#DCDEE2", + text: "#17191C", + text_2: "#33373D", + muted: "#5C626B", + border: "#CFD3D9", + border_strong: "#B5BBC3", + code_bg: "#DCDEE2", + private_bg: "#DCDEE2", + private_text: "#33373D", + ln_text: "#545A63", }, dark: BaseTokens { - bg: "#12101A", - surface: "#1A1725", - surface_2: "#221E2E", - text: "#E7E4F0", - text_2: "#C9C4D8", - muted: "#9E97B0", - border: "#2C2739", - border_strong: "#3C3650", - code_bg: "#221E2E", - private_bg: "#221E2E", - private_text: "#C9C4D8", - ln_text: "#9E97B0", + bg: "#1A1B1E", + surface: "#222428", + surface_2: "#2B2D32", + text: "#E6E8EB", + text_2: "#C8CCD2", + muted: "#9499A2", + border: "#33363C", + border_strong: "#44484F", + code_bg: "#2B2D32", + private_bg: "#2B2D32", + private_text: "#C8CCD2", + ln_text: "#9499A2", }, }, - // Dusk: cool blue-grey, deeper and bluer than Mist. + // Dusk: cool blue-grey, a deeper blue-tinted neutral. Base { id: "dusk", light: BaseTokens { @@ -1047,100 +471,36 @@ pub const BASES: &[Base] = &[ ln_text: "#8F9BB0", }, }, - // Blush: a soft warm pink tint, the only pink page on offer. - Base { - id: "blush", - light: BaseTokens { - bg: "#FAF1F3", - surface: "#FFFFFF", - surface_2: "#F2E4E8", - text: "#2D1A20", - text_2: "#4A2F38", - muted: "#76606A", - border: "#E6D5DA", - border_strong: "#CFBAC1", - code_bg: "#F2E4E8", - private_bg: "#F2E4E8", - private_text: "#4A2F38", - ln_text: "#6C5660", - }, - dark: BaseTokens { - bg: "#150F12", - surface: "#1D1519", - surface_2: "#261C21", - text: "#F0E4E8", - text_2: "#D3C3C9", - muted: "#A3929A", - border: "#35282E", - border_strong: "#473840", - code_bg: "#261C21", - private_bg: "#261C21", - private_text: "#D3C3C9", - ln_text: "#A3929A", - }, - }, - // Graphite: a cool neutral grey, darker and flatter than Stone. - Base { - id: "graphite", - light: BaseTokens { - bg: "#E9EAEC", - surface: "#F9FAFB", - surface_2: "#DCDEE2", - text: "#17191C", - text_2: "#33373D", - muted: "#5C626B", - border: "#CFD3D9", - border_strong: "#B5BBC3", - code_bg: "#DCDEE2", - private_bg: "#DCDEE2", - private_text: "#33373D", - ln_text: "#545A63", - }, - dark: BaseTokens { - bg: "#1A1B1E", - surface: "#222428", - surface_2: "#2B2D32", - text: "#E6E8EB", - text_2: "#C8CCD2", - muted: "#9499A2", - border: "#33363C", - border_strong: "#44484F", - code_bg: "#2B2D32", - private_bg: "#2B2D32", - private_text: "#C8CCD2", - ln_text: "#9499A2", - }, - }, - // Pine: a teal-green tint, clearly bluer than Fern. + // Ink: crisp white in light, near-black in dark. Base { - id: "pine", + id: "ink", light: BaseTokens { - bg: "#E8F3F0", + bg: "#FCFCFB", surface: "#FFFFFF", - surface_2: "#DAEAE5", - text: "#13211E", - text_2: "#2E403C", - muted: "#5A6E69", - border: "#CCE0DA", - border_strong: "#B3CCC4", - code_bg: "#DAEAE5", - private_bg: "#DAEAE5", - private_text: "#2E403C", - ln_text: "#52675F", + surface_2: "#F2F2F0", + text: "#111111", + text_2: "#333333", + muted: "#5F5F5C", + border: "#E3E3E0", + border_strong: "#C9C9C5", + code_bg: "#F2F2F0", + private_bg: "#F2F2F0", + private_text: "#333333", + ln_text: "#5F5F5C", }, dark: BaseTokens { - bg: "#0A1614", - surface: "#11211E", - surface_2: "#182C28", - text: "#DDEEE9", - text_2: "#BAD2CB", - muted: "#8AA59E", - border: "#213A34", - border_strong: "#2F4C45", - code_bg: "#182C28", - private_bg: "#182C28", - private_text: "#BAD2CB", - ln_text: "#8AA59E", + bg: "#000000", + surface: "#0B0B0B", + surface_2: "#141414", + text: "#F2F2F0", + text_2: "#CFCFCC", + muted: "#9C9C98", + border: "#242424", + border_strong: "#353535", + code_bg: "#141414", + private_bg: "#141414", + private_text: "#CFCFCC", + ln_text: "#9C9C98", }, }, ]; diff --git a/crates/forge-web/src/custom_colour.rs b/crates/forge-web/src/custom_colour.rs new file mode 100644 index 0000000..006a6c3 --- /dev/null +++ b/crates/forge-web/src/custom_colour.rs @@ -0,0 +1,405 @@ +//! Colours for any hue. A stored `hue-N` id (see `accent::hue_of`) is not a row of +//! `accent_palette.rs`: its colours are built here from the OKLCH hue N, in the same roles a preset +//! has. The accent sits at one lightness and chroma, with black or white text by contrast. The +//! background is a neutral with a little chroma toward the hue. `stylesheet` writes the rules that +//! apply them, in the shape `build.rs` writes for the presets. + +/// The colours of one accent in one theme: the fields of `accent_palette::Tokens`, owned, because a +/// hue is built at run time. +#[derive(Debug, Clone)] +pub struct OwnedTokens { + pub accent: String, + pub accent_ink: String, + pub accent_soft: String, + pub accent_text: String, + pub focus: String, + pub public_bg: String, + pub public_text: String, + pub note: String, + pub hunk_bg: String, + pub hunk_text: String, +} + +impl OwnedTokens { + /// The `(property, value)` pairs, in the order of `accent_palette::TOKEN_VARS`. + pub fn decls(&self) -> Vec<(&'static str, &str)> { + vec![ + ("--accent", self.accent.as_str()), + ("--accent-ink", self.accent_ink.as_str()), + ("--accent-soft", self.accent_soft.as_str()), + ("--accent-text", self.accent_text.as_str()), + ("--focus", self.focus.as_str()), + ("--public-bg", self.public_bg.as_str()), + ("--public-text", self.public_text.as_str()), + ("--note", self.note.as_str()), + ("--hunk-bg", self.hunk_bg.as_str()), + ("--hunk-text", self.hunk_text.as_str()), + ] + } +} + +/// The neutral colours of one base in one theme: the fields of `accent_palette::BaseTokens`, owned. +#[derive(Debug, Clone)] +pub struct OwnedBaseTokens { + pub bg: String, + pub surface: String, + pub surface_2: String, + pub text: String, + pub text_2: String, + pub muted: String, + pub border: String, + pub border_strong: String, + pub code_bg: String, + pub private_bg: String, + pub private_text: String, + pub ln_text: String, +} + +impl OwnedBaseTokens { + /// The `(property, value)` pairs, in the order of `accent_palette::BASE_TOKEN_VARS`. + pub fn decls(&self) -> Vec<(&'static str, &str)> { + vec![ + ("--bg", self.bg.as_str()), + ("--surface", self.surface.as_str()), + ("--surface-2", self.surface_2.as_str()), + ("--text", self.text.as_str()), + ("--text-2", self.text_2.as_str()), + ("--muted", self.muted.as_str()), + ("--border", self.border.as_str()), + ("--border-strong", self.border_strong.as_str()), + ("--code-bg", self.code_bg.as_str()), + ("--private-bg", self.private_bg.as_str()), + ("--private-text", self.private_text.as_str()), + ("--ln-text", self.ln_text.as_str()), + ] + } +} + +/// Chroma of the neutrals: small, like the bases on offer. +const NEUTRAL_CHROMA: f64 = 0.012; + +/// Slack for float error when testing whether a colour is inside sRGB. +const GAMUT_EPS: f64 = 1e-6; + +/// The accent colours for a hue, in one theme. Light: a mid accent with its ink darker than it. +/// Dark: a light accent with its ink lighter than it, on a dark soft tint. +pub fn accent_tokens(hue: u16, dark: bool) -> OwnedTokens { + let h = f64::from(hue); + let (accent, ink, soft, hunk_bg) = if dark { + ( + colour(0.75, 0.11, h), + colour(0.84, 0.09, h), + colour(0.27, 0.04, h), + colour(0.22, 0.03, h), + ) + } else { + ( + colour(0.52, 0.12, h), + colour(0.46, 0.12, h), + colour(0.95, 0.025, h), + colour(0.975, 0.012, h), + ) + }; + OwnedTokens { + accent_text: text_on(&accent), + focus: if dark { accent.clone() } else { ink.clone() }, + hunk_text: if dark { + accent.clone() + } else { + colour(0.44, 0.10, h) + }, + public_bg: soft.clone(), + public_text: ink.clone(), + note: ink.clone(), + accent_ink: ink, + accent_soft: soft, + hunk_bg, + accent, + } +} + +/// The neutral colours for a hue, in one theme. Each is a tint of the hue at `NEUTRAL_CHROMA`, at +/// the lightness the presets use for that role. +pub fn base_tokens(hue: u16, dark: bool) -> OwnedBaseTokens { + let h = f64::from(hue); + let n = |l: f64| colour(l, NEUTRAL_CHROMA, h); + if dark { + let surface_2 = n(0.26); + let text_2 = n(0.82); + OwnedBaseTokens { + bg: n(0.19), + surface: n(0.225), + text: n(0.93), + muted: n(0.70), + border: n(0.30), + border_strong: n(0.38), + code_bg: surface_2.clone(), + private_bg: surface_2.clone(), + surface_2, + private_text: text_2.clone(), + text_2, + ln_text: n(0.70), + } + } else { + let surface_2 = n(0.945); + let text_2 = n(0.33); + OwnedBaseTokens { + bg: n(0.972), + surface: "#FFFFFF".to_owned(), + text: n(0.23), + muted: n(0.52), + border: n(0.89), + border_strong: n(0.82), + code_bg: surface_2.clone(), + private_bg: surface_2.clone(), + surface_2, + private_text: text_2.clone(), + text_2, + ln_text: n(0.50), + } + } +} + +/// The `#RRGGBB` of an OKLCH colour (lightness 0..1, chroma, hue in degrees). Chroma is cut back +/// until the colour fits sRGB, so the lightness and the hue hold. +pub fn colour(lightness: f64, chroma: f64, hue: f64) -> String { + let c = fit_chroma(lightness, chroma, hue); + let [r, g, b] = oklch_to_linear(lightness, c, hue) + .map(|v| (encode(v.clamp(0.0, 1.0)) * 255.0).round() as u8); + format!("#{r:02X}{g:02X}{b:02X}") +} + +/// The OKLCH hue of a `#RRGGBB` colour, rounded to a whole degree in `0..360`. +#[allow(dead_code)] // Used by the preset hues, which the settings picker reads. +pub fn hex_hue(hex: &str) -> u16 { + let (_, _, hue) = linear_to_oklch(hex_linear(hex)); + (hue.round() as u16) % 360 +} + +/// The WCAG contrast ratio between two `#RRGGBB` colours. +pub fn contrast_ratio(a: &str, b: &str) -> f64 { + let (x, y) = (relative_luminance(a), relative_luminance(b)); + (x.max(y) + 0.05) / (x.min(y) + 0.05) +} + +/// The rules for the custom ids of one stylesheet: each argument is the hue of an accent or base +/// id (`None` for a preset, which `build.rs` already styles). They mirror `build.rs`: the light +/// tokens go on the light attribute, the dark tokens on the dark attribute in both dark states. +pub fn stylesheet( + accent_light: Option, + accent_dark: Option, + base_light: Option, + base_dark: Option, +) -> String { + let mut css = String::new(); + if let Some(hue) = accent_light { + let selector = format!(":root[data-accent-light=\"hue-{hue}\"]"); + rule(&mut css, "", &selector, &accent_tokens(hue, false).decls()); + } + if let Some(hue) = accent_dark { + let selector = format!(":root[data-accent-dark=\"hue-{hue}\"]"); + dark_rules(&mut css, &selector, &accent_tokens(hue, true).decls()); + } + if let Some(hue) = base_light { + let selector = format!(":root[data-base-light=\"hue-{hue}\"]"); + rule(&mut css, "", &selector, &base_tokens(hue, false).decls()); + } + if let Some(hue) = base_dark { + let selector = format!(":root[data-base-dark=\"hue-{hue}\"]"); + dark_rules(&mut css, &selector, &base_tokens(hue, true).decls()); + } + css +} + +/// Appends `selector { … }` with one `--token: value;` line per declaration. `indent` is 0 or 2. +fn rule(css: &mut String, indent: &str, selector: &str, decls: &[(&str, &str)]) { + css.push_str(&format!("{indent}{selector} {{\n")); + for (name, value) in decls { + css.push_str(&format!("{indent} {name}: {value};\n")); + } + css.push_str(&format!("{indent}}}\n")); +} + +/// The dark rules for one choice: by the OS, and by `data-theme`. The `:not` and the second +/// attribute add specificity, so they win over the light rule whatever the light choice is. +fn dark_rules(css: &mut String, selector: &str, decls: &[(&str, &str)]) { + css.push_str("@media (prefers-color-scheme: dark) {\n"); + rule( + css, + " ", + &format!("{selector}:not([data-theme=\"light\"])"), + decls, + ); + css.push_str("}\n"); + rule(css, "", &format!("{selector}[data-theme=\"dark\"]"), decls); +} + +/// The text colour on a fill: white or near-black, whichever reads better. +fn text_on(fill: &str) -> String { + const WHITE: &str = "#FFFFFF"; + const INK: &str = "#111111"; + if contrast_ratio(WHITE, fill) >= contrast_ratio(INK, fill) { + WHITE.to_owned() + } else { + INK.to_owned() + } +} + +/// The largest chroma up to `chroma` that fits sRGB at this lightness and hue. +fn fit_chroma(lightness: f64, chroma: f64, hue: f64) -> f64 { + if in_gamut(lightness, chroma, hue) { + return chroma; + } + let (mut low, mut high) = (0.0, chroma); + for _ in 0..32 { + let mid = (low + high) / 2.0; + if in_gamut(lightness, mid, hue) { + low = mid; + } else { + high = mid; + } + } + low +} + +fn in_gamut(lightness: f64, chroma: f64, hue: f64) -> bool { + oklch_to_linear(lightness, chroma, hue) + .iter() + .all(|v| (-GAMUT_EPS..=1.0 + GAMUT_EPS).contains(v)) +} + +/// OKLCH to linear sRGB. A channel can fall outside `0..=1` when the colour is out of gamut. +fn oklch_to_linear(lightness: f64, chroma: f64, hue: f64) -> [f64; 3] { + let (sin, cos) = hue.to_radians().sin_cos(); + let a = chroma * cos; + let b = chroma * sin; + let l = lightness + 0.396_337_777_4 * a + 0.215_803_757_3 * b; + let m = lightness - 0.105_561_345_8 * a - 0.063_854_172_8 * b; + let s = lightness - 0.089_484_177_5 * a - 1.291_485_548 * b; + let (l, m, s) = (l * l * l, m * m * m, s * s * s); + [ + 4.076_741_662_1 * l - 3.307_711_591_3 * m + 0.230_969_929_2 * s, + -1.268_438_004_6 * l + 2.609_757_401_1 * m - 0.341_319_396_5 * s, + -0.004_196_086_3 * l - 0.703_418_614_7 * m + 1.707_614_701 * s, + ] +} + +/// Linear sRGB to OKLCH: lightness, chroma and hue in degrees. +#[allow(dead_code)] // Used by `hex_hue`, which the settings picker reads through the preset hues. +fn linear_to_oklch([r, g, b]: [f64; 3]) -> (f64, f64, f64) { + let l = (0.412_221_470_8 * r + 0.536_332_536_3 * g + 0.051_445_992_9 * b).cbrt(); + let m = (0.211_903_498_2 * r + 0.680_699_545_1 * g + 0.107_396_956_6 * b).cbrt(); + let s = (0.088_302_461_9 * r + 0.281_718_837_6 * g + 0.629_978_700_5 * b).cbrt(); + let lightness = 0.210_454_255_3 * l + 0.793_617_785 * m - 0.004_072_046_8 * s; + let a = 1.977_998_495_1 * l - 2.428_592_205 * m + 0.450_593_709_9 * s; + let b = 0.025_904_037_1 * l + 0.782_771_766_2 * m - 0.808_675_766 * s; + ( + lightness, + a.hypot(b), + b.atan2(a).to_degrees().rem_euclid(360.0), + ) +} + +fn relative_luminance(hex: &str) -> f64 { + let [r, g, b] = hex_linear(hex); + 0.2126 * r + 0.7152 * g + 0.0722 * b +} + +/// The linear sRGB channels of a `#RRGGBB` colour. Our own colours only, so a bad one is a bug. +fn hex_linear(hex: &str) -> [f64; 3] { + let channel = |i: usize| { + let byte = u8::from_str_radix(&hex[i..i + 2], 16).expect("a #RRGGBB colour"); + decode(f64::from(byte) / 255.0) + }; + [channel(1), channel(3), channel(5)] +} + +/// sRGB transfer function, encoded value to linear light. +fn decode(v: f64) -> f64 { + if v <= 0.040_45 { + v / 12.92 + } else { + ((v + 0.055) / 1.055).powf(2.4) + } +} + +/// Linear light to the sRGB encoded value. +fn encode(v: f64) -> f64 { + if v <= 0.003_130_8 { + 12.92 * v + } else { + 1.055 * v.powf(1.0 / 2.4) - 0.055 + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::accent_palette::{BASE_TOKEN_VARS, TOKEN_VARS}; + + fn is_hex_colour(value: &str) -> bool { + value.len() == 7 + && value.starts_with('#') + && value[1..].bytes().all(|b| b.is_ascii_hexdigit()) + } + + #[test] + fn the_extremes_are_exact() { + assert_eq!(colour(1.0, 0.0, 0.0), "#FFFFFF"); + assert_eq!(colour(0.0, 0.0, 0.0), "#000000"); + } + + #[test] + fn a_preset_hue_reads_back_from_its_colour() { + // Sage is about 149 degrees; Ocean about 240. + assert_eq!(hex_hue("#5E7D63"), 149); + assert_eq!(hex_hue("#467799"), 240); + } + + #[test] + fn every_hue_gives_hex_colours_with_the_right_token_names() { + for hue in 0..360 { + for dark in [false, true] { + let accent = accent_tokens(hue, dark); + let names: Vec<&str> = accent.decls().iter().map(|(n, _)| *n).collect(); + let expected: Vec<&str> = TOKEN_VARS.iter().map(|t| t.name).collect(); + assert_eq!(names, expected); + for (name, value) in accent.decls() { + assert!(is_hex_colour(value), "{hue} {dark} {name}: {value}"); + } + let base = base_tokens(hue, dark); + let names: Vec<&str> = base.decls().iter().map(|(n, _)| *n).collect(); + let expected: Vec<&str> = BASE_TOKEN_VARS.iter().map(|t| t.name).collect(); + assert_eq!(names, expected); + for (name, value) in base.decls() { + assert!(is_hex_colour(value), "{hue} {dark} {name}: {value}"); + } + } + } + } + + #[test] + fn the_stylesheet_keys_each_rule_on_its_hue() { + let css = stylesheet(Some(215), Some(215), None, Some(7)); + assert!( + css.contains(":root[data-accent-light=\"hue-215\"] {\n --accent: #"), + "{css}" + ); + assert!( + css.contains( + "@media (prefers-color-scheme: dark) {\n :root[data-accent-dark=\"hue-215\"]:not([data-theme=\"light\"]) {" + ), + "{css}" + ); + assert!( + css.contains(":root[data-accent-dark=\"hue-215\"][data-theme=\"dark\"] {"), + "{css}" + ); + assert!( + css.contains(":root[data-base-dark=\"hue-7\"][data-theme=\"dark\"] {"), + "{css}" + ); + assert!(!css.contains("data-base-light"), "{css}"); + assert!(stylesheet(None, None, None, None).is_empty()); + } +} diff --git a/crates/forge-web/src/i18n/accents.rs b/crates/forge-web/src/i18n/accents.rs index 0b6c075..b4b512e 100644 --- a/crates/forge-web/src/i18n/accents.rs +++ b/crates/forge-web/src/i18n/accents.rs @@ -9,48 +9,16 @@ pub fn accent_name(lang: Lang, id: &str) -> &'static str { match (id, lang) { ("sage", Lang::En) => "Sage", ("sage", Lang::Tr) => "Adaçayı", + ("spark", Lang::En) => "Spark", + ("spark", Lang::Tr) => "Kıvılcım", ("ocean", Lang::En) => "Ocean", ("ocean", Lang::Tr) => "Okyanus", ("plum", Lang::En) => "Plum", ("plum", Lang::Tr) => "Erik", ("rose", Lang::En) => "Rose", ("rose", Lang::Tr) => "Gül", - ("ochre", Lang::En) => "Ochre", - ("ochre", Lang::Tr) => "Toprak", - ("slate", Lang::En) => "Slate", - ("slate", Lang::Tr) => "Arduvaz", - ("teal", Lang::En) => "Teal", - ("teal", Lang::Tr) => "Camgöbeği", - ("moss", Lang::En) => "Moss", - ("moss", Lang::Tr) => "Yosun", - ("indigo", Lang::En) => "Indigo", - ("indigo", Lang::Tr) => "Çivit", - ("amber", Lang::En) => "Amber", - ("amber", Lang::Tr) => "Kehribar", - ("clay", Lang::En) => "Clay", - ("clay", Lang::Tr) => "Kil", - ("berry", Lang::En) => "Berry", - ("berry", Lang::Tr) => "Böğürtlen", - ("ember", Lang::En) => "Ember", - ("ember", Lang::Tr) => "Kor", - ("crimson", Lang::En) => "Crimson", - ("crimson", Lang::Tr) => "Kızıl", - ("coral", Lang::En) => "Coral", - ("coral", Lang::Tr) => "Mercan", ("gold", Lang::En) => "Gold", ("gold", Lang::Tr) => "Altın", - ("lime", Lang::En) => "Lime", - ("lime", Lang::Tr) => "Misket", - ("mint", Lang::En) => "Mint", - ("mint", Lang::Tr) => "Nane", - ("cyan", Lang::En) => "Cyan", - ("cyan", Lang::Tr) => "Turkuaz", - ("sky", Lang::En) => "Sky", - ("sky", Lang::Tr) => "Gökyüzü", - ("violet", Lang::En) => "Violet", - ("violet", Lang::Tr) => "Menekşe", - ("fuchsia", Lang::En) => "Fuchsia", - ("fuchsia", Lang::Tr) => "Fuşya", _ => "", } } @@ -61,26 +29,14 @@ pub fn base_name(lang: Lang, id: &str) -> &'static str { match (id, lang) { ("fern", Lang::En) => "Fern", ("fern", Lang::Tr) => "Eğrelti", - ("stone", Lang::En) => "Stone", - ("stone", Lang::Tr) => "Taş", - ("sand", Lang::En) => "Sand", - ("sand", Lang::Tr) => "Kum", - ("mist", Lang::En) => "Mist", - ("mist", Lang::Tr) => "Sis", + ("spark", Lang::En) => "Spark", + ("spark", Lang::Tr) => "Kıvılcım", ("ink", Lang::En) => "Ink", ("ink", Lang::Tr) => "Mürekkep", - ("dune", Lang::En) => "Dune", - ("dune", Lang::Tr) => "Kumul", - ("lilac", Lang::En) => "Lilac", - ("lilac", Lang::Tr) => "Leylak", ("dusk", Lang::En) => "Dusk", ("dusk", Lang::Tr) => "Alacakaranlık", - ("blush", Lang::En) => "Blush", - ("blush", Lang::Tr) => "Pudra", ("graphite", Lang::En) => "Graphite", ("graphite", Lang::Tr) => "Grafit", - ("pine", Lang::En) => "Pine", - ("pine", Lang::Tr) => "Çam", _ => "", } } diff --git a/crates/forge-web/src/main.rs b/crates/forge-web/src/main.rs index 75a5253..73c324c 100644 --- a/crates/forge-web/src/main.rs +++ b/crates/forge-web/src/main.rs @@ -24,6 +24,7 @@ mod boot; mod ci; mod client_ip; mod compression; +mod custom_colour; mod exporter; mod git_auth; mod git_http; diff --git a/crates/forge-web/src/pages/assets.rs b/crates/forge-web/src/pages/assets.rs index a6f4dea..9e8e2c1 100644 --- a/crates/forge-web/src/pages/assets.rs +++ b/crates/forge-web/src/pages/assets.rs @@ -1,13 +1,17 @@ -//! Serves the compiled stylesheet (build.rs output) at `/assets/main.css`, and the syntax -//! highlighting classes at `/assets/highlight.css`. +//! Serves the compiled stylesheet (build.rs output) at `/assets/main.css`, the syntax +//! highlighting classes at `/assets/highlight.css`, and the colours of the hue choices at +//! `/theme/custom.css`. use std::sync::OnceLock; use topcoat::Result; use topcoat::context::Cx; +use topcoat::router::request::uri; use topcoat::router::response::Response; use topcoat::router::{HeaderMap, StatusCode, header, route}; +use crate::accent; +use crate::custom_colour; use crate::render::highlight::highlight_css; const CSS: &str = include_str!(concat!(env!("OUT_DIR"), "/main.css")); @@ -81,6 +85,43 @@ async fn highlight_css_route() -> Result<(StatusCode, HeaderMap, &'static str)> Ok((StatusCode::OK, headers, highlight_css_text())) } +/// The colours of the hue choices on one page. The query names the four choices: `al` and `ad` for +/// the accent (light and dark), `bl` and `bd` for the base. A `hue-N` id makes its rules, and any +/// other value is ignored, so a bad link gets an empty stylesheet. The query sets the whole content, +/// so the response is immutable like the others. +#[route(GET "/theme/custom.css")] +async fn custom_css_route(cx: &Cx) -> Result<(StatusCode, HeaderMap, String)> { + let mut hues: [Option; 4] = [None; 4]; + if let Some(query) = uri(cx).query() { + for (key, value) in form_urlencoded::parse(query.as_bytes()) { + let slot = match key.as_ref() { + "al" => 0, + "ad" => 1, + "bl" => 2, + "bd" => 3, + _ => continue, + }; + hues[slot] = accent::hue_of(&value); + } + } + let [accent_light, accent_dark, base_light, base_dark] = hues; + let css = custom_colour::stylesheet(accent_light, accent_dark, base_light, base_dark); + let mut headers = HeaderMap::new(); + headers.insert( + header::CONTENT_TYPE, + "text/css; charset=utf-8" + .parse() + .expect("static header value"), + ); + headers.insert( + header::CACHE_CONTROL, + "public, max-age=31536000, immutable" + .parse() + .expect("static header value"), + ); + Ok((StatusCode::OK, headers, css)) +} + // The offline page (shown by the service worker when a page load finds the server down) and its // script. Not hashed and not immutable: the service worker fetches them again on each update. const OFFLINE_HTML: &str = include_str!("../../assets/offline.html"); @@ -391,4 +432,48 @@ mod tests { let body = to_bytes(res.into_body(), usize::MAX).await.unwrap(); assert!(!body.is_empty()); } + + #[tokio::test] + async fn custom_css_keys_the_hue_choices_and_ignores_the_rest() { + let router: Router = Router::builder().discover().cookies().build(); + let req = Request::builder() + .method("GET") + .uri("/theme/custom.css?al=hue-215&ad=plum&bl=hue-7&bd=hue-360") + .body(Body::empty()) + .unwrap(); + let res = router.handle(req).await; + assert_eq!(res.status(), 200); + assert_eq!(res.headers()["content-type"], "text/css; charset=utf-8"); + assert!( + res.headers()["cache-control"] + .to_str() + .unwrap() + .contains("immutable") + ); + let body = to_bytes(res.into_body(), usize::MAX).await.unwrap(); + let css = String::from_utf8(body.to_vec()).unwrap(); + assert!( + css.contains(":root[data-accent-light=\"hue-215\"] {"), + "{css}" + ); + assert!(css.contains(":root[data-base-light=\"hue-7\"] {"), "{css}"); + assert!(!css.contains("plum"), "{css}"); + assert!(!css.contains("hue-360"), "{css}"); + } + + #[tokio::test] + async fn custom_css_with_no_hue_is_empty_and_never_an_error() { + let router: Router = Router::builder().discover().cookies().build(); + for uri in ["/theme/custom.css", "/theme/custom.css?al=hue-x&bd=%00"] { + let req = Request::builder() + .method("GET") + .uri(uri) + .body(Body::empty()) + .unwrap(); + let res = router.handle(req).await; + assert_eq!(res.status(), 200, "{uri}"); + let body = to_bytes(res.into_body(), usize::MAX).await.unwrap(); + assert!(body.is_empty(), "{uri}"); + } + } } diff --git a/crates/forge-web/src/pages/settings_live.rs b/crates/forge-web/src/pages/settings_live.rs index c6dc0ce..bb33d35 100644 --- a/crates/forge-web/src/pages/settings_live.rs +++ b/crates/forge-web/src/pages/settings_live.rs @@ -54,7 +54,9 @@ async fn settings_events(cx: &Cx) -> Result { /// The appearance of a user's `` as `name=value` pairs joined by `;`. An empty value means /// the attribute is removed (the automatic theme). It mirrors the attributes `views::shell` sets, so -/// a tab that applies it shows what a fresh load of the page would show. +/// a tab that applies it shows what a fresh load of the page would show. The last pair, `custom_css`, +/// is the stylesheet of the hue choices (empty for presets only); `nav.js` applies only `data-*` +/// pairs, so it reads this one itself. /// The account state the alive stream compares: the appearance (see below) and the language /// preference, which changes the page text rather than its attributes. pub async fn account_state(state: &AppState, user_id: &str) -> Result<(String, String)> { @@ -70,12 +72,16 @@ pub async fn account_state(state: &AppState, user_id: &str) -> Result<(String, S } else { "left" }; + let accent_light = crate::accent::accent_id(&user.accent); + let accent_dark = crate::accent::accent_id(&user.accent_dark); + let base_light = crate::accent::base_id(&user.base_light); + let base_dark = crate::accent::base_id(&user.base_dark); + // The stylesheet of the hue choices, empty when there is none (see `views::shell`). + let custom_css = + crate::accent::custom_css_href(accent_light, accent_dark, base_light, base_dark) + .unwrap_or_default(); let appearance = format!( - "data-theme={theme};data-accent-light={};data-accent-dark={};data-base-light={};data-base-dark={};data-sidebar={sidebar}", - crate::accent::resolve(&user.accent).id, - crate::accent::resolve(&user.accent_dark).id, - crate::accent::resolve_base(&user.base_light).id, - crate::accent::resolve_base(&user.base_dark).id, + "data-theme={theme};data-accent-light={accent_light};data-accent-dark={accent_dark};data-base-light={base_light};data-base-dark={base_dark};data-sidebar={sidebar};custom_css={custom_css}", ); Ok((appearance, user.language.clone())) } diff --git a/crates/forge-web/src/views/mod.rs b/crates/forge-web/src/views/mod.rs index f34f1a8..e810e7c 100644 --- a/crates/forge-web/src/views/mod.rs +++ b/crates/forge-web/src/views/mod.rs @@ -211,10 +211,18 @@ pub async fn shell( crate::accent_palette::BASE_DEFAULT, ), }; - let accent_light_id = crate::accent::resolve(stored_accent_light).id; - let accent_dark_id = crate::accent::resolve(stored_accent_dark).id; - let base_light_id = crate::accent::resolve_base(stored_base_light).id; - let base_dark_id = crate::accent::resolve_base(stored_base_dark).id; + // A `hue-N` choice passes through as itself; an id forge does not offer shows the default. + let accent_light_id = crate::accent::accent_id(stored_accent_light); + let accent_dark_id = crate::accent::accent_id(stored_accent_dark); + let base_light_id = crate::accent::base_id(stored_base_light); + let base_dark_id = crate::accent::base_id(stored_base_dark); + // The stylesheet that styles the hue choices, when there is one. The presets need none. + let custom_css = crate::accent::custom_css_href( + accent_light_id, + accent_dark_id, + base_light_id, + base_dark_id, + ); // The side of the shell sidebar: `right` only when stored so, `left` otherwise. let sidebar_side = match viewer { Some((user, _)) if user.sidebar_side == "right" => "right", @@ -261,6 +269,9 @@ pub async fn shell( (full_title) + if let Some(href) = &custom_css { + + } if let Some(href) = extra_stylesheet { @@ -1370,11 +1381,11 @@ mod shell_tests { #[tokio::test] async fn the_html_tag_carries_the_viewers_colours() { - let tag = html_tag_for_colours(["ocean", "plum", "ink", "mist"]).await; + let tag = html_tag_for_colours(["ocean", "plum", "ink", "dusk"]).await; assert!(tag.contains("data-accent-light=\"ocean\""), "{tag}"); assert!(tag.contains("data-accent-dark=\"plum\""), "{tag}"); assert!(tag.contains("data-base-light=\"ink\""), "{tag}"); - assert!(tag.contains("data-base-dark=\"mist\""), "{tag}"); + assert!(tag.contains("data-base-dark=\"dusk\""), "{tag}"); assert!(!tag.contains("data-accent="), "{tag}"); // Stored ids that are no longer offered fall back to the defaults, never to nothing. let removed = html_tag_for_colours(["removed-palette", "removed-palette", "x", "x"]).await; @@ -1382,6 +1393,17 @@ mod shell_tests { assert!(removed.contains("data-accent-dark=\"sage\""), "{removed}"); assert!(removed.contains("data-base-light=\"fern\""), "{removed}"); assert!(removed.contains("data-base-dark=\"fern\""), "{removed}"); + // A removed preset (`ochre`, `mist`) falls back too, and a hue passes through as itself. + let retired = html_tag_for_colours(["ochre", "ember", "mist", "stone"]).await; + assert!(retired.contains("data-accent-light=\"sage\""), "{retired}"); + assert!(retired.contains("data-accent-dark=\"sage\""), "{retired}"); + assert!(retired.contains("data-base-light=\"fern\""), "{retired}"); + assert!(retired.contains("data-base-dark=\"fern\""), "{retired}"); + let hued = html_tag_for_colours(["hue-215", "hue-7", "hue-300", "spark"]).await; + assert!(hued.contains("data-accent-light=\"hue-215\""), "{hued}"); + assert!(hued.contains("data-accent-dark=\"hue-7\""), "{hued}"); + assert!(hued.contains("data-base-light=\"hue-300\""), "{hued}"); + assert!(hued.contains("data-base-dark=\"spark\""), "{hued}"); } #[tokio::test]