1
use ratatui::style::Color;
2

            
3
use super::ColorDepth;
4

            
5
/// The sixteen CGA colours.
6
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
7
pub enum Cga {
8
    #[default]
9
    Black,
10
    Blue,
11
    Green,
12
    Cyan,
13
    Red,
14
    Magenta,
15
    Brown,
16
    LightGray,
17
    DarkGray,
18
    LightBlue,
19
    LightGreen,
20
    LightCyan,
21
    LightRed,
22
    LightMagenta,
23
    Yellow,
24
    White,
25
}
26

            
27
impl Cga {
28
    pub(super) const ALL: [Cga; 16] = [
29
        Cga::Black,
30
        Cga::Blue,
31
        Cga::Green,
32
        Cga::Cyan,
33
        Cga::Red,
34
        Cga::Magenta,
35
        Cga::Brown,
36
        Cga::LightGray,
37
        Cga::DarkGray,
38
        Cga::LightBlue,
39
        Cga::LightGreen,
40
        Cga::LightCyan,
41
        Cga::LightRed,
42
        Cga::LightMagenta,
43
        Cga::Yellow,
44
        Cga::White,
45
    ];
46

            
47
8128119
    const fn entry(self) -> (&'static str, u32) {
48
8128119
        match self {
49
1089248
            Cga::Black => ("black", 0x00_00_00),
50
819041
            Cga::Blue => ("blue", 0x00_00_AA),
51
816258
            Cga::Green => ("green", 0x00_AA_00),
52
793283
            Cga::Cyan => ("cyan", 0x00_AA_AA),
53
690163
            Cga::Red => ("red", 0xAA_00_00),
54
714291
            Cga::Magenta => ("magenta", 0xAA_00_AA),
55
629203
            Cga::Brown => ("brown", 0xAA_55_00),
56
701197
            Cga::LightGray => ("lightgray", 0xAA_AA_AA),
57
473579
            Cga::DarkGray => ("darkgray", 0x55_55_55),
58
297105
            Cga::LightBlue => ("lightblue", 0x55_55_FF),
59
264725
            Cga::LightGreen => ("lightgreen", 0x55_FF_55),
60
230868
            Cga::LightCyan => ("lightcyan", 0x55_FF_FF),
61
162122
            Cga::LightRed => ("lightred", 0xFF_55_55),
62
153244
            Cga::LightMagenta => ("lightmagenta", 0xFF_55_FF),
63
139051
            Cga::Yellow => ("yellow", 0xFF_FF_55),
64
154741
            Cga::White => ("white", 0xFF_FF_FF),
65
        }
66
8128119
    }
67

            
68
    /// The colour's own RGB, whatever depth the terminal shows it at.
69
    #[must_use]
70
1339
    pub fn rgb(self) -> [u8; 3] {
71
1339
        let [_, r, g, b] = self.entry().1.to_be_bytes();
72
1339
        [r, g, b]
73
1339
    }
74

            
75
    #[must_use]
76
1014715
    pub fn from_name(name: &str) -> Option<Cga> {
77
7803333
        Self::ALL.into_iter().find(|cga| cga.entry().0 == name)
78
1014715
    }
79

            
80
    /// The colour as the terminal shows it at `depth`.
81
    #[must_use]
82
324039
    pub fn color(self, depth: ColorDepth) -> Color {
83
324039
        match depth {
84
323447
            ColorDepth::Rgb => Color::from_u32(self.entry().1),
85
103
            ColorDepth::Indexed256 => Color::Indexed(self.index256()),
86
489
            ColorDepth::Ansi16 => self.ansi(),
87
        }
88
324039
    }
89

            
90
    /// Like [`Cga::color`], but ANSI-16 backgrounds only have the eight dark colours.
91
    #[must_use]
92
162040
    pub fn background(self, depth: ColorDepth) -> Color {
93
162040
        match depth {
94
199
            ColorDepth::Ansi16 => self.dark().ansi(),
95
161841
            _ => self.color(depth),
96
        }
97
162040
    }
98

            
99
    /// The RGB a cell with this background shows at `depth`: the colour itself at `Rgb`, the
100
    /// xterm palette entry at `Indexed256`, the dark colour at `Ansi16`.
101
    #[must_use]
102
1361
    pub fn background_rgb(self, depth: ColorDepth) -> [u8; 3] {
103
1361
        match depth {
104
1128
            ColorDepth::Rgb => self.rgb(),
105
104
            ColorDepth::Indexed256 => xterm_rgb(self.index256()),
106
129
            ColorDepth::Ansi16 => self.dark().rgb(),
107
        }
108
1361
    }
109

            
110
    /// The nearest cube or grey-ramp index, which terminal themes cannot remap.
111
207
    const fn index256(self) -> u8 {
112
207
        match self {
113
11
            Cga::Black => 16,
114
29
            Cga::Blue => 19,
115
25
            Cga::Green => 34,
116
23
            Cga::Cyan => 37,
117
1
            Cga::Red => 124,
118
5
            Cga::Magenta => 127,
119
3
            Cga::Brown => 130,
120
57
            Cga::LightGray => 248,
121
32
            Cga::DarkGray => 240,
122
11
            Cga::LightBlue => 63,
123
1
            Cga::LightGreen => 83,
124
1
            Cga::LightCyan => 87,
125
3
            Cga::LightRed => 203,
126
1
            Cga::LightMagenta => 207,
127
1
            Cga::Yellow => 227,
128
3
            Cga::White => 231,
129
        }
130
207
    }
131

            
132
688
    const fn ansi(self) -> Color {
133
688
        match self {
134
120
            Cga::Black => Color::Black,
135
135
            Cga::Blue => Color::Blue,
136
72
            Cga::Green => Color::Green,
137
59
            Cga::Cyan => Color::Cyan,
138
60
            Cga::Red => Color::Red,
139
38
            Cga::Magenta => Color::Magenta,
140
35
            Cga::Brown => Color::Yellow,
141
106
            Cga::LightGray => Color::Gray,
142
1
            Cga::DarkGray => Color::DarkGray,
143
1
            Cga::LightBlue => Color::LightBlue,
144
10
            Cga::LightGreen => Color::LightGreen,
145
2
            Cga::LightCyan => Color::LightCyan,
146
1
            Cga::LightRed => Color::LightRed,
147
1
            Cga::LightMagenta => Color::LightMagenta,
148
19
            Cga::Yellow => Color::LightYellow,
149
28
            Cga::White => Color::White,
150
        }
151
688
    }
152

            
153
328
    const fn dark(self) -> Cga {
154
328
        match self {
155
38
            Cga::DarkGray => Cga::Black,
156
10
            Cga::LightBlue => Cga::Blue,
157
1
            Cga::LightGreen => Cga::Green,
158
1
            Cga::LightCyan => Cga::Cyan,
159
3
            Cga::LightRed => Cga::Red,
160
1
            Cga::LightMagenta => Cga::Magenta,
161
1
            Cga::Yellow => Cga::Brown,
162
1
            Cga::White => Cga::LightGray,
163
272
            other => other,
164
        }
165
328
    }
166
}
167

            
168
/// The colour of xterm palette entry `index` in the cube and grey ramp (16..=255).
169
104
fn xterm_rgb(index: u8) -> [u8; 3] {
170
    const LEVELS: [u8; 6] = [0, 95, 135, 175, 215, 255];
171
104
    match index {
172
51
        232.. => [8 + 10 * (index - 232); 3],
173
        _ => {
174
53
            let cube = usize::from(index.saturating_sub(16));
175
53
            [
176
53
                LEVELS[cube / 36 % 6],
177
53
                LEVELS[cube / 6 % 6],
178
53
                LEVELS[cube % 6],
179
53
            ]
180
        }
181
    }
182
104
}