Skip to main content

mrk/
theme.rs

1use crate::document::Rgb;
2
3mod presets;
4
5pub use presets::{
6    CATPPUCCIN_LATTE, CATPPUCCIN_MOCHA, DRACULA, GITHUB_DARK, GITHUB_LIGHT, GRUVBOX_DARK, GRUVBOX_LIGHT, MRK_DARK, MRK_LIGHT, NORD,
7    TOKYO_NIGHT,
8};
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub enum Appearance {
12    Dark,
13    Light,
14}
15
16#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
17#[non_exhaustive]
18pub struct Palette {
19    pub text: Rgb,
20    pub muted: Rgb,
21    pub subtle: Rgb,
22    pub surface: Rgb,
23    pub accent: Rgb,
24    pub h1: Rgb,
25    pub h2: Rgb,
26    pub h3: Rgb,
27    pub link: Rgb,
28    pub code: Rgb,
29    pub success: Rgb,
30    pub note: Rgb,
31    pub tip: Rgb,
32    pub important: Rgb,
33    pub warning: Rgb,
34    pub caution: Rgb,
35}
36
37impl Palette {
38    /// Every colour of the palette, in the order the fields are declared.
39    pub fn colors(self) -> impl Iterator<Item = Rgb> {
40        let Self { text, muted, subtle, surface, accent, h1, h2, h3, link, code, success, note, tip, important, warning, caution } = self;
41        [text, muted, subtle, surface, accent, h1, h2, h3, link, code, success, note, tip, important, warning, caution].into_iter()
42    }
43}
44
45/// The syntax themes the presets use; `code` maps each onto the one embedded in two-face.
46#[derive(Clone, Copy, Debug, PartialEq, Eq)]
47#[non_exhaustive]
48pub enum SyntaxTheme {
49    CatppuccinLatte,
50    CatppuccinMacchiato,
51    CatppuccinMocha,
52    Dracula,
53    GitHub,
54    GruvboxDark,
55    GruvboxLight,
56    Nord,
57    OneHalfDark,
58    OneHalfLight,
59    TwoDark,
60}
61
62#[derive(Clone, Copy, Debug, PartialEq, Eq)]
63#[non_exhaustive]
64pub struct Theme {
65    pub name: &'static str,
66    pub appearance: Appearance,
67    pub palette: Palette,
68    pub syntax: SyntaxTheme,
69}
70
71/// Every built-in theme name, defaults first.
72pub fn names() -> impl Iterator<Item = &'static str> {
73    presets::ALL.iter().map(|theme| theme.name)
74}
75
76/// The built-in theme called `name`, ignoring case.
77pub fn find(name: &str) -> Option<Theme> {
78    presets::ALL.iter().find(|theme| theme.name.eq_ignore_ascii_case(name.trim())).copied()
79}
80
81pub fn default_for(appearance: Appearance) -> Theme {
82    match appearance {
83        Appearance::Dark => MRK_DARK,
84        Appearance::Light => MRK_LIGHT,
85    }
86}
87
88#[cfg(test)]
89mod tests {
90    #![allow(clippy::unwrap_used)]
91
92    use super::*;
93
94    fn relative_luminance(color: Rgb) -> f64 {
95        let channel = |value: u8| {
96            let value = f64::from(value) / 255.0;
97            if value <= 0.039_28 { value / 12.92 } else { ((value + 0.055) / 1.055).powf(2.4) }
98        };
99        0.2126 * channel(color.0) + 0.7152 * channel(color.1) + 0.0722 * channel(color.2)
100    }
101
102    fn contrast(left: Rgb, right: Rgb) -> f64 {
103        let (left, right) = (relative_luminance(left), relative_luminance(right));
104        (left.max(right) + 0.05) / (left.min(right) + 0.05)
105    }
106
107    fn all() -> impl Iterator<Item = Theme> {
108        names().map(|name| find(name).unwrap())
109    }
110
111    #[test]
112    fn every_requested_preset_exists() {
113        let expected = [
114            "mrk-dark",
115            "mrk-light",
116            "catppuccin-mocha",
117            "catppuccin-latte",
118            "tokyo-night",
119            "nord",
120            "dracula",
121            "gruvbox-dark",
122            "gruvbox-light",
123            "github-dark",
124            "github-light",
125        ];
126
127        assert_eq!(names().collect::<Vec<_>>(), expected);
128    }
129
130    #[test]
131    fn text_is_readable_on_surface() {
132        for theme in all() {
133            let ratio = contrast(theme.palette.text, theme.palette.surface);
134            assert!(ratio >= 4.5, "{}: text on surface is {ratio:.2}:1", theme.name);
135        }
136    }
137
138    #[test]
139    fn muted_stays_readable_and_subtle_stays_quiet() {
140        for theme in all() {
141            let Palette { text, muted, subtle, surface, .. } = theme.palette;
142            let (text, muted, subtle) = (contrast(text, surface), contrast(muted, surface), contrast(subtle, surface));
143            assert!(muted >= 3.5, "{}: muted on surface is {muted:.2}:1", theme.name);
144            assert!((1.2..2.5).contains(&subtle), "{}: subtle on surface is {subtle:.2}:1", theme.name);
145            assert!(text > muted && muted > subtle, "{}: text, muted, subtle must fade in order", theme.name);
146        }
147    }
148
149    #[test]
150    fn appearance_matches_the_surface_lightness() {
151        for theme in all() {
152            let is_light_surface = relative_luminance(theme.palette.surface) > 0.5;
153            assert_eq!(is_light_surface, theme.appearance == Appearance::Light, "{}", theme.name);
154        }
155    }
156
157    #[test]
158    fn find_ignores_case() {
159        assert_eq!(find("Catppuccin-Mocha").map(|theme| theme.name), Some("catppuccin-mocha"));
160        assert_eq!(find("nope"), None);
161    }
162}