//! Color schemes: the nine palettes the user can pick, the cookie that remembers the //! choice, and the swatch colors the picker shows. The palettes themselves are CSS //! custom properties in `styles.css`, keyed by ``. use topcoat::{ context::Cx, cookie::{Cookie, Cookies, SameSite, cookies, time::Duration}, }; /// Name of the cookie that stores the color scheme. pub const SCHEME_COOKIE: &str = "scheme"; /// How long the color scheme choice is remembered. pub const SCHEME_COOKIE_DAYS: i64 = 365; /// A color scheme. Each one is a palette of light and dark tokens in `styles.css`. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Scheme { Graphite, Cobalt, Oxblood, Teal, Plum, Mono, Tangerine, Sage, Spark, } /// Every scheme, in the order the picker lists them. pub const ALL: [Scheme; 9] = [ Scheme::Graphite, Scheme::Cobalt, Scheme::Oxblood, Scheme::Teal, Scheme::Plum, Scheme::Mono, Scheme::Tangerine, Scheme::Sage, Scheme::Spark, ]; /// The scheme used when no valid cookie is set. pub const DEFAULT: Scheme = Scheme::Sage; impl Scheme { /// The value stored in the cookie and in `data-scheme` on ``. pub fn id(self) -> &'static str { match self { Self::Graphite => "graphite", Self::Cobalt => "cobalt", Self::Oxblood => "oxblood", Self::Teal => "teal", Self::Plum => "plum", Self::Mono => "mono", Self::Tangerine => "tangerine", Self::Sage => "sage", Self::Spark => "spark", } } /// Parses a cookie or form value. Returns `None` for unknown schemes. pub fn from_id(id: &str) -> Option { ALL.into_iter().find(|s| s.id() == id) } /// The proper name of the scheme. Names are not translated. pub fn name(self) -> &'static str { match self { Self::Graphite => "Graphite & Highlighter", Self::Cobalt => "Cobalt Ink", Self::Oxblood => "Oxblood", Self::Teal => "Teal Ledger", Self::Plum => "Plum", Self::Mono => "Monochrome", Self::Tangerine => "Tangerine", Self::Sage => "Sage", Self::Spark => "Spark", } } /// Three swatch colors as `(light, dark)` pairs: the sidebar, the accent, and the /// active row. The picker draws them as dots. pub fn dots(self) -> [(&'static str, &'static str); 3] { match self { // @dots:begin Self::Graphite => [("#eceef2", "#1d2026"), ("#f2b600", "#ffc93c"), ("#ffe08a", "#5c4700")], Self::Cobalt => [("#eef3fb", "#121a2c"), ("#1f4fd1", "#5b8bff"), ("#dce7ff", "#1d2f5c")], Self::Oxblood => [("#eeeae8", "#1c1818"), ("#8e1b22", "#d85c63"), ("#f8dcd9", "#4a1d21")], Self::Teal => [("#e8f0f0", "#121d1c"), ("#0f5e5a", "#3fb3a6"), ("#cdeee9", "#15403b")], Self::Plum => [("#efeaf3", "#1a1620"), ("#7b2d6e", "#d27ac4"), ("#f1d9ee", "#4a2446")], Self::Mono => [("#f0f0f0", "#161616"), ("#111111", "#f2f2f2"), ("#e4e4e4", "#2e2e2e")], Self::Tangerine => [("#eceef0", "#16191c"), ("#f59e0b", "#ffb347"), ("#fde9c4", "#4f3412")], Self::Sage => [("#e9efe6", "#111211"), ("#567752", "#4a6847"), ("#d9ebd3", "#233024")], Self::Spark => [("#eeeef0", "#1a1512"), ("#ff8205", "#ff9a2e"), ("#ffe8c7", "#58320a")], // @dots:end } } } /// The scheme of this request. The `scheme` cookie wins; an unknown or missing value /// means the default. pub fn scheme(cx: &Cx) -> Scheme { cookies(cx) .get(SCHEME_COOKIE) .and_then(|cookie| Scheme::from_id(cookie.value())) .unwrap_or(DEFAULT) } /// The cookie that remembers `scheme` for a year. pub fn scheme_cookie(scheme: Scheme) -> Cookie<'static> { Cookie::build((SCHEME_COOKIE, scheme.id())) .path("/") .same_site(SameSite::Lax) .max_age(Duration::days(SCHEME_COOKIE_DAYS)) .build() }