1mod palette;
17mod studio;
18
19use crate::color::Rgba;
20
21pub use palette::ThemeId;
22#[cfg(feature = "serde")]
23pub use studio::ThemeImportError;
24
25pub const ANIMATION_TIME: f32 = 0.10;
32
33#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
35#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
36#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
37#[non_exhaustive]
38pub enum ThemeMode {
44 #[default]
46 Light,
47 Dark,
49}
50
51impl ThemeMode {
52 pub fn from_setting(value: &str) -> Self {
54 if value == "dark" { Self::Dark } else { Self::Light }
55 }
56
57 pub fn as_setting(self) -> &'static str {
59 match self {
60 Self::Light => "light",
61 Self::Dark => "dark",
62 }
63 }
64}
65
66#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
68#[derive(Clone, Copy, Debug, PartialEq, Eq)]
69#[non_exhaustive]
70pub struct Theme {
76 pub background: Rgba,
78 pub surface: Rgba,
80 pub surface_raised: Rgba,
82 pub sunken: Rgba,
84 pub text: Rgba,
86 pub text_muted: Rgba,
88 pub accent: Rgba,
90 pub accent_text: Rgba,
92 pub border: Rgba,
94 pub selection: Rgba,
96 pub hover: Rgba,
98 pub danger: Rgba,
100 pub warning: Rgba,
102 pub success: Rgba,
104 pub done: Rgba,
106 pub link: Rgba,
108 pub scrollbar: Rgba,
110 pub focus: Rgba,
112}
113
114impl Default for Theme {
115 fn default() -> Self {
116 Self::light()
117 }
118}
119
120impl Theme {
121 pub fn light() -> Self {
123 palette::theme(ThemeId::GithubLight)
124 }
125
126 pub fn dark() -> Self {
128 palette::theme(ThemeId::GithubDark)
129 }
130
131 pub fn for_id(id: ThemeId) -> Self {
133 palette::theme(id)
134 }
135
136 pub fn is_dark(&self) -> bool {
138 relative_luminance(self.background) < 0.5
139 }
140
141 pub fn with_accent(mut self, accent: Rgba) -> Self {
153 let towards = if self.is_dark() { Rgba::WHITE } else { Rgba::BLACK };
154 self.accent = accent;
155 self.focus = until_contrast(accent, self.background, towards, 3.0);
156 self.link = until_contrast(accent, self.background, towards, 4.5);
157 self.selection = mix(self.background, accent, if self.is_dark() { 0.35 } else { 0.28 });
158 self.accent_text = if contrast_ratio(Rgba::WHITE, accent) >= contrast_ratio(Rgba::BLACK, accent) {
160 Rgba::WHITE
161 } else {
162 Rgba::BLACK
163 };
164 if contrast_ratio(self.accent_text, self.accent) < 4.5 {
165 let away = if self.accent_text == Rgba::WHITE {
167 Rgba::BLACK
168 } else {
169 Rgba::WHITE
170 };
171 self.accent = until_contrast(self.accent, self.accent_text, away, 4.5);
172 }
173 self
174 }
175
176 pub fn accessibility_issues(&self) -> Vec<(&'static str, f32)> {
179 let mut issues = Vec::new();
180 for (label, foreground, background, minimum) in [
181 ("text on background", self.text, self.background, 4.5),
182 ("text on surface", self.text, self.surface, 4.5),
183 ("muted text on background", self.text_muted, self.background, 4.5),
184 ("link on background", self.link, self.background, 4.5),
185 ("accent text on accent", self.accent_text, self.accent, 4.5),
186 ("danger on background", self.danger, self.background, 4.5),
187 ("focus on background", self.focus, self.background, 3.0),
188 ] {
189 let ratio = contrast_ratio(foreground, background);
190 if ratio < minimum {
191 issues.push((label, ratio));
192 }
193 }
194 issues
195 }
196
197 pub fn tokens(&self) -> [(&'static str, Rgba); 18] {
201 [
202 ("background", self.background),
203 ("surface", self.surface),
204 ("surface_raised", self.surface_raised),
205 ("sunken", self.sunken),
206 ("text", self.text),
207 ("text_muted", self.text_muted),
208 ("accent", self.accent),
209 ("accent_text", self.accent_text),
210 ("border", self.border),
211 ("selection", self.selection),
212 ("hover", self.hover),
213 ("danger", self.danger),
214 ("warning", self.warning),
215 ("success", self.success),
216 ("done", self.done),
217 ("link", self.link),
218 ("scrollbar", self.scrollbar),
219 ("focus", self.focus),
220 ]
221 }
222}
223
224pub fn effective_id(id: ThemeId, follow_system: bool, system_dark: bool) -> ThemeId {
231 if follow_system {
232 id.follow_system(system_dark)
233 } else {
234 id
235 }
236}
237
238pub fn mix(a: Rgba, b: Rgba, t: f32) -> Rgba {
245 a.mix(b, t)
246}
247
248pub(crate) fn until_contrast(color: Rgba, background: Rgba, towards: Rgba, minimum: f32) -> Rgba {
251 let mut candidate = color;
252 for step in 0..=20 {
253 candidate = mix(color, towards, step as f32 / 20.0);
254 if contrast_ratio(candidate, background) >= minimum {
255 break;
256 }
257 }
258 candidate
259}
260
261pub fn relative_luminance(color: Rgba) -> f32 {
268 color.relative_luminance()
269}
270
271pub fn readable_on(fill: Rgba, dark: Rgba) -> Rgba {
278 if contrast_ratio(Rgba::WHITE, fill) >= contrast_ratio(dark, fill) {
279 Rgba::WHITE
280 } else {
281 dark
282 }
283}
284
285pub fn contrast_ratio(a: Rgba, b: Rgba) -> f32 {
292 let (first, second) = (relative_luminance(a), relative_luminance(b));
293 let (light, dark) = if first > second {
294 (first, second)
295 } else {
296 (second, first)
297 };
298 (light + 0.05) / (dark + 0.05)
299}
300
301#[cfg(test)]
302mod tests {
303 use super::*;
304
305 #[test]
306 fn every_theme_has_consistent_darkness() {
307 for id in ThemeId::ALL {
308 assert_eq!(Theme::for_id(id).is_dark(), id.mode() == ThemeMode::Dark, "{id:?}");
309 }
310 }
311
312 #[test]
313 fn contrast_meets_aa_for_primary_pairs() {
314 for id in ThemeId::ALL {
315 let theme = Theme::for_id(id);
316 for (foreground, background, label) in [
317 (theme.text, theme.background, "text/background"),
318 (theme.text, theme.surface, "text/surface"),
319 (theme.text_muted, theme.background, "muted/background"),
320 (theme.link, theme.background, "link/background"),
321 (theme.accent_text, theme.accent, "accent text/accent"),
322 (theme.danger, theme.background, "danger/background"),
323 ] {
324 let ratio = contrast_ratio(foreground, background);
325 assert!(ratio >= 4.5, "{id:?} {label}: {ratio:.2}");
326 }
327 assert!(contrast_ratio(theme.focus, theme.background) >= 3.0, "{id:?} focus");
328 }
329 }
330
331 #[test]
332 fn destructive_text_meets_aa_in_every_theme() {
333 for id in ThemeId::ALL {
334 let theme = Theme::for_id(id);
335 let text = readable_on(theme.danger, theme.background);
336 let ratio = contrast_ratio(text, theme.danger);
337 assert!(ratio >= 3.0, "{id:?} destructive: {ratio:.2}");
338 }
339 }
340
341 #[test]
342 fn built_in_themes_have_no_accessibility_issues() {
343 for id in ThemeId::ALL {
344 assert_eq!(Theme::for_id(id).accessibility_issues(), Vec::new(), "{id:?}");
345 }
346 }
347
348 #[test]
349 fn accents_keep_aa_contrast_on_every_base() {
350 let accents = [
351 Rgba::from_rgb(0xff, 0xd7, 0x00),
352 Rgba::from_rgb(0x00, 0x7a, 0xcc),
353 Rgba::from_rgb(0xe0, 0x6c, 0x2b),
354 Rgba::from_rgb(0x8a, 0x2b, 0xe2),
355 Rgba::from_rgb(0x20, 0xc0, 0x60),
356 ];
357 for id in ThemeId::ALL {
358 for accent in accents {
359 let theme = Theme::for_id(id).with_accent(accent);
360 assert_eq!(theme.accessibility_issues(), Vec::new(), "{id:?} {accent:?}");
361 }
362 }
363 }
364
365 #[cfg(feature = "serde")]
366 #[test]
367 fn vscode_themes_import_and_export() {
368 let text = r##"{
369 "colors": {
370 "editor.background": "#1e1e1e",
371 "foreground": "#d4d4d4",
372 "focusBorder": "#007acc",
373 "list.activeSelectionBackground": "#264f78",
374 "descriptionForeground": "#9d9d9d"
375 }
376 }"##;
377 let theme = Theme::from_vscode_json(text).expect("import");
378 assert_eq!(theme.background, Rgba::from_rgb(0x1e, 0x1e, 0x1e));
379 assert_eq!(theme.text, Rgba::from_rgb(0xd4, 0xd4, 0xd4));
380 assert!(theme.accessibility_issues().is_empty());
381 let css = theme.to_css_variables();
382 assert!(css.contains("--wb-background: #1e1e1e;"));
383 assert_eq!(theme.to_json()["background"], "#1e1e1e");
384 assert!(Theme::from_vscode_json("{\"colors\":{}}").is_err());
385 }
386
387 #[test]
388 fn high_contrast_keeps_aa() {
389 for theme in [Theme::dark().high_contrast(), Theme::light().high_contrast()] {
390 assert!(theme.accessibility_issues().is_empty());
391 assert_eq!(
392 theme.background,
393 if theme.is_dark() { Rgba::BLACK } else { Rgba::WHITE }
394 );
395 }
396 }
397
398 #[test]
399 fn black_and_white_are_twenty_one_to_one() {
400 assert!((contrast_ratio(Rgba::BLACK, Rgba::WHITE) - 21.0).abs() < 0.01);
401 }
402
403 #[test]
404 fn modes_round_trip() {
405 for mode in [ThemeMode::Light, ThemeMode::Dark] {
406 assert_eq!(ThemeMode::from_setting(mode.as_setting()), mode);
407 }
408 }
409
410 #[test]
411 fn tokens_cover_every_field() {
412 let theme = Theme::light();
413 assert_eq!(theme.tokens().len(), 18);
414 assert_eq!(theme.tokens()[0], ("background", theme.background));
415 assert_eq!(theme.tokens()[17], ("focus", theme.focus));
416 }
417}