BlueGit

ict rusty-teams

Files

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

schemes.rs

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