BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-base / src / theme / schemes.rs

schemes.rs

schemes.rs 18.3 KiB Download Raw
1use std::cell::Cell;
2use std::sync::atomic::{AtomicU8, AtomicUsize, Ordering};
3
4use super::kvt::KVT;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
7pub enum Shade {
8 #[default]
9 Dark,
10 Light,
11 HighContrast,
12}
13
14impl Shade {
15 pub const ALL: [Shade; 3] = [Shade::Dark, Shade::Light, Shade::HighContrast];
16
17 pub fn is_dark(self) -> bool {
18 !matches!(self, Shade::Light)
19 }
20
21 fn index(self) -> u8 {
22 match self {
23 Shade::Dark => 0,
24 Shade::Light => 1,
25 Shade::HighContrast => 2,
26 }
27 }
28
29 fn from_index(index: u8) -> Shade {
30 match index {
31 1 => Shade::Light,
32 2 => Shade::HighContrast,
33 _ => Shade::Dark,
34 }
35 }
36}
37
38#[derive(Debug, PartialEq, Eq)]
39pub struct Palette {
40 pub shade: Shade,
41 pub background: u32,
42 pub surface: u32,
43 pub surface_raised: u32,
44 pub row_hover: u32,
45 pub popup: u32,
46 pub popup_hover: u32,
47 pub popup_nested: u32,
48 pub popup_nested_hover: u32,
49 pub reply_surface: u32,
50 pub border: u32,
51 pub border_strong: u32,
52 pub foreground: u32,
53 pub text_strong: u32,
54 pub text_soft: u32,
55 pub text_muted: u32,
56 pub text_faint: u32,
57 pub accent: u32,
58 pub accent_text: u32,
59 pub accent_soft: u32,
60 pub accent_strong: u32,
61 pub on_accent: u32,
62 pub bubble_own: u32,
63 pub bubble_other: u32,
64 pub reaction_on_own: u32,
65 pub mention_background: u32,
66 pub mention_background_own: u32,
67 pub own_meta: u32,
68 pub own_read: u32,
69 pub drop_background: u32,
70 pub selection: u32,
71 pub rule_own: u32,
72 pub badge_muted: u32,
73 pub toast_mention: u32,
74 pub green: u32,
75 pub red: u32,
76 pub red_soft: u32,
77 pub red_tint: u32,
78 pub amber: u32,
79 pub out_of_office: u32,
80 pub close_button_hover: u32,
81 pub close_button_pressed: u32,
82 pub meeting_avatar: u32,
83 pub bot_avatar: u32,
84 pub avatar_palette: [u32; 6],
85 pub code_surface: u32,
86 pub code_header_border: u32,
87 pub inline_code_fill: u32,
88 pub inline_code_fill_own: u32,
89 pub quote_fill: u32,
90 pub inline_code_border: u32,
91 pub code_frame: u32,
92 pub table_header: u32,
93}
94
95#[derive(Debug, PartialEq, Eq)]
96pub struct ColorScheme {
97 pub id: &'static str,
98 pub name: &'static str,
99 pub dark: Palette,
100 pub light: Palette,
101}
102
103impl ColorScheme {
104 pub fn palette(&self, shade: Shade) -> &Palette {
105 match shade {
106 Shade::Dark => &self.dark,
107 Shade::Light => &self.light,
108 Shade::HighContrast => &HIGH_CONTRAST,
109 }
110 }
111}
112
113const AVATAR_PALETTE: [u32; 6] = [0x9a4a22, 0x44606e, 0x8a3434, 0x4f6a3c, 0x6a4f7a, 0x2f6b63];
114
115pub(super) const DARK_BASE: Palette = Palette {
116 shade: Shade::Dark,
117 background: 0,
118 surface: 0,
119 surface_raised: 0,
120 row_hover: 0,
121 popup: 0,
122 popup_hover: 0,
123 popup_nested: 0,
124 popup_nested_hover: 0,
125 reply_surface: 0,
126 border: 0,
127 border_strong: 0,
128 foreground: 0,
129 text_strong: 0,
130 text_soft: 0,
131 text_muted: 0,
132 text_faint: 0,
133 accent: 0,
134 accent_text: 0,
135 accent_soft: 0,
136 accent_strong: 0,
137 on_accent: 0,
138 bubble_own: 0,
139 bubble_other: 0,
140 reaction_on_own: 0,
141 mention_background: 0,
142 mention_background_own: 0,
143 own_meta: 0,
144 own_read: 0,
145 drop_background: 0,
146 selection: 0,
147 rule_own: 0,
148 badge_muted: 0,
149 toast_mention: 0,
150 green: 0,
151 red: 0,
152 red_soft: 0,
153 red_tint: 0,
154 amber: 0,
155 out_of_office: 0xc239b3,
156 close_button_hover: 0,
157 close_button_pressed: 0,
158 meeting_avatar: 0x4f52b2,
159 bot_avatar: 0x3a96dd,
160 avatar_palette: AVATAR_PALETTE,
161 code_surface: 0x0000003d,
162 code_header_border: 0xffffff0f,
163 inline_code_fill: 0xffffff14,
164 inline_code_fill_own: 0x0000003d,
165 quote_fill: 0x00000052,
166 inline_code_border: 0xffffff1a,
167 code_frame: 0xffffff3d,
168 table_header: 0xffffff0d,
169};
170
171pub(super) const LIGHT_BASE: Palette = Palette {
172 shade: Shade::Light,
173 code_surface: 0x0000000a,
174 code_header_border: 0x0000000f,
175 inline_code_fill: 0x0000000d,
176 inline_code_fill_own: 0x0000000f,
177 quote_fill: 0x0000000a,
178 inline_code_border: 0x00000014,
179 code_frame: 0x0000002e,
180 table_header: 0x00000008,
181 ..DARK_BASE
182};
183
184pub const RUSTY_TEAMS: ColorScheme = ColorScheme {
185 id: "rusty-teams",
186 name: "Rusty Teams",
187 dark: Palette {
188 background: 0x0f0f10,
189 surface: 0x161617,
190 surface_raised: 0x232325,
191 row_hover: 0x1c1c1e,
192 popup: 0x2c2c2f,
193 popup_hover: 0x38383c,
194 popup_nested: 0x3a3a3e,
195 popup_nested_hover: 0x47474c,
196 reply_surface: 0x19191b,
197 border: 0x262628,
198 border_strong: 0x3f3f42,
199 foreground: 0xf4f4f5,
200 text_strong: 0xe4e4e7,
201 text_soft: 0xd4d4d8,
202 text_muted: 0xa1a1a6,
203 text_faint: 0x8e8e93,
204 accent: 0xce6a3b,
205 accent_text: 0xe08a5c,
206 accent_soft: 0xf5c4a5,
207 accent_strong: 0xa84f27,
208 on_accent: 0x0f0f10,
209 bubble_own: 0x4f2a1b,
210 bubble_other: 0x232325,
211 reaction_on_own: 0x6b3a26,
212 mention_background: 0x3a2a23,
213 mention_background_own: 0x3b1d11,
214 own_meta: 0xc9a28c,
215 own_read: 0xf5c4a5,
216 drop_background: 0xce6a3b14,
217 selection: 0xce6a3b66,
218 rule_own: 0x6a3a25,
219 badge_muted: 0x3f3f42,
220 toast_mention: 0x2e1d15,
221 green: 0x22c55e,
222 red: 0xef4444,
223 red_soft: 0xf87171,
224 red_tint: 0xfca5a5,
225 amber: 0xfbbf24,
226 close_button_hover: 0xc42b1c,
227 close_button_pressed: 0xb5271a,
228 ..DARK_BASE
229 },
230 light: Palette {
231 background: 0xe9e9eb,
232 surface: 0xf4f4f5,
233 surface_raised: 0xffffff,
234 row_hover: 0xfafafa,
235 popup: 0xffffff,
236 popup_hover: 0xf1f1f2,
237 popup_nested: 0xffffff,
238 popup_nested_hover: 0xebebed,
239 reply_surface: 0xf9f9fa,
240 border: 0xe2e2e5,
241 border_strong: 0xd0d0d4,
242 foreground: 0x18181b,
243 text_strong: 0x27272a,
244 text_soft: 0x3f3f46,
245 text_muted: 0x5f5f66,
246 text_faint: 0x6e6e76,
247 accent: 0xb4501f,
248 accent_text: 0xa84a1d,
249 accent_soft: 0x8f3d17,
250 accent_strong: 0x93421d,
251 on_accent: 0xffffff,
252 bubble_own: 0xfae6da,
253 bubble_other: 0xffffff,
254 reaction_on_own: 0xf2cfb9,
255 mention_background: 0xfbebe1,
256 mention_background_own: 0xf2cfb9,
257 own_meta: 0x7a5444,
258 own_read: 0xa84a1d,
259 drop_background: 0xb4501f14,
260 selection: 0xb4501f40,
261 rule_own: 0xecc3a8,
262 badge_muted: 0xd0d0d4,
263 toast_mention: 0xfbebe1,
264 green: 0x16a34a,
265 red: 0xc81e1e,
266 red_soft: 0xb91c1c,
267 red_tint: 0x991b1b,
268 amber: 0xc27c0e,
269 close_button_hover: 0xc42b1c,
270 close_button_pressed: 0xb5271a,
271 ..LIGHT_BASE
272 },
273};
274
275pub const TEAMS: ColorScheme = ColorScheme {
276 id: "teams",
277 name: "Teams",
278 dark: Palette {
279 background: 0x0a0a0a,
280 surface: 0x1f1f1f,
281 surface_raised: 0x2e2e2e,
282 row_hover: 0x292929,
283 popup: 0x333333,
284 popup_hover: 0x3d3d3d,
285 popup_nested: 0x424242,
286 popup_nested_hover: 0x4d4d4d,
287 reply_surface: 0x242424,
288 border: 0x333333,
289 border_strong: 0x3d3d3d,
290 foreground: 0xffffff,
291 text_strong: 0xe0e0e0,
292 text_soft: 0xd6d6d6,
293 text_muted: 0xadadad,
294 text_faint: 0x999999,
295 accent: 0x5b5fc7,
296 accent_text: 0x7f85f5,
297 accent_soft: 0xaab1fa,
298 accent_strong: 0x444791,
299 on_accent: 0xffffff,
300 bubble_own: 0x2f2f4a,
301 bubble_other: 0x292929,
302 reaction_on_own: 0x383966,
303 mention_background: 0x2f2f4a,
304 mention_background_own: 0x3d3e78,
305 own_meta: 0xadadad,
306 own_read: 0xaab1fa,
307 drop_background: 0x5b5fc714,
308 selection: 0x5b5fc766,
309 rule_own: 0x444791,
310 badge_muted: 0x3d3d3d,
311 toast_mention: 0x2f2f4a,
312 green: 0x92c353,
313 red: 0xc4314b,
314 red_soft: 0xe37d80,
315 red_tint: 0xf1bbbc,
316 amber: 0xffaa44,
317 close_button_hover: 0xc4314b,
318 close_button_pressed: 0xa72037,
319 ..DARK_BASE
320 },
321 light: Palette {
322 background: 0xebebeb,
323 surface: 0xf5f5f5,
324 surface_raised: 0xffffff,
325 row_hover: 0xfafafa,
326 popup: 0xffffff,
327 popup_hover: 0xf5f5f5,
328 popup_nested: 0xffffff,
329 popup_nested_hover: 0xf0f0f0,
330 reply_surface: 0xfafafa,
331 border: 0xe0e0e0,
332 border_strong: 0xd1d1d1,
333 foreground: 0x242424,
334 text_strong: 0x333333,
335 text_soft: 0x424242,
336 text_muted: 0x616161,
337 text_faint: 0x707070,
338 accent: 0x5b5fc7,
339 accent_text: 0x5b5fc7,
340 accent_soft: 0x4f52b2,
341 accent_strong: 0x444791,
342 on_accent: 0xffffff,
343 bubble_own: 0xe8ebfa,
344 bubble_other: 0xffffff,
345 reaction_on_own: 0xdce0fa,
346 mention_background: 0xe8ebfa,
347 mention_background_own: 0xc5cbfa,
348 own_meta: 0x616161,
349 own_read: 0x4f52b2,
350 drop_background: 0x5b5fc714,
351 selection: 0x5b5fc740,
352 rule_own: 0xc5cbfa,
353 badge_muted: 0xd1d1d1,
354 toast_mention: 0xe8ebfa,
355 green: 0x13a10e,
356 red: 0xc4314b,
357 red_soft: 0xb10e1c,
358 red_tint: 0x8e192e,
359 amber: 0xc27c0e,
360 close_button_hover: 0xc4314b,
361 close_button_pressed: 0xa72037,
362 ..LIGHT_BASE
363 },
364};
365
366pub const HIGH_CONTRAST: Palette = Palette {
367 shade: Shade::HighContrast,
368 background: 0x000000,
369 surface: 0x000000,
370 surface_raised: 0x000000,
371 row_hover: 0x003b40,
372 popup: 0x000000,
373 popup_hover: 0x003b40,
374 popup_nested: 0x000000,
375 popup_nested_hover: 0x003b40,
376 reply_surface: 0x000000,
377 border: 0xffffff,
378 border_strong: 0xffffff,
379 foreground: 0xffffff,
380 text_strong: 0xffffff,
381 text_soft: 0xffffff,
382 text_muted: 0xffffff,
383 text_faint: 0x3ff23f,
384 accent: 0x1aebff,
385 accent_text: 0xffff00,
386 accent_soft: 0x1aebff,
387 accent_strong: 0x1aebff,
388 on_accent: 0x000000,
389 bubble_own: 0x002e33,
390 bubble_other: 0x000000,
391 reaction_on_own: 0x003b40,
392 mention_background: 0x000000,
393 mention_background_own: 0x000000,
394 own_meta: 0xffffff,
395 own_read: 0x1aebff,
396 drop_background: 0x1aebff26,
397 selection: 0x1aebff80,
398 rule_own: 0xffffff,
399 badge_muted: 0xffffff,
400 toast_mention: 0x000000,
401 green: 0x3ff23f,
402 red: 0xff4d4d,
403 red_soft: 0xff4d4d,
404 red_tint: 0xff4d4d,
405 amber: 0xffff00,
406 close_button_hover: 0xc4314b,
407 close_button_pressed: 0xa72037,
408 code_surface: 0x000000ff,
409 code_header_border: 0xffffffff,
410 inline_code_fill: 0x000000ff,
411 inline_code_fill_own: 0x000000ff,
412 quote_fill: 0x000000ff,
413 inline_code_border: 0xffffffff,
414 code_frame: 0xffffffff,
415 table_header: 0x000000ff,
416 ..DARK_BASE
417};
418
419/// The first entry is the default scheme (`default_scheme`, and the fallback for an unknown id).
420pub const SCHEMES: [&ColorScheme; 3] = [&KVT, &RUSTY_TEAMS, &TEAMS];
421
422static ACTIVE_SCHEME: AtomicUsize = AtomicUsize::new(0);
423static ACTIVE_SHADE: AtomicU8 = AtomicU8::new(0);
424
425thread_local! {
426 static THREAD_SCHEME: Cell<Option<usize>> = const { Cell::new(None) };
427 static THREAD_SHADE: Cell<Option<Shade>> = const { Cell::new(None) };
428}
429
430pub fn default_scheme() -> &'static ColorScheme {
431 SCHEMES[0]
432}
433
434pub fn scheme_by_id(id: &str) -> Option<&'static ColorScheme> {
435 SCHEMES.into_iter().find(|scheme| scheme.id == id)
436}
437
438fn position(id: &str) -> usize {
439 SCHEMES
440 .iter()
441 .position(|scheme| scheme.id == id)
442 .unwrap_or(0)
443}
444
445pub fn active_scheme() -> &'static ColorScheme {
446 let index = THREAD_SCHEME
447 .with(Cell::get)
448 .unwrap_or_else(|| ACTIVE_SCHEME.load(Ordering::Relaxed));
449 SCHEMES[index.min(SCHEMES.len() - 1)]
450}
451
452pub fn active_shade() -> Shade {
453 THREAD_SHADE
454 .with(Cell::get)
455 .unwrap_or_else(|| Shade::from_index(ACTIVE_SHADE.load(Ordering::Relaxed)))
456}
457
458pub fn active_palette() -> &'static Palette {
459 active_scheme().palette(active_shade())
460}
461
462pub fn set_active_scheme(id: &str) {
463 ACTIVE_SCHEME.store(position(id), Ordering::Relaxed);
464}
465
466pub fn set_active_shade(shade: Shade) {
467 ACTIVE_SHADE.store(shade.index(), Ordering::Relaxed);
468}
469
470pub fn set_thread_scheme(id: Option<&str>) {
471 THREAD_SCHEME.with(|cell| cell.set(id.map(position)));
472}
473
474pub fn set_thread_shade(shade: Option<Shade>) {
475 THREAD_SHADE.with(|cell| cell.set(shade));
476}
477
478pub fn palettes() -> impl Iterator<Item = (&'static ColorScheme, &'static Palette)> {
479 SCHEMES.into_iter().flat_map(|scheme| {
480 Shade::ALL
481 .into_iter()
482 .map(move |shade| (scheme, scheme.palette(shade)))
483 })
484}
485
486#[cfg(test)]
487mod tests {
488 use std::collections::HashSet;
489
490 use super::*;
491
492 fn channel(hex: u32, shift: u32) -> f64 {
493 let value = f64::from((hex >> shift) & 0xff) / 255.;
494 if value <= 0.04045 {
495 value / 12.92
496 } else {
497 ((value + 0.055) / 1.055).powf(2.4)
498 }
499 }
500
501 fn luminance(hex: u32) -> f64 {
502 0.2126 * channel(hex, 16) + 0.7152 * channel(hex, 8) + 0.0722 * channel(hex, 0)
503 }
504
505 fn contrast(first: u32, second: u32) -> f64 {
506 let (first, second) = (luminance(first), luminance(second));
507 (first.max(second) + 0.05) / (first.min(second) + 0.05)
508 }
509
510 #[test]
511 fn scheme_ids_and_names_are_unique() {
512 let ids: HashSet<_> = SCHEMES.iter().map(|scheme| scheme.id).collect();
513 let names: HashSet<_> = SCHEMES.iter().map(|scheme| scheme.name).collect();
514 assert_eq!(ids.len(), SCHEMES.len());
515 assert_eq!(names.len(), SCHEMES.len());
516 }
517
518 #[test]
519 fn koninklijke_van_twist_is_the_default() {
520 assert_eq!(default_scheme().id, "koninklijke-van-twist");
521 assert_eq!(default_scheme(), &KVT);
522 assert_eq!(scheme_by_id("rusty-teams"), Some(&RUSTY_TEAMS));
523 assert_eq!(scheme_by_id("teams"), Some(&TEAMS));
524 assert_eq!(scheme_by_id("solarized"), None);
525 }
526
527 #[test]
528 fn teams_scheme_uses_the_teams_brand_ramp_in_both_shades() {
529 assert_eq!(TEAMS.dark.accent_text, 0x7f85f5);
530 assert_eq!(TEAMS.dark.bubble_own, 0x2f2f4a);
531 assert_eq!(TEAMS.dark.surface, 0x1f1f1f);
532 assert_eq!(TEAMS.light.accent, 0x5b5fc7);
533 assert_eq!(TEAMS.light.bubble_own, 0xe8ebfa);
534 assert_eq!(TEAMS.light.foreground, 0x242424);
535 assert_eq!(TEAMS.light.background, 0xebebeb);
536 }
537
538 #[test]
539 fn every_palette_names_its_own_shade() {
540 for scheme in SCHEMES {
541 for shade in Shade::ALL {
542 assert_eq!(scheme.palette(shade).shade, shade, "{}", scheme.id);
543 }
544 }
545 }
546
547 #[test]
548 fn dark_palettes_layer_their_surfaces_from_dark_to_light() {
549 for scheme in SCHEMES {
550 let palette = &scheme.dark;
551 let layers = [
552 palette.background,
553 palette.surface,
554 palette.reply_surface,
555 palette.row_hover,
556 palette.surface_raised,
557 palette.popup,
558 palette.popup_hover,
559 ];
560 assert!(
561 layers.windows(2).all(|pair| pair[0] < pair[1]),
562 "{} surfaces are not ordered dark to light",
563 scheme.id
564 );
565 assert!(palette.popup_nested > palette.popup, "{}", scheme.id);
566 assert!(
567 palette.popup_nested_hover > palette.popup_nested,
568 "{}",
569 scheme.id
570 );
571 }
572 }
573
574 #[test]
575 fn light_palettes_lift_islands_and_flyouts_off_the_window_ground() {
576 for scheme in SCHEMES {
577 let palette = &scheme.light;
578 assert!(palette.background < palette.surface, "{}", scheme.id);
579 assert!(palette.surface < palette.reply_surface, "{}", scheme.id);
580 assert!(palette.surface < palette.popup, "{}", scheme.id);
581 assert_ne!(palette.popup_hover, palette.popup, "{}", scheme.id);
582 assert_ne!(
583 palette.popup_nested_hover, palette.popup_nested,
584 "{}",
585 scheme.id
586 );
587 assert_ne!(palette.row_hover, palette.surface, "{}", scheme.id);
588 }
589 }
590
591 #[test]
592 fn text_stays_readable_on_every_surface_of_every_palette() {
593 for (scheme, palette) in palettes() {
594 let grounds = [
595 palette.background,
596 palette.surface,
597 palette.reply_surface,
598 palette.row_hover,
599 palette.surface_raised,
600 palette.popup,
601 palette.bubble_other,
602 palette.bubble_own,
603 ];
604 let checks = [
605 ("foreground", palette.foreground, 4.5),
606 ("text_soft", palette.text_soft, 4.5),
607 ("text_muted", palette.text_muted, 3.),
608 ("text_faint", palette.text_faint, 2.5),
609 ("accent_text", palette.accent_text, 3.),
610 ];
611 for (name, text, minimum) in checks {
612 for ground in grounds {
613 let ratio = contrast(text, ground);
614 assert!(
615 ratio >= minimum,
616 "{} {:?}: {name} #{text:06x} on #{ground:06x} is {ratio:.2}",
617 scheme.id,
618 palette.shade
619 );
620 }
621 }
622 assert!(
623 contrast(palette.on_accent, palette.accent) >= 4.5,
624 "{} {:?}: on_accent",
625 scheme.id,
626 palette.shade
627 );
628 assert!(
629 contrast(palette.own_meta, palette.bubble_own) >= 3.,
630 "{} {:?}: own_meta",
631 scheme.id,
632 palette.shade
633 );
634 }
635 }
636
637 #[test]
638 fn a_thread_override_wins_over_the_process_scheme() {
639 set_thread_scheme(Some("teams"));
640 assert_eq!(active_scheme().id, "teams");
641 set_thread_shade(Some(Shade::Light));
642 assert_eq!(active_palette(), &TEAMS.light);
643 set_thread_shade(Some(Shade::HighContrast));
644 assert_eq!(active_palette(), &HIGH_CONTRAST);
645 set_thread_scheme(Some("unknown"));
646 assert_eq!(active_scheme().id, "koninklijke-van-twist");
647 set_thread_scheme(None);
648 set_thread_shade(None);
649 }
650}