1mod apply;
15
16use egui::{Color32, Stroke};
17
18pub use apply::{
19 apply, apply_id, apply_theme, apply_tokens, current, motion_enabled, set_reduced_motion, system_is_dark,
20};
21#[cfg(feature = "serde")]
22pub use crisol_core::theme::ThemeImportError;
23pub use crisol_core::theme::{ThemeId, ThemeMode, effective_id};
24
25use crisol_core::color::Rgba;
26
27pub const ANIMATION_TIME: f32 = crisol_core::theme::ANIMATION_TIME;
29
30fn color32(color: Rgba) -> Color32 {
32 let [r, g, b, a] = color.to_array();
33 Color32::from_rgba_premultiplied(r, g, b, a)
34}
35
36fn rgba(color: Color32) -> Rgba {
38 Rgba::from_array(color.to_array())
39}
40
41#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
43#[derive(Clone, Copy, Debug, PartialEq, Eq)]
44pub struct Theme {
45 pub background: Color32,
47 pub surface: Color32,
49 pub surface_raised: Color32,
51 pub sunken: Color32,
53 pub text: Color32,
55 pub text_muted: Color32,
57 pub accent: Color32,
59 pub accent_text: Color32,
61 pub border: Color32,
63 pub selection: Color32,
65 pub hover: Color32,
67 pub danger: Color32,
69 pub warning: Color32,
71 pub success: Color32,
73 pub done: Color32,
75 pub link: Color32,
77 pub scrollbar: Color32,
79 pub focus: Color32,
81}
82
83impl Theme {
84 pub fn light() -> Self {
86 Self::for_id(ThemeId::GithubLight)
87 }
88
89 pub fn dark() -> Self {
91 Self::for_id(ThemeId::GithubDark)
92 }
93
94 pub fn for_id(id: ThemeId) -> Self {
96 Self::from_core(crisol_core::theme::Theme::for_id(id))
97 }
98
99 pub fn to_core(self) -> crisol_core::theme::Theme {
101 let mut theme = crisol_core::theme::Theme::default();
102 theme.background = rgba(self.background);
103 theme.surface = rgba(self.surface);
104 theme.surface_raised = rgba(self.surface_raised);
105 theme.sunken = rgba(self.sunken);
106 theme.text = rgba(self.text);
107 theme.text_muted = rgba(self.text_muted);
108 theme.accent = rgba(self.accent);
109 theme.accent_text = rgba(self.accent_text);
110 theme.border = rgba(self.border);
111 theme.selection = rgba(self.selection);
112 theme.hover = rgba(self.hover);
113 theme.danger = rgba(self.danger);
114 theme.warning = rgba(self.warning);
115 theme.success = rgba(self.success);
116 theme.done = rgba(self.done);
117 theme.link = rgba(self.link);
118 theme.scrollbar = rgba(self.scrollbar);
119 theme.focus = rgba(self.focus);
120 theme
121 }
122
123 pub fn from_core(theme: crisol_core::theme::Theme) -> Self {
125 Self {
126 background: color32(theme.background),
127 surface: color32(theme.surface),
128 surface_raised: color32(theme.surface_raised),
129 sunken: color32(theme.sunken),
130 text: color32(theme.text),
131 text_muted: color32(theme.text_muted),
132 accent: color32(theme.accent),
133 accent_text: color32(theme.accent_text),
134 border: color32(theme.border),
135 selection: color32(theme.selection),
136 hover: color32(theme.hover),
137 danger: color32(theme.danger),
138 warning: color32(theme.warning),
139 success: color32(theme.success),
140 done: color32(theme.done),
141 link: color32(theme.link),
142 scrollbar: color32(theme.scrollbar),
143 focus: color32(theme.focus),
144 }
145 }
146
147 pub fn is_dark(&self) -> bool {
149 relative_luminance(self.background) < 0.5
150 }
151
152 pub fn with_accent(self, accent: Color32) -> Self {
154 Self::from_core(self.to_core().with_accent(rgba(accent)))
155 }
156
157 pub fn accessibility_issues(&self) -> Vec<(&'static str, f32)> {
159 self.to_core().accessibility_issues()
160 }
161
162 pub fn border_stroke(&self) -> Stroke {
164 Stroke::new(1.0, self.border)
165 }
166
167 pub fn tokens(&self) -> [(&'static str, Color32); 18] {
169 [
170 ("background", self.background),
171 ("surface", self.surface),
172 ("surface_raised", self.surface_raised),
173 ("sunken", self.sunken),
174 ("text", self.text),
175 ("text_muted", self.text_muted),
176 ("accent", self.accent),
177 ("accent_text", self.accent_text),
178 ("border", self.border),
179 ("selection", self.selection),
180 ("hover", self.hover),
181 ("danger", self.danger),
182 ("warning", self.warning),
183 ("success", self.success),
184 ("done", self.done),
185 ("link", self.link),
186 ("scrollbar", self.scrollbar),
187 ("focus", self.focus),
188 ]
189 }
190
191 pub fn high_contrast(self) -> Self {
193 Self::from_core(self.to_core().high_contrast())
194 }
195
196 pub fn ensure_aa(self) -> Self {
198 Self::from_core(self.to_core().ensure_aa())
199 }
200
201 pub fn to_css_variables(&self) -> String {
203 self.to_core().to_css_variables()
204 }
205
206 #[cfg(feature = "serde")]
211 pub fn from_vscode_json(text: &str) -> Result<Self, ThemeImportError> {
212 crisol_core::theme::Theme::from_vscode_json(text).map(Self::from_core)
213 }
214
215 #[cfg(feature = "serde")]
217 pub fn to_json(&self) -> serde_json::Value {
218 self.to_core().to_json()
219 }
220}
221
222pub fn mix(a: Color32, b: Color32, t: f32) -> Color32 {
224 color32(crisol_core::theme::mix(rgba(a), rgba(b), t))
225}
226
227pub fn relative_luminance(color: Color32) -> f32 {
229 crisol_core::theme::relative_luminance(rgba(color))
230}
231
232pub fn readable_on(fill: Color32, dark: Color32) -> Color32 {
234 color32(crisol_core::theme::readable_on(rgba(fill), rgba(dark)))
235}
236
237pub fn contrast_ratio(a: Color32, b: Color32) -> f32 {
239 crisol_core::theme::contrast_ratio(rgba(a), rgba(b))
240}
241
242#[cfg(test)]
243mod tests {
244 use super::*;
245
246 #[test]
247 fn every_theme_round_trips_through_the_core() {
248 for id in ThemeId::ALL {
249 let theme = Theme::for_id(id);
250 assert_eq!(Theme::from_core(theme.to_core()), theme, "{id:?}");
251 }
252 }
253
254 #[test]
255 fn every_theme_has_consistent_darkness() {
256 for id in ThemeId::ALL {
257 assert_eq!(Theme::for_id(id).is_dark(), id.mode() == ThemeMode::Dark, "{id:?}");
258 }
259 }
260
261 #[test]
262 fn contrast_meets_aa_for_primary_pairs() {
263 for id in ThemeId::ALL {
264 let theme = Theme::for_id(id);
265 for (foreground, background, label) in [
266 (theme.text, theme.background, "text/background"),
267 (theme.text, theme.surface, "text/surface"),
268 (theme.text_muted, theme.background, "muted/background"),
269 (theme.link, theme.background, "link/background"),
270 (theme.accent_text, theme.accent, "accent text/accent"),
271 (theme.danger, theme.background, "danger/background"),
272 ] {
273 let ratio = contrast_ratio(foreground, background);
274 assert!(ratio >= 4.5, "{id:?} {label}: {ratio:.2}");
275 }
276 assert!(contrast_ratio(theme.focus, theme.background) >= 3.0, "{id:?} focus");
277 }
278 }
279
280 #[test]
281 fn destructive_text_meets_aa_in_every_theme() {
282 for id in ThemeId::ALL {
283 let theme = Theme::for_id(id);
284 let text = readable_on(theme.danger, theme.background);
285 let ratio = contrast_ratio(text, theme.danger);
286 assert!(ratio >= 3.0, "{id:?} destructive: {ratio:.2}");
287 }
288 }
289
290 #[test]
291 fn built_in_themes_have_no_accessibility_issues() {
292 for id in ThemeId::ALL {
293 assert_eq!(Theme::for_id(id).accessibility_issues(), Vec::new(), "{id:?}");
294 }
295 }
296
297 #[test]
298 fn accents_keep_aa_contrast_on_every_base() {
299 let accents = [
300 Color32::from_rgb(0xff, 0xd7, 0x00),
301 Color32::from_rgb(0x00, 0x7a, 0xcc),
302 Color32::from_rgb(0xe0, 0x6c, 0x2b),
303 Color32::from_rgb(0x8a, 0x2b, 0xe2),
304 Color32::from_rgb(0x20, 0xc0, 0x60),
305 ];
306 for id in ThemeId::ALL {
307 for accent in accents {
308 let theme = Theme::for_id(id).with_accent(accent);
309 assert_eq!(theme.accessibility_issues(), Vec::new(), "{id:?} {accent:?}");
310 }
311 }
312 }
313
314 #[cfg(feature = "serde")]
315 #[test]
316 fn vscode_themes_import_and_export() {
317 let text = r##"{
318 "colors": {
319 "editor.background": "#1e1e1e",
320 "foreground": "#d4d4d4",
321 "focusBorder": "#007acc",
322 "list.activeSelectionBackground": "#264f78",
323 "descriptionForeground": "#9d9d9d"
324 }
325 }"##;
326 let theme = Theme::from_vscode_json(text).expect("import");
327 assert_eq!(theme.background, Color32::from_rgb(0x1e, 0x1e, 0x1e));
328 assert_eq!(theme.text, Color32::from_rgb(0xd4, 0xd4, 0xd4));
329 assert!(theme.accessibility_issues().is_empty());
330 let css = theme.to_css_variables();
331 assert!(css.contains("--wb-background: #1e1e1e;"));
332 assert_eq!(theme.to_json()["background"], "#1e1e1e");
333 assert!(Theme::from_vscode_json("{\"colors\":{}}").is_err());
334 }
335
336 #[test]
337 fn high_contrast_keeps_aa() {
338 for theme in [Theme::dark().high_contrast(), Theme::light().high_contrast()] {
339 assert!(theme.accessibility_issues().is_empty());
340 assert_eq!(
341 theme.background,
342 if theme.is_dark() {
343 Color32::BLACK
344 } else {
345 Color32::WHITE
346 }
347 );
348 }
349 }
350
351 #[test]
352 fn black_and_white_are_twenty_one_to_one() {
353 assert!((contrast_ratio(Color32::BLACK, Color32::WHITE) - 21.0).abs() < 0.01);
354 }
355
356 #[test]
357 fn modes_round_trip() {
358 for mode in [ThemeMode::Light, ThemeMode::Dark] {
359 assert_eq!(ThemeMode::from_setting(mode.as_setting()), mode);
360 }
361 }
362}