1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/// Represents an enum with variants that can be used to describe the foreground and background colors of a character in a terminal or console application.
#[cfg(not(feature = "TRUE_COLORS"))]
#[repr(u8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
pub enum Color {
/// <table><tr><td style="background-color: #000000; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
#[default]
Black = 0x00,
/// <table><tr><td style="background-color: #000080; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
DarkBlue = 0x01,
/// <table><tr><td style="background-color: #008000; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
DarkGreen = 0x02,
/// <table><tr><td style="background-color: #008080; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Teal = 0x03,
/// <table><tr><td style="background-color: #800000; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
DarkRed = 0x04,
/// <table><tr><td style="background-color: #800080; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Magenta = 0x05,
/// <table><tr><td style="background-color: #808000; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Olive = 0x06,
/// <table><tr><td style="background-color: #C0C0C0; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Silver = 0x07,
/// <table><tr><td style="background-color: #808080; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Gray = 0x08,
/// <table><tr><td style="background-color: #0000FF; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Blue = 0x09,
/// <table><tr><td style="background-color: #00FF00; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Green = 0x0A,
/// <table><tr><td style="background-color: #00FFFF; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Aqua = 0x0B,
/// <table><tr><td style="background-color: #FF0000; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Red = 0x0C,
/// <table><tr><td style="background-color: #FF00FF; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Pink = 0x0D,
/// <table><tr><td style="background-color: #FFFF00; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Yellow = 0x0E,
/// <table><tr><td style="background-color: #FFFFFF; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
White = 0x0F,
/// a marker for transparent color
Transparent = 0x10,
}
/// Represents an enum with variants that can be used to describe the foreground and background colors of a character in a terminal or console application.
#[cfg(feature = "TRUE_COLORS")]
#[repr(u32)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
pub enum Color {
/// <table><tr><td style="background-color: #000000; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
#[default]
Black = 0x00,
/// <table><tr><td style="background-color: #000080; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
DarkBlue = 0x01,
/// <table><tr><td style="background-color: #008000; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
DarkGreen = 0x02,
/// <table><tr><td style="background-color: #008080; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Teal = 0x03,
/// <table><tr><td style="background-color: #800000; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
DarkRed = 0x04,
/// <table><tr><td style="background-color: #800080; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Magenta = 0x05,
/// <table><tr><td style="background-color: #808000; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Olive = 0x06,
/// <table><tr><td style="background-color: #C0C0C0; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Silver = 0x07,
/// <table><tr><td style="background-color: #808080; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Gray = 0x08,
/// <table><tr><td style="background-color: #0000FF; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Blue = 0x09,
/// <table><tr><td style="background-color: #00FF00; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Green = 0x0A,
/// <table><tr><td style="background-color: #00FFFF; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Aqua = 0x0B,
/// <table><tr><td style="background-color: #FF0000; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Red = 0x0C,
/// <table><tr><td style="background-color: #FF00FF; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Pink = 0x0D,
/// <table><tr><td style="background-color: #FFFF00; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
Yellow = 0x0E,
/// <table><tr><td style="background-color: #FFFFFF; width: 20px; height: 20px; border: 1px solid #000;"></td></tr></table>
White = 0x0F,
/// a marker for transparent color
Transparent = 0x10,
/// A 24-bit RGB color, for example `(255, 128, 0)`.
RGB(u8, u8, u8),
}
impl Color {
/// Returns a high-contrast color (`Black` or `White`) for drawing over this color.
///
/// Dark palette colors map to [`White`](Self::White), light ones to [`Black`](Self::Black).
/// [`Transparent`](Self::Transparent) is unchanged. With the `TRUE_COLORS` feature, RGB values
/// use luminance: light colors return black, dark colors return white.
///
/// # Example
/// ```rust
/// use appcui::prelude::*;
///
/// assert_eq!(Color::Black.contrast_color(), Color::White);
/// assert_eq!(Color::Yellow.contrast_color(), Color::Black);
/// ```
#[inline(always)]
pub fn contrast_color(&self) -> Self {
match self {
Color::Black | Color::DarkBlue | Color::DarkGreen | Color::Teal | Color::DarkRed | Color::Magenta | Color::Olive | Color::Gray | Color::Blue | Color::Red => Color::White,
Color::Silver | Color::Green | Color::Aqua | Color::Pink | Color::Yellow | Color::White => Color::Black,
Color::Transparent => Color::Transparent,
#[cfg(feature = "TRUE_COLORS")]
Color::RGB(r, g, b) => {
if (2126 * (*r as i32) + 7152 * (*g as i32) + 722 * (*b as i32)) > 1275000 {
Color::Black
} else {
Color::White
}
}
}
}
/// Returns the complementary (inverted) color.
///
/// Each of the 16 palette colors maps to its opposite (for example [`Black`](Self::Black) ↔
/// [`White`](Self::White), [`Red`](Self::Red) ↔ [`Teal`](Self::Teal)).
/// [`Transparent`](Self::Transparent) is unchanged. With the `TRUE_COLORS` feature, RGB channels
/// are inverted as `255 - channel`.
///
/// # Example
/// ```rust
/// use appcui::prelude::*;
///
/// assert_eq!(Color::Black.inverse_color(), Color::White);
/// assert_eq!(Color::Red.inverse_color(), Color::Teal);
/// ```
#[inline(always)]
pub fn inverse_color(&self) -> Self {
match self {
Color::Black => Color::White, // #000000 → #FFFFFF
Color::DarkBlue => Color::Yellow, // #000080 → #FFFF7F
Color::DarkGreen => Color::Pink, // #008000 → #FF7FFF
Color::Teal => Color::Red, // #008080 → #FF7F7F
Color::DarkRed => Color::Aqua, // #800000 → #7FFFFF
Color::Magenta => Color::Green, // #800080 → #7FFF7F
Color::Olive => Color::Blue, // #808000 → #7F7FFF
Color::Silver => Color::Gray, // #C0C0C0 → #3F3F3F
Color::Gray => Color::Silver, // #808080 → #7F7F7F
Color::Blue => Color::Olive, // #0000FF → #FFFF00
Color::Green => Color::Magenta, // #00FF00 → #FF00FF
Color::Aqua => Color::DarkRed, // #00FFFF → #FF0000
Color::Red => Color::Teal, // #FF0000 → #00FFFF
Color::Pink => Color::DarkGreen, // #FF00FF → #00FF00
Color::Yellow => Color::DarkBlue, // #FFFF00 → #0000FF
Color::White => Color::Black, // #FFFFFF → #000000
Color::Transparent => Color::Transparent,
#[cfg(feature = "TRUE_COLORS")]
Color::RGB(r, g, b) => Color::from_rgb(255 - *r, 255 - *g, 255 - *b),
}
}
/// Builds a palette color from its numeric index (`0`–`16`).
///
/// Indexes match the variant discriminants: `0` is [`Black`](Self::Black), `15` is
/// [`White`](Self::White), and `16` is [`Transparent`](Self::Transparent). Any other value
/// returns `None`. RGB colors cannot be constructed this way.
///
/// # Example
/// ```rust
/// use appcui::prelude::*;
///
/// assert_eq!(Color::from_value(12), Some(Color::Red));
/// assert_eq!(Color::from_value(99), None);
/// ```
pub fn from_value(value: i32) -> Option<Color> {
match value {
0 => Some(Color::Black),
1 => Some(Color::DarkBlue),
2 => Some(Color::DarkGreen),
3 => Some(Color::Teal),
4 => Some(Color::DarkRed),
5 => Some(Color::Magenta),
6 => Some(Color::Olive),
7 => Some(Color::Silver),
8 => Some(Color::Gray),
9 => Some(Color::Blue),
10 => Some(Color::Green),
11 => Some(Color::Aqua),
12 => Some(Color::Red),
13 => Some(Color::Pink),
14 => Some(Color::Yellow),
15 => Some(Color::White),
16 => Some(Color::Transparent),
_ => None,
}
}
/// Returns the variant name, such as `"Red"` or `"Transparent"`.
///
/// With the `TRUE_COLORS` feature, any 24-bit RGB value returns `"RGB"`.
///
/// # Example
/// ```rust
/// use appcui::prelude::*;
///
/// assert_eq!(Color::Aqua.name(), "Aqua");
/// ```
pub fn name(&self) -> &str {
match self {
Color::Black => "Black",
Color::DarkBlue => "DarkBlue",
Color::DarkGreen => "DarkGreen",
Color::Teal => "Teal",
Color::DarkRed => "DarkRed",
Color::Magenta => "Magenta",
Color::Olive => "Olive",
Color::Silver => "Silver",
Color::Gray => "Gray",
Color::Blue => "Blue",
Color::Green => "Green",
Color::Aqua => "Aqua",
Color::Red => "Red",
Color::Pink => "Pink",
Color::Yellow => "Yellow",
Color::White => "White",
Color::Transparent => "Transparent",
#[cfg(feature = "TRUE_COLORS")]
Color::RGB(_, _, _) => "RGB",
}
}
#[inline(always)]
pub(crate) fn as_color_index(&self) -> u8 {
match self {
Color::Black => 0,
Color::DarkBlue => 1,
Color::DarkGreen => 2,
Color::Teal => 3,
Color::DarkRed => 4,
Color::Magenta => 5,
Color::Olive => 6,
Color::Silver => 7,
Color::Gray => 8,
Color::Blue => 9,
Color::Green => 10,
Color::Aqua => 11,
Color::Red => 12,
Color::Pink => 13,
Color::Yellow => 14,
Color::White => 15,
Color::Transparent => 16,
#[cfg(feature = "TRUE_COLORS")]
Color::RGB(r, g, b) => {
let mut index = 0;
if *r > 64 {
index |= 0b100;
}
if *g > 64 {
index |= 0b010;
}
if *b > 64 {
index |= 0b001;
}
// 192 (0xc0) should remain like this so that we obtain th Silver
if *r >= 196 || *g >= 196 || *b >= 196 {
index |= 0b1000;
}
index
}
}
}
// #[cfg(feature = "TRUE_COLORS")]
// #[inline(always)]
// pub(crate) fn is_rgb(&self) -> bool {
// matches!(self, Color::RGB(_, _, _))
// }
// #[cfg(not(feature = "TRUE_COLORS"))]
// #[inline(always)]
// pub(crate) fn is_rgb(&self) -> bool {
// false
// }
#[cfg(feature = "TRUE_COLORS")]
#[inline(always)]
pub(crate) fn rgb(&self) -> Option<(u8, u8, u8)> {
match self {
Color::RGB(r, g, b) => Some((*r, *g, *b)),
_ => None,
}
}
#[cfg(not(feature = "TRUE_COLORS"))]
#[inline(always)]
pub(crate) fn rgb(&self) -> Option<(u8, u8, u8)> {
None
}
/// Builds a color from 8-bit red, green, and blue components.
///
/// With the `TRUE_COLORS` feature this stores a 24-bit RGB color. Without it, the value is
/// quantized to the nearest of the 16 palette colors.
///
/// # Example
/// ```rust
/// use appcui::prelude::*;
///
/// let white = Color::from_rgb(255, 255, 255);
/// assert_eq!(white.contrast_color(), Color::Black);
/// ```
pub fn from_rgb(r: u8, g: u8, b: u8) -> Color {
#[cfg(feature = "TRUE_COLORS")]
return Color::RGB(r, g, b);
#[cfg(not(feature = "TRUE_COLORS"))]
{
let mut index = 0;
if r > 64 {
index |= 0b100;
}
if g > 64 {
index |= 0b010;
}
if b > 64 {
index |= 0b001;
}
// 192 (0xc0) should remain like this so that we obtain th Silver
if r >= 196 || g >= 196 || b >= 196 {
index |= 0b1000;
}
Color::from_value(index).unwrap()
}
}
}