//! The app's palette. //! //! One shell colour scheme: near-black surfaces tinted blue, brass for what the user acts on, //! and mint or coral only for results. Champion art and victory banners are the loudest things //! on screen, so the chrome around them stays quiet. //! //! Every colour is OKLCH, written as lightness, chroma and hue. The two appearances share the //! hues and swap lightness, so a control keeps its identity in light mode. use rune::tok::Theme; use rune::{Color, oklch, oklcha}; /// Hues, one per role. Neutrals take a touch of the shell's blue; text keeps the same cast. const SHELL: f32 = 268.0; const BRASS: f32 = 84.0; const MINT: f32 = 157.0; const CORAL: f32 = 22.0; pub struct Palette { /// Surfaces and ink: what rune's widgets read. pub theme: Theme, /// The window chrome: title bar and status bar, a step above or below the page. pub chrome: Color, /// A field's well, sunk into the card it sits in. pub field: Color, /// What the user acts on: primary buttons, sliders, the selected tab. pub accent: Color, /// Ink for text on an accent fill. pub accent_ink: Color, pub success: Color, pub danger: Color, pub warning: Color, } pub fn palette(dark: bool) -> Palette { if dark { Palette { theme: Theme { bg: oklch(0.168, 0.012, SHELL), card: oklch(0.212, 0.014, SHELL), card_border: oklcha(1.0, 0.0, 0.0, 0.055), text: oklch(0.962, 0.003, SHELL), text_secondary: oklch(0.734, 0.012, SHELL), separator: oklcha(1.0, 0.0, 0.0, 0.05), control_track: oklcha(1.0, 0.0, 0.0, 0.045), control_off: oklcha(1.0, 0.0, 0.0, 0.09), indicator: oklch(0.44, 0.01, SHELL), knob: oklch(0.985, 0.0, 0.0), hover: oklcha(1.0, 0.0, 0.0, 0.018), press: oklcha(1.0, 0.0, 0.0, 0.035), shadow: oklcha(0.0, 0.0, 0.0, 0.45), shadow_soft: oklcha(0.0, 0.0, 0.0, 0.3), edge: oklcha(0.0, 0.0, 0.0, 0.32), knob_shadow: oklcha(0.0, 0.0, 0.0, 0.32), }, chrome: oklch(0.192, 0.013, SHELL), field: oklch(0.178, 0.012, SHELL), accent: oklch(0.818, 0.116, BRASS), accent_ink: oklch(0.245, 0.035, BRASS), success: oklch(0.782, 0.132, MINT), danger: oklch(0.706, 0.152, CORAL), warning: oklch(0.812, 0.115, 72.0), } } else { Palette { theme: Theme { bg: oklch(0.966, 0.004, SHELL), card: oklch(1.0, 0.0, 0.0), card_border: oklcha(0.2, 0.02, SHELL, 0.1), text: oklch(0.235, 0.012, SHELL), text_secondary: oklch(0.515, 0.013, SHELL), separator: oklcha(0.2, 0.02, SHELL, 0.12), control_track: oklcha(0.2, 0.01, SHELL, 0.13), control_off: oklcha(0.2, 0.01, SHELL, 0.22), indicator: oklch(1.0, 0.0, 0.0), knob: oklch(1.0, 0.0, 0.0), hover: oklcha(0.2, 0.01, SHELL, 0.06), press: oklcha(0.2, 0.01, SHELL, 0.11), shadow: oklcha(0.2, 0.02, SHELL, 0.16), shadow_soft: oklcha(0.2, 0.02, SHELL, 0.07), edge: oklcha(0.2, 0.02, SHELL, 0.2), knob_shadow: oklcha(0.2, 0.02, SHELL, 0.3), }, chrome: oklch(0.988, 0.003, SHELL), field: oklch(0.955, 0.004, SHELL), accent: oklch(0.535, 0.104, 74.0), accent_ink: oklch(1.0, 0.0, 0.0), success: oklch(0.512, 0.122, MINT), danger: oklch(0.55, 0.165, CORAL), warning: oklch(0.52, 0.112, 68.0), } } } #[cfg(test)] mod tests { use super::*; /// Relative luminance of a linear-light colour, as WCAG defines contrast. fn luminance(c: Color) -> f64 { 0.2126 * c.r as f64 + 0.7152 * c.g as f64 + 0.0722 * c.b as f64 } fn contrast(a: Color, b: Color) -> f64 { let (x, y) = (luminance(a), luminance(b)); let (hi, lo) = if x > y { (x, y) } else { (y, x) }; (hi + 0.05) / (lo + 0.05) } /// Every colour that carries text keeps WCAG AA (4.5:1) on the surfaces it is drawn on, /// in both appearances. The palette is tuned by hand, so this is the guard that a nudge /// to one number did not make a label unreadable. #[test] fn ink_reads_on_the_surfaces() { for dark in [true, false] { let p = palette(dark); let mode = if dark { "dark" } else { "light" }; for (name, ink) in [ ("text", p.theme.text), ("text_secondary", p.theme.text_secondary), ("accent", p.accent), ("success", p.success), ("danger", p.danger), ("warning", p.warning), ] { for (surface, bg) in [ ("bg", p.theme.bg), ("card", p.theme.card), ("chrome", p.chrome), ] { let ratio = contrast(ink, bg); assert!( ratio >= 4.5, "{mode}: {name} on {surface} is {ratio:.2}:1, under 4.5:1" ); } } let ratio = contrast(p.accent, p.accent_ink); assert!( ratio >= 4.5, "{mode}: accent_ink on accent is {ratio:.2}:1, under 4.5:1" ); } } /// A field's well has to be visible inside the card it sits in. #[test] fn the_field_well_stands_out_from_the_card() { for dark in [true, false] { let p = palette(dark); let ratio = contrast(p.field, p.theme.card); assert!(ratio >= 1.04, "{dark}: field vs card is {ratio:.2}:1"); } } }