Skip to main content

rouge_theme/
palette.rs

1use crate::Rgba;
2use serde::{Deserialize, Serialize};
3
4macro_rules! roles {
5    ($($role:ident: $name:ident),* $(,)?) => {
6        #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7        pub struct UiPalette<T = Rgba> {
8            $(pub $role: T,)*
9        }
10
11        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12        pub enum Role {
13            $($name,)*
14        }
15
16        impl<T> UiPalette<T> {
17            pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> UiPalette<U> {
18                UiPalette { $($role: f(self.$role),)* }
19            }
20
21            #[must_use]
22            pub const fn get(&self, role: Role) -> &T {
23                match role {
24                    $(Role::$name => &self.$role,)*
25                }
26            }
27        }
28
29        impl UiPalette<Role> {
30            pub const ROLES: Self = UiPalette { $($role: Role::$name,)* };
31        }
32    };
33}
34
35roles!(
36    canvas: Canvas,
37    surface: Surface,
38    surface_hover: SurfaceHover,
39    surface_selected: SurfaceSelected,
40    chrome: Chrome,
41    elevated: Elevated,
42    input: Input,
43    text: Text,
44    text_secondary: TextSecondary,
45    text_muted: TextMuted,
46    border: Border,
47    border_subtle: BorderSubtle,
48    accent: Accent,
49    accent_foreground: AccentForeground,
50    info: Info,
51    positive: Positive,
52    warning: Warning,
53    destructive: Destructive,
54    destructive_foreground: DestructiveForeground,
55    scrim: Scrim,
56);
57
58#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
59pub enum ButtonVariant {
60    Primary,
61    Secondary,
62    Destructive,
63    #[default]
64    Ghost,
65}
66#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
67pub enum InteractionState {
68    #[default]
69    Rest,
70    Hovered,
71    Disabled,
72}
73#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
74pub struct ControlState {
75    pub interaction: InteractionState,
76    pub selected: bool,
77}
78
79impl ControlState {
80    #[must_use]
81    pub const fn new(interaction: InteractionState) -> Self {
82        Self {
83            interaction,
84            selected: false,
85        }
86    }
87
88    #[must_use]
89    pub const fn selected(mut self, selected: bool) -> Self {
90        self.selected = selected;
91        self
92    }
93}
94#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
95pub enum SelectionState {
96    #[default]
97    None,
98    Selected,
99    Focused,
100    Disabled,
101}
102
103#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
104pub enum NoticeTone {
105    Neutral,
106    #[default]
107    Info,
108    Positive,
109    Warning,
110    Error,
111}
112#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
113pub enum ModalSize {
114    Compact,
115    #[default]
116    Medium,
117    Wide,
118}
119#[derive(Debug, Clone, Copy, PartialEq, Eq)]
120pub struct SemanticStyle<T = Rgba> {
121    pub foreground: T,
122    pub background: Option<T>,
123    pub emphasized: bool,
124}
125
126impl<T> SemanticStyle<T> {
127    pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> SemanticStyle<U> {
128        SemanticStyle {
129            foreground: f(self.foreground),
130            background: self.background.map(f),
131            emphasized: self.emphasized,
132        }
133    }
134}
135
136impl<T: Copy> UiPalette<T> {
137    #[must_use]
138    pub fn control_style(self, variant: ButtonVariant, state: ControlState) -> SemanticStyle<T> {
139        let (foreground, background) = match (variant, state.interaction, state.selected) {
140            (ButtonVariant::Primary, _, _) => (self.accent_foreground, Some(self.accent)),
141            (ButtonVariant::Destructive, InteractionState::Hovered, _) => {
142                (self.destructive, Some(self.surface_hover))
143            }
144            (ButtonVariant::Destructive, _, _) => (self.destructive, None),
145            (ButtonVariant::Ghost, _, true) => (self.accent, Some(self.surface_selected)),
146            (ButtonVariant::Secondary | ButtonVariant::Ghost, InteractionState::Hovered, _) => {
147                (self.text, Some(self.surface_hover))
148            }
149            (ButtonVariant::Secondary, _, _) => (self.text, Some(self.elevated)),
150            (ButtonVariant::Ghost, _, false) => (self.text_secondary, None),
151        };
152        SemanticStyle {
153            foreground,
154            background,
155            emphasized: state.selected,
156        }
157    }
158    #[must_use]
159    pub fn selection_style(self, state: SelectionState) -> SemanticStyle<T> {
160        match state {
161            SelectionState::None => SemanticStyle {
162                foreground: self.text,
163                background: Some(self.surface),
164                emphasized: false,
165            },
166            SelectionState::Selected => SemanticStyle {
167                foreground: self.accent,
168                background: Some(self.surface_selected),
169                emphasized: false,
170            },
171            SelectionState::Focused => SemanticStyle {
172                foreground: self.accent_foreground,
173                background: Some(self.accent),
174                emphasized: true,
175            },
176            SelectionState::Disabled => SemanticStyle {
177                foreground: self.text_muted,
178                background: Some(self.surface),
179                emphasized: false,
180            },
181        }
182    }
183    #[must_use]
184    pub fn notice_style(self, tone: NoticeTone) -> SemanticStyle<T> {
185        let foreground = match tone {
186            NoticeTone::Neutral => self.text_secondary,
187            NoticeTone::Info => self.info,
188            NoticeTone::Positive => self.positive,
189            NoticeTone::Warning => self.warning,
190            NoticeTone::Error => self.destructive,
191        };
192        SemanticStyle {
193            foreground,
194            background: None,
195            emphasized: matches!(tone, NoticeTone::Warning | NoticeTone::Error),
196        }
197    }
198}
199
200impl UiPalette {
201    #[must_use]
202    pub const fn for_dark(dark: bool) -> Self {
203        if dark { Self::dark() } else { Self::light() }
204    }
205
206    #[must_use]
207    pub const fn light() -> Self {
208        Self {
209            canvas: Rgba::rgb(0xf5f5f7),
210            surface: Rgba::rgb(0xf5f5f7),
211            surface_hover: Rgba::new(0, 0, 0, 13),
212            surface_selected: Rgba::new(0, 0, 0, 26),
213            chrome: Rgba::rgb(0xebebed),
214            elevated: Rgba::rgb(0xffffff),
215            input: Rgba::rgb(0xffffff),
216            text: Rgba::rgb(0x1d1d1f),
217            text_secondary: Rgba::rgb(0x424245),
218            text_muted: Rgba::rgb(0x6e6e73),
219            border: Rgba::rgb(0xd2d2d7),
220            border_subtle: Rgba::rgb(0xe5e5ea),
221            accent: Rgba::rgb(0x007653),
222            accent_foreground: Rgba::rgb(0xffffff),
223            info: Rgba::rgb(0x0a6cff),
224            positive: Rgba::rgb(0x248a3d),
225            warning: Rgba::rgb(0xb25000),
226            destructive: Rgba::rgb(0xd70015),
227            destructive_foreground: Rgba::rgb(0xffffff),
228            scrim: Rgba::new(0, 0, 0, 89),
229        }
230    }
231
232    #[must_use]
233    pub const fn dark() -> Self {
234        Self {
235            canvas: Rgba::rgb(0x1c1c1e),
236            surface: Rgba::rgb(0x1c1c1e),
237            surface_hover: Rgba::new(255, 255, 255, 18),
238            surface_selected: Rgba::new(255, 255, 255, 33),
239            chrome: Rgba::rgb(0x161618),
240            elevated: Rgba::rgb(0x2c2c2e),
241            input: Rgba::rgb(0x2c2c2e),
242            text: Rgba::rgb(0xf5f5f7),
243            text_secondary: Rgba::rgb(0xc7c7cc),
244            text_muted: Rgba::rgb(0x98989d),
245            border: Rgba::rgb(0x48484a),
246            border_subtle: Rgba::rgb(0x38383a),
247            accent: Rgba::rgb(0x158561),
248            accent_foreground: Rgba::rgb(0xffffff),
249            info: Rgba::rgb(0x0a84ff),
250            positive: Rgba::rgb(0x30d158),
251            warning: Rgba::rgb(0xff9f0a),
252            destructive: Rgba::rgb(0xff453a),
253            destructive_foreground: Rgba::rgb(0xffffff),
254            scrim: Rgba::new(0, 0, 0, 128),
255        }
256    }
257}
258
259impl Default for UiPalette {
260    fn default() -> Self {
261        Self::light()
262    }
263}