1use crate::palette::{Palette, DEFAULT_PALETTE_INDEX, PALETTE};
2
3pub type Rgb = (u8, u8, u8);
4
5pub struct ColorScheme {
6 pub foreground: Rgb,
7 pub background: Rgb,
8 pub ansi_colors: [Rgb; 256],
9}
10
11impl Default for ColorScheme {
12 fn default() -> Self {
13 Self::new(DEFAULT_PALETTE_INDEX)
14 }
15}
16
17impl ColorScheme {
18 pub fn new(palette_index: usize) -> Self {
19 PALETTE
20 .get(palette_index)
21 .unwrap_or(&PALETTE[DEFAULT_PALETTE_INDEX])
22 .into()
23 }
24}
25
26impl From<&Palette> for ColorScheme {
27 fn from(palette: &Palette) -> Self {
28 let mut colors = [(0, 0, 0); 256];
29 colors[..16].copy_from_slice(&palette.ansi_colors);
30
31 for index in 0..216 {
32 let r = index / 36;
33 let g = (index % 36) / 6;
34 let b = index % 6;
35 let scale = |c: usize| if c == 0 { 0 } else { (c * 40 + 55) as u8 };
36 colors[index + 16] = (scale(r), scale(g), scale(b));
37 }
38
39 for gray_level in 0..24 {
40 let index = 16 + 216 + gray_level;
41 let color_value = (gray_level * 10 + 8) as u8;
42 colors[index] = (color_value, color_value, color_value);
43 }
44
45 Self {
46 foreground: palette.foreground,
47 background: palette.background,
48 ansi_colors: colors,
49 }
50 }
51}