use anyhow::{Result, bail}; use ratatui::style::Color; use serde::{Deserialize, Serialize}; use std::str::FromStr; #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "kebab-case")] pub enum Theme { Dark, Monokai, #[default] Spotifyish, Nord, Gruvbox, Daylight, Paper, } pub struct Palette { pub bg: Color, pub panel: Color, pub accent: Color, pub muted: Color, pub text: Color, pub selection: Color, pub border: Color, } impl Theme { pub fn name(self) -> &'static str { match self { Self::Dark => "dark", Self::Monokai => "monokai", Self::Spotifyish => "spotifyish", Self::Nord => "nord", Self::Gruvbox => "gruvbox", Self::Daylight => "daylight", Self::Paper => "paper", } } pub fn next(self) -> Self { match self { Self::Dark => Self::Monokai, Self::Monokai => Self::Spotifyish, Self::Spotifyish => Self::Nord, Self::Nord => Self::Gruvbox, Self::Gruvbox => Self::Daylight, Self::Daylight => Self::Paper, Self::Paper => Self::Dark, } } /// Shared semantic colors keep all widgets consistent when switching themes. pub fn palette(self) -> Palette { let (bg, panel, accent, muted, text, selection) = match self { Self::Dark => (0x10141c, 0x171d28, 0x64dcc2, 0x909bb1, 0xe0e7f1, 0x263a46), Self::Monokai => (0x272822, 0x34352f, 0xf92672, 0xa6a28c, 0xf8f8f2, 0x49483e), Self::Spotifyish => (0x121212, 0x202020, 0x1ed760, 0xb3b3b3, 0xffffff, 0x303d34), Self::Nord => (0x2e3440, 0x3b4252, 0x88c0d0, 0xa4b0c3, 0xeceff4, 0x4c566a), Self::Gruvbox => (0x282828, 0x3c3836, 0xfabd2f, 0xbdae93, 0xebdbb2, 0x504945), Self::Daylight => (0xffffff, 0xeef2f6, 0x004899, 0x384555, 0x111827, 0xdbe8ff), Self::Paper => (0xfff8e8, 0xf3ead4, 0x733510, 0x504538, 0x251e16, 0xe5d6b7), }; let rgb = |hex: u32| Color::Rgb((hex >> 16) as u8, (hex >> 8) as u8, hex as u8); Palette { bg: rgb(bg), panel: rgb(panel), accent: rgb(accent), muted: rgb(muted), text: rgb(text), selection: rgb(selection), border: rgb(match self { Self::Daylight => 0x526070, Self::Paper => 0x6b5c48, Self::Gruvbox => 0x665c54, _ => panel, }), } } } impl FromStr for Theme { type Err = anyhow::Error; fn from_str(value: &str) -> Result { match value.to_ascii_lowercase().as_str() { "dark" => Ok(Self::Dark), "monokai" => Ok(Self::Monokai), "spotifyish" => Ok(Self::Spotifyish), "nord" => Ok(Self::Nord), "gruvbox" => Ok(Self::Gruvbox), "daylight" => Ok(Self::Daylight), "paper" => Ok(Self::Paper), _ => { bail!( "Unknown theme. Choose dark, monokai, spotifyish, nord, gruvbox, daylight, or paper." ) } } } } #[cfg(test)] mod tests { use super::*; #[test] fn theme_cycle_includes_light_themes_and_roundtrips_settings() { let mut theme = Theme::default(); let mut names = std::collections::HashSet::new(); loop { assert!(names.insert(theme.name())); assert_eq!(theme.name().parse::().unwrap(), theme); assert_eq!( serde_json::from_str::(&serde_json::to_string(&theme).unwrap()).unwrap(), theme ); theme = theme.next(); if theme == Theme::default() { break; } } assert!(names.contains("gruvbox")); assert!(names.contains("daylight")); assert!(names.contains("paper")); } #[test] fn outdoor_themes_keep_text_keys_and_borders_readable() { let luminance = |color: Color| { let Color::Rgb(r, g, b) = color else { panic!("Expected RGB") }; let linear = |value: u8| { let v = f64::from(value) / 255.; if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) } }; 0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b) }; let contrast = |a, b| { let (a, b) = (luminance(a), luminance(b)); (a.max(b) + 0.05) / (a.min(b) + 0.05) }; for theme in [Theme::Daylight, Theme::Paper] { let palette = theme.palette(); for background in [palette.bg, palette.panel, palette.selection] { for foreground in [palette.text, palette.accent, palette.muted] { assert!( contrast(foreground, background) >= 4.5, "{} has low text contrast", theme.name() ); } } assert!(contrast(palette.border, palette.bg) >= 3.); } } #[test] fn only_current_theme_name_is_accepted() { let theme: Theme = serde_json::from_str("\"spotifyish\"").unwrap(); assert_eq!("spotifyish".parse::().unwrap(), theme); assert_eq!(serde_json::to_string(&theme).unwrap(), "\"spotifyish\""); for removed in ["spotify", "spotify-ish"] { assert!(removed.parse::().is_err()); assert!(serde_json::from_str::(&format!("\"{removed}\"")).is_err()); } } }