1use ratatui::style::{Color, Style};
15
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub enum ColorSupport {
23 TrueColor,
24 Ansi256,
25 Ansi16,
26}
27
28impl ColorSupport {
29 pub fn detect() -> Self {
33 if let Some(value) = std::env::var_os("COLORTERM") {
34 let value = value.to_string_lossy();
35 if value.contains("truecolor") || value.contains("24bit") {
36 return Self::TrueColor;
37 }
38 }
39 if let Some(value) = std::env::var_os("TERM") {
40 let value = value.to_string_lossy();
41 if value.contains("256color") {
42 return Self::Ansi256;
43 }
44 if value.contains("direct") {
45 return Self::TrueColor;
46 }
47 }
48 match ratatui::crossterm::style::available_color_count() {
49 u16::MAX => Self::TrueColor,
50 256 => Self::Ansi256,
51 _ => Self::Ansi16,
52 }
53 }
54
55 pub const NAMES: &'static str = "auto, truecolor, 256, 16";
56
57 pub fn parse(value: &str) -> Option<Self> {
59 match value.to_ascii_lowercase().as_str() {
60 "auto" => Some(Self::detect()),
61 "truecolor" | "24bit" => Some(Self::TrueColor),
62 "256" | "ansi256" => Some(Self::Ansi256),
63 "16" | "ansi16" => Some(Self::Ansi16),
64 _ => None,
65 }
66 }
67}
68
69#[derive(Clone, Copy, Debug, PartialEq, Eq)]
71pub struct Palette {
72 pub dim: Color,
74 pub dim_selected: Color,
76 pub highlight_bg: Color,
81 pub accent: Color,
83 pub bar: Color,
85 pub ok: Color,
87 pub warn: Color,
89 pub crit: Color,
91 pub focus: Color,
93 pub anon: Color,
95 pub file: Color,
97 pub shmem: Color,
99 pub swap: Color,
101 pub swap_pss: Color,
103 pub tile_fg: Color,
105 pub treemap: [Color; 6],
107}
108
109impl Palette {
110 pub const DEFAULT: Self = Self {
112 dim: Color::DarkGray,
113 dim_selected: Color::Gray,
114 highlight_bg: Color::Rgb(38, 79, 120),
115 accent: Color::Cyan,
116 bar: Color::Cyan,
117 ok: Color::Green,
118 warn: Color::Yellow,
119 crit: Color::Red,
120 focus: Color::Yellow,
121 anon: Color::Magenta,
122 file: Color::Cyan,
123 shmem: Color::Yellow,
124 swap: Color::LightBlue,
125 swap_pss: Color::LightBlue,
126 tile_fg: Color::Gray,
127 treemap: [
128 Color::Rgb(46, 64, 94),
129 Color::Rgb(60, 47, 82),
130 Color::Rgb(36, 72, 72),
131 Color::Rgb(82, 62, 42),
132 Color::Rgb(52, 72, 46),
133 Color::Rgb(78, 47, 58),
134 ],
135 };
136
137 pub fn highlight(&self) -> Style {
138 Style::new().bg(self.highlight_bg)
139 }
140
141 pub fn dim(&self, selected: bool) -> Color {
143 if selected {
144 self.dim_selected
145 } else {
146 self.dim
147 }
148 }
149
150 pub fn rendered(self, support: ColorSupport) -> Self {
152 Self {
153 dim: render_color(self.dim, support),
154 dim_selected: render_color(self.dim_selected, support),
155 highlight_bg: render_color(self.highlight_bg, support),
156 accent: render_color(self.accent, support),
157 bar: render_color(self.bar, support),
158 ok: render_color(self.ok, support),
159 warn: render_color(self.warn, support),
160 crit: render_color(self.crit, support),
161 focus: render_color(self.focus, support),
162 anon: render_color(self.anon, support),
163 file: render_color(self.file, support),
164 shmem: render_color(self.shmem, support),
165 swap: render_color(self.swap, support),
166 swap_pss: render_color(self.swap_pss, support),
167 tile_fg: render_color(self.tile_fg, support),
168 treemap: self.treemap.map(|color| render_color(color, support)),
169 }
170 }
171}
172
173const fn rgb(r: u8, g: u8, b: u8) -> Color {
176 Color::Rgb(r, g, b)
177}
178
179fn render_color(color: Color, support: ColorSupport) -> Color {
181 match (support, color) {
182 (ColorSupport::TrueColor, color) => color,
183 (ColorSupport::Ansi256, Color::Rgb(r, g, b)) => Color::Indexed(rgb_to_256(r, g, b)),
184 (ColorSupport::Ansi16, Color::Rgb(r, g, b)) => nearest_ansi16(r, g, b),
185 (_, color) => color,
186 }
187}
188
189fn rgb_to_256(r: u8, g: u8, b: u8) -> u8 {
192 let (rf, gf, bf) = (r as f64, g as f64, b as f64);
193 if (rf / 11.0).round() == (gf / 11.0).round() && (gf / 11.0).round() == (bf / 11.0).round() {
194 (232.0 + (rf / 11.0).round()).min(255.0) as u8
195 } else {
196 (16.0 + (rf / 51.0).round() * 36.0 + (gf / 51.0).round() * 6.0 + (bf / 51.0).round()) as u8
197 }
198}
199
200#[rustfmt::skip]
201const ANSI16_RGB: [(u8, u8, u8); 16] = [
202 (0, 0, 0), (128, 0, 0), (0, 128, 0), (128, 128, 0),
203 (0, 0, 128), (128, 0, 128), (0, 128, 128), (192, 192, 192),
204 (85, 85, 85), (255, 0, 0), (0, 255, 0), (255, 255, 0),
205 (0, 0, 255), (255, 0, 255), (0, 255, 255), (255, 255, 255),
206];
207
208#[rustfmt::skip]
209const ANSI16_COLORS: [Color; 16] = [
210 Color::Black, Color::Red, Color::Green, Color::Yellow,
211 Color::Blue, Color::Magenta, Color::Cyan, Color::Gray,
212 Color::DarkGray, Color::LightRed, Color::LightGreen, Color::LightYellow,
213 Color::LightBlue, Color::LightMagenta, Color::LightCyan, Color::White,
214];
215
216fn nearest_ansi16(r: u8, g: u8, b: u8) -> Color {
217 let mut best = 0;
218 let mut best_distance = u32::MAX;
219 for (index, (r2, g2, b2)) in ANSI16_RGB.into_iter().enumerate() {
220 let distance = (i32::from(r) - i32::from(r2)).pow(2) as u32
221 + (i32::from(g) - i32::from(g2)).pow(2) as u32
222 + (i32::from(b) - i32::from(b2)).pow(2) as u32;
223 if distance < best_distance {
224 best_distance = distance;
225 best = index;
226 }
227 }
228 ANSI16_COLORS[best]
229}
230
231#[derive(Clone, Copy, Debug, PartialEq, Eq)]
233pub struct Theme {
234 pub name: &'static str,
235 pub palette: Palette,
236}
237
238impl Theme {
239 pub const DEFAULT: Self = Self {
240 name: "default",
241 palette: Palette::DEFAULT,
242 };
243
244 pub const BTOP: Self = Self {
247 name: "btop",
248 palette: Palette {
249 dim: rgb(0x40, 0x40, 0x40),
250 dim_selected: rgb(0xee, 0xee, 0xee),
251 highlight_bg: rgb(0x6a, 0x2f, 0x2f),
252 accent: rgb(0xee, 0xee, 0xee),
253 bar: rgb(0x80, 0xd0, 0xa3),
254 ok: rgb(0x77, 0xca, 0x9b),
255 warn: rgb(0xcb, 0xc0, 0x6c),
256 crit: rgb(0xdc, 0x4c, 0x4c),
257 focus: rgb(0xb5, 0x40, 0x40),
258 anon: rgb(0xff, 0x47, 0x69),
259 file: rgb(0x26, 0xc5, 0xff),
260 shmem: rgb(0xff, 0xb8, 0x14),
261 swap: rgb(0xb0, 0xa9, 0xde),
262 swap_pss: rgb(0xb0, 0xa9, 0xde),
263 tile_fg: rgb(0x22, 0x22, 0x22),
264 treemap: [
265 rgb(0xff, 0x47, 0x69),
266 rgb(0x26, 0xc5, 0xff),
267 rgb(0xff, 0xb8, 0x14),
268 rgb(0xdc, 0xff, 0x85),
269 rgb(0xb0, 0xa9, 0xde),
270 rgb(0xdc, 0xaf, 0xde),
271 ],
272 },
273 };
274
275 pub const DRACULA: Self = Self {
279 name: "dracula",
280 palette: Palette {
281 dim: rgb(0x44, 0x47, 0x5a),
282 dim_selected: rgb(0xf8, 0xf8, 0xf2),
283 highlight_bg: rgb(0xff, 0x79, 0xc6),
284 accent: rgb(0xf8, 0xf8, 0xf2),
285 bar: rgb(0x50, 0xfa, 0x7b),
286 ok: rgb(0x50, 0xfa, 0x7b),
287 warn: rgb(0xf1, 0xfa, 0x8c),
288 crit: rgb(0xff, 0x55, 0x55),
289 focus: rgb(0x62, 0x72, 0xa4),
290 anon: rgb(0x0d, 0xfa, 0x49),
291 file: rgb(0x26, 0xd7, 0xfd),
292 shmem: rgb(0xff, 0x9c, 0x33),
293 swap: rgb(0xbd, 0x93, 0xf9),
294 swap_pss: rgb(0xbd, 0x93, 0xf9),
295 tile_fg: rgb(0x28, 0x2a, 0x36),
296 treemap: [
297 rgb(0x50, 0xfa, 0x7b),
298 rgb(0xbd, 0x93, 0xf9),
299 rgb(0xff, 0x79, 0xc6),
300 rgb(0x8b, 0xe9, 0xfd),
301 rgb(0xff, 0x9c, 0x33),
302 rgb(0xff, 0x33, 0xa8),
303 ],
304 },
305 };
306
307 pub const GRUVBOX: Self = Self {
311 name: "gruvbox-dark",
312 palette: Palette {
313 dim: rgb(0x7c, 0x6f, 0x64),
314 dim_selected: rgb(0xfa, 0xbd, 0x2f),
315 highlight_bg: rgb(0x3c, 0x38, 0x36),
316 accent: rgb(0xeb, 0xdb, 0xb2),
317 bar: rgb(0xb8, 0xbb, 0x26),
318 ok: rgb(0xb8, 0xbb, 0x26),
319 warn: rgb(0xfa, 0xbd, 0x2f),
320 crit: rgb(0xfb, 0x49, 0x34),
321 focus: rgb(0xd7, 0x99, 0x21),
322 anon: rgb(0xfb, 0x49, 0x34),
323 file: rgb(0x83, 0xa5, 0x98),
324 shmem: rgb(0xfa, 0xbd, 0x2f),
325 swap: rgb(0xa3, 0xa8, 0xf7),
326 swap_pss: rgb(0xa3, 0xa8, 0xf7),
327 tile_fg: rgb(0x1d, 0x20, 0x21),
328 treemap: [
329 rgb(0xb8, 0xbb, 0x26),
330 rgb(0x83, 0xa5, 0x98),
331 rgb(0xfa, 0xbd, 0x2f),
332 rgb(0x98, 0x97, 0x1a),
333 rgb(0xa3, 0xa8, 0xf7),
334 rgb(0xd3, 0x86, 0x9b),
335 ],
336 },
337 };
338
339 pub const KANAGAWA: Self = Self {
342 name: "kanagawa-wave",
343 palette: Palette {
344 dim: rgb(0x72, 0x71, 0x69),
345 dim_selected: rgb(0xdc, 0xa5, 0x61),
346 highlight_bg: rgb(0x22, 0x32, 0x49),
347 accent: rgb(0xdc, 0xd7, 0xba),
348 bar: rgb(0x98, 0xbb, 0x6c),
349 ok: rgb(0x98, 0xbb, 0x6c),
350 warn: rgb(0xdc, 0xa5, 0x61),
351 crit: rgb(0xe8, 0x24, 0x24),
352 focus: rgb(0xc3, 0x40, 0x43),
353 anon: rgb(0x7f, 0xb4, 0xca),
354 file: rgb(0xff, 0x9e, 0x3b),
355 shmem: rgb(0x98, 0xbb, 0x6c),
356 swap: rgb(0x95, 0x7f, 0xbb),
357 swap_pss: rgb(0x95, 0x7f, 0xbb),
358 tile_fg: rgb(0x16, 0x16, 0x1d),
359 treemap: [
360 rgb(0x98, 0xbb, 0x6c),
361 rgb(0xc0, 0xa3, 0x6e),
362 rgb(0x93, 0x8a, 0xa9),
363 rgb(0xe8, 0x24, 0x24),
364 rgb(0x7e, 0x9c, 0xdb),
365 rgb(0xdc, 0xa5, 0x61),
366 ],
367 },
368 };
369
370 pub const LIGHT: Self = Self {
372 name: "light",
373 palette: Palette {
374 dim: Color::DarkGray,
375 dim_selected: Color::Black,
376 highlight_bg: Color::Rgb(191, 219, 254),
377 accent: Color::Blue,
378 bar: Color::Blue,
379 ok: Color::Green,
380 warn: Color::Rgb(146, 64, 14),
382 crit: Color::Red,
383 focus: Color::Blue,
384 anon: Color::Magenta,
385 file: Color::Blue,
386 shmem: Color::Rgb(146, 64, 14),
387 swap: Color::Rgb(0x8e, 0x45, 0x85),
388 swap_pss: Color::Rgb(0x8e, 0x45, 0x85),
389 tile_fg: Color::Black,
390 treemap: [
391 Color::Rgb(219, 234, 254),
392 Color::Rgb(237, 233, 254),
393 Color::Rgb(204, 251, 241),
394 Color::Rgb(254, 243, 199),
395 Color::Rgb(220, 252, 231),
396 Color::Rgb(254, 226, 226),
397 ],
398 },
399 };
400
401 pub const ANSI16: Self = Self {
404 name: "ansi16",
405 palette: Palette {
406 dim: Color::DarkGray,
407 dim_selected: Color::Gray,
408 highlight_bg: Color::Blue,
409 accent: Color::Cyan,
410 bar: Color::Cyan,
411 ok: Color::Green,
412 warn: Color::Yellow,
413 crit: Color::Red,
414 focus: Color::Yellow,
415 anon: Color::Blue,
416 file: Color::Cyan,
417 shmem: Color::Yellow,
418 swap: Color::Magenta,
419 swap_pss: Color::Magenta,
420 tile_fg: Color::Gray,
421 treemap: [
422 Color::Blue,
423 Color::Magenta,
424 Color::Cyan,
425 Color::Yellow,
426 Color::Green,
427 Color::Red,
428 ],
429 },
430 };
431
432 pub fn rendered(self, support: ColorSupport) -> Self {
434 Self {
435 name: self.name,
436 palette: self.palette.rendered(support),
437 }
438 }
439
440 pub const ALL: [Self; 7] = [
441 Self::DEFAULT,
442 Self::BTOP,
443 Self::DRACULA,
444 Self::GRUVBOX,
445 Self::KANAGAWA,
446 Self::LIGHT,
447 Self::ANSI16,
448 ];
449 pub const NAMES: &'static str =
450 "default, btop, dracula, gruvbox-dark, kanagawa-wave, light, ansi16";
451
452 pub fn by_name(name: &str) -> Option<Self> {
453 Self::ALL
454 .iter()
455 .copied()
456 .find(|theme| theme.name.eq_ignore_ascii_case(name))
457 }
458
459 pub fn next(self) -> Self {
460 let index = Self::ALL
461 .iter()
462 .position(|theme| theme.name == self.name)
463 .unwrap_or(0);
464 Self::ALL[(index + 1) % Self::ALL.len()]
465 }
466}
467
468#[cfg(test)]
469mod tests {
470 use super::{Palette, Theme};
471 use ratatui::style::Color;
472
473 #[test]
474 fn default_theme_keeps_the_shipped_chrome_colours() {
475 let p = Theme::DEFAULT.palette;
476 assert_eq!(p, Palette::DEFAULT);
477 assert_eq!(p.highlight_bg, Color::Rgb(38, 79, 120));
478 assert_eq!(p.dim, Color::DarkGray);
479 assert_eq!(p.dim_selected, Color::Gray);
480 assert_eq!(p.accent, Color::Cyan);
481 assert_eq!(p.bar, Color::Cyan);
482 assert_eq!(p.focus, Color::Yellow);
483 assert_eq!(p.treemap[0], Color::Rgb(46, 64, 94));
484 assert_eq!(p.treemap[5], Color::Rgb(78, 47, 58));
485 }
486
487 #[test]
488 fn theme_lookup_is_case_insensitive_and_rejects_unknown_names() {
489 assert_eq!(Theme::by_name("default"), Some(Theme::DEFAULT));
490 assert_eq!(Theme::by_name("BTOP"), Some(Theme::BTOP));
491 assert_eq!(Theme::by_name("Dracula"), Some(Theme::DRACULA));
492 assert_eq!(Theme::by_name("gruvbox-dark"), Some(Theme::GRUVBOX));
493 assert_eq!(Theme::by_name("Kanagawa-Wave"), Some(Theme::KANAGAWA));
494 assert_eq!(Theme::by_name("Light"), Some(Theme::LIGHT));
495 assert_eq!(Theme::by_name("ANSI16"), Some(Theme::ANSI16));
496 assert_eq!(Theme::by_name("solarized"), None);
497 }
498
499 #[test]
500 fn t_cycles_through_every_builtin_and_wraps() {
501 for pair in Theme::ALL.windows(2) {
502 assert_eq!(pair[0].next(), pair[1], "{} ->", pair[0].name);
503 }
504 assert_eq!(Theme::ALL[Theme::ALL.len() - 1].next(), Theme::ALL[0]);
505 }
506
507 #[test]
508 fn names_lists_every_theme_in_order() {
509 let joined = Theme::ALL
510 .iter()
511 .map(|theme| theme.name)
512 .collect::<Vec<_>>()
513 .join(", ");
514 assert_eq!(joined, Theme::NAMES);
515 }
516
517 #[test]
518 fn btop_theme_matches_the_btop_default_palette() {
519 let p = Theme::BTOP.palette;
520 assert_eq!(p.highlight_bg, Color::Rgb(0x6a, 0x2f, 0x2f));
521 assert_eq!(p.ok, Color::Rgb(0x77, 0xca, 0x9b));
522 assert_eq!(p.warn, Color::Rgb(0xcb, 0xc0, 0x6c));
523 assert_eq!(p.crit, Color::Rgb(0xdc, 0x4c, 0x4c));
524 assert_eq!(p.treemap[1], Color::Rgb(0x26, 0xc5, 0xff));
525 }
526
527 #[test]
528 fn ansi256_conversion_uses_the_cube_and_grey_ramp() {
529 assert_eq!(super::rgb_to_256(255, 0, 0), 196);
530 assert_eq!(super::rgb_to_256(0, 255, 0), 46);
531 assert_eq!(super::rgb_to_256(0, 0, 255), 21);
532 assert_eq!(super::rgb_to_256(0, 0, 0), 232);
533 assert_eq!(super::rgb_to_256(255, 255, 255), 255);
534 assert_eq!(super::rgb_to_256(128, 128, 128), 244);
535 }
536
537 #[test]
538 fn rendered_256_maps_rgb_to_indexed_and_keeps_the_name() {
539 let theme = Theme::BTOP.rendered(super::ColorSupport::Ansi256);
540 assert_eq!(theme.name, "btop");
541 assert_eq!(
542 theme.palette.highlight_bg,
543 Color::Indexed(super::rgb_to_256(0x6a, 0x2f, 0x2f))
544 );
545 assert!(matches!(theme.palette.treemap[0], Color::Indexed(_)));
546 }
547
548 #[test]
549 fn rendered_16_uses_named_ansi_colours() {
550 use super::ColorSupport;
551 assert_eq!(
552 super::render_color(Color::Rgb(255, 0, 0), ColorSupport::Ansi16),
553 Color::LightRed
554 );
555 assert_eq!(
556 super::render_color(Color::Rgb(0, 0, 0), ColorSupport::Ansi16),
557 Color::Black
558 );
559 assert_eq!(
560 super::render_color(Color::Rgb(255, 255, 255), ColorSupport::Ansi16),
561 Color::White
562 );
563 assert_eq!(
565 super::render_color(Color::Rgb(0x44, 0x47, 0x5a), ColorSupport::Ansi16),
566 Color::DarkGray
567 );
568 assert_eq!(
570 super::render_color(Color::Cyan, ColorSupport::Ansi16),
571 Color::Cyan
572 );
573 }
574
575 #[test]
576 fn truecolor_rendering_is_a_no_op() {
577 assert_eq!(
578 Theme::DRACULA.rendered(super::ColorSupport::TrueColor),
579 Theme::DRACULA
580 );
581 }
582
583 #[test]
584 fn pss_mix_colours_are_theme_derived_and_distinct() {
585 for theme in Theme::ALL {
586 let palette = theme.palette;
587 let distinct: std::collections::HashSet<Color> =
588 [palette.anon, palette.file, palette.shmem, palette.swap]
589 .into_iter()
590 .collect();
591 assert_eq!(distinct.len(), 4, "{} reuses a PSS mix colour", theme.name);
592 assert_eq!(palette.swap_pss, palette.swap, "{}", theme.name);
593 }
594
595 let p = Theme::DEFAULT.palette;
597 assert_eq!(p.anon, Color::Magenta);
598 assert_eq!(p.file, Color::Cyan);
599 assert_eq!(p.shmem, Color::Yellow);
600 assert_eq!(p.swap, Color::LightBlue);
601 }
602
603 #[test]
604 fn every_theme_has_distinct_fills_and_no_tile_fg_collision() {
605 for theme in Theme::ALL {
606 let palette = theme.palette;
607 let distinct: std::collections::HashSet<Color> =
608 palette.treemap.iter().copied().collect();
609 assert_eq!(distinct.len(), 6, "{} reuses a tile fill", theme.name);
610 assert!(
611 !distinct.contains(&palette.tile_fg),
612 "{} tile_fg is also a tile fill",
613 theme.name
614 );
615 }
616 }
617
618 #[test]
619 fn ansi16_uses_no_rgb_or_indexed_colours() {
620 let p = Theme::ANSI16.palette;
621 let slots = [
622 p.dim,
623 p.dim_selected,
624 p.highlight_bg,
625 p.accent,
626 p.bar,
627 p.ok,
628 p.warn,
629 p.crit,
630 p.focus,
631 p.anon,
632 p.file,
633 p.shmem,
634 p.swap,
635 p.swap_pss,
636 p.tile_fg,
637 ];
638 for color in slots.into_iter().chain(p.treemap) {
639 assert!(
640 !matches!(color, Color::Rgb(..) | Color::Indexed(_)),
641 "ansi16 theme leaked {color:?}"
642 );
643 }
644 }
645}