BlueGit

ict rusty-teams

Files

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

schemes.rs

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