asciigraph-rs 0.1.5

Lightweight ASCII line graphs for the terminal
Documentation
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Color Definitions

use std::fmt;
use std::fmt::Formatter;
use std::str::FromStr;

// `#[repr(transparent)]` ensures that AnsiColor has the exact same memory layout as an u8, making it zero-cost to wrap

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(transparent)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AnsiColor(u8);

impl AnsiColor {
    /// Construct new `AnsiColor`
    ///
    /// # Example
    ///
    /// ```
    /// use asciigraph::AnsiColor;
    ///
    /// let color = AnsiColor::new(13);
    ///
    /// assert_eq!(color.code(), 13);
    /// ```
    pub fn new(number: u8) -> Self {
        AnsiColor(number)
    }

    pub fn code(&self) -> u8 {
        self.0
    }

    // Mapping the Ansi Color name to color names provided in string.
    // Output will be Some(AnsiColor(0)) for "default".
    // To get the number, it's AnsiColor::Default.code()
    //
    /// # Example
    ///
    /// ```
    /// use asciigraph::AnsiColor;
    ///
    /// let color = AnsiColor::AQUA;
    /// let color_from_str = AnsiColor::get_ansi_color("aqua");
    ///
    /// assert!(color_from_str.is_some());
    /// assert_eq!(color, color_from_str.unwrap());
    /// ```
    pub fn get_ansi_color(color: &str) -> Option<AnsiColor> {
        // .ok() converts Result -> Option
        // will work the same as before.
        //
        // in library crate from_str usually return `Result` not `Option`
        //
        // implementing [`FromStr`] and [`TryFrom`] is the idiomatic way of
        // building a Type from &str.
        AnsiColor::from_str(color).ok()
    }

    // Named ANSI 256-color constants
    pub const DEFAULT: AnsiColor = AnsiColor(0);
    pub const ALICE_BLUE: AnsiColor = AnsiColor(255);
    pub const ANTIQUE_WHITE: AnsiColor = AnsiColor(255);
    pub const AQUA: AnsiColor = AnsiColor(14);
    pub const AQUAMARINE: AnsiColor = AnsiColor(122);
    pub const AZURE: AnsiColor = AnsiColor(15);
    pub const BEIGE: AnsiColor = AnsiColor(230);
    pub const BISQUE: AnsiColor = AnsiColor(224);
    pub const BLACK: AnsiColor = AnsiColor(188); // dummy value
    pub const BLANCHED_ALMOND: AnsiColor = AnsiColor(230);
    pub const BLUE: AnsiColor = AnsiColor(12);
    pub const BLUE_VIOLET: AnsiColor = AnsiColor(92);
    pub const BROWN: AnsiColor = AnsiColor(88);
    pub const BURLY_WOOD: AnsiColor = AnsiColor(180);
    pub const CADET_BLUE: AnsiColor = AnsiColor(73);
    pub const CHARTREUSE: AnsiColor = AnsiColor(118);
    pub const CHOCOLATE: AnsiColor = AnsiColor(166);
    pub const CORAL: AnsiColor = AnsiColor(209);
    pub const CORNFLOWER_BLUE: AnsiColor = AnsiColor(68);
    pub const CORNSILK: AnsiColor = AnsiColor(230);
    pub const CRIMSON: AnsiColor = AnsiColor(161);
    pub const CYAN: AnsiColor = AnsiColor(14);
    pub const DARK_BLUE: AnsiColor = AnsiColor(18);
    pub const DARK_CYAN: AnsiColor = AnsiColor(30);
    pub const DARK_GOLDENROD: AnsiColor = AnsiColor(136);
    pub const DARK_GRAY: AnsiColor = AnsiColor(248);
    pub const DARK_GREEN: AnsiColor = AnsiColor(22);
    pub const DARK_KHAKI: AnsiColor = AnsiColor(143);
    pub const DARK_MAGENTA: AnsiColor = AnsiColor(90);
    pub const DARK_OLIVE_GREEN: AnsiColor = AnsiColor(59);
    pub const DARK_ORANGE: AnsiColor = AnsiColor(208);
    pub const DARK_ORCHID: AnsiColor = AnsiColor(134);
    pub const DARK_RED: AnsiColor = AnsiColor(88);
    pub const DARK_SALMON: AnsiColor = AnsiColor(173);
    pub const DARK_SEA_GREEN: AnsiColor = AnsiColor(108);
    pub const DARK_SLATE_BLUE: AnsiColor = AnsiColor(60);
    pub const DARK_SLATE_GRAY: AnsiColor = AnsiColor(238);
    pub const DARK_TURQUOISE: AnsiColor = AnsiColor(44);
    pub const DARK_VIOLET: AnsiColor = AnsiColor(92);
    pub const DEEP_PINK: AnsiColor = AnsiColor(198);
    pub const DEEP_SKY_BLUE: AnsiColor = AnsiColor(39);
    pub const DIM_GRAY: AnsiColor = AnsiColor(242);
    pub const DODGER_BLUE: AnsiColor = AnsiColor(33);
    pub const FIREBRICK: AnsiColor = AnsiColor(124);
    pub const FLORAL_WHITE: AnsiColor = AnsiColor(15);
    pub const FOREST_GREEN: AnsiColor = AnsiColor(28);
    pub const FUCHSIA: AnsiColor = AnsiColor(13);
    pub const GAINSBORO: AnsiColor = AnsiColor(253);
    pub const GHOST_WHITE: AnsiColor = AnsiColor(15);
    pub const GOLD: AnsiColor = AnsiColor(220);
    pub const GOLDENROD: AnsiColor = AnsiColor(178);
    pub const GRAY: AnsiColor = AnsiColor(8);
    pub const GREEN: AnsiColor = AnsiColor(2);
    pub const GREEN_YELLOW: AnsiColor = AnsiColor(155);
    pub const HONEYDEW: AnsiColor = AnsiColor(15);
    pub const HOT_PINK: AnsiColor = AnsiColor(205);
    pub const INDIAN_RED: AnsiColor = AnsiColor(167);
    pub const INDIGO: AnsiColor = AnsiColor(54);
    pub const IVORY: AnsiColor = AnsiColor(15);
    pub const KHAKI: AnsiColor = AnsiColor(222);
    pub const LAVENDER: AnsiColor = AnsiColor(254);
    pub const LAVENDER_BLUSH: AnsiColor = AnsiColor(255);
    pub const LAWN_GREEN: AnsiColor = AnsiColor(118);
    pub const LEMON_CHIFFON: AnsiColor = AnsiColor(230);
    pub const LIGHT_BLUE: AnsiColor = AnsiColor(152);
    pub const LIGHT_CORAL: AnsiColor = AnsiColor(210);
    pub const LIGHT_CYAN: AnsiColor = AnsiColor(195);
    pub const LIGHT_GOLDENROD_YELLOW: AnsiColor = AnsiColor(230);
    pub const LIGHT_GRAY: AnsiColor = AnsiColor(252);
    pub const LIGHT_GREEN: AnsiColor = AnsiColor(120);
    pub const LIGHT_PINK: AnsiColor = AnsiColor(217);
    pub const LIGHT_SALMON: AnsiColor = AnsiColor(216);
    pub const LIGHT_SEA_GREEN: AnsiColor = AnsiColor(37);
    pub const LIGHT_SKY_BLUE: AnsiColor = AnsiColor(117);
    pub const LIGHT_SLATE_GRAY: AnsiColor = AnsiColor(103);
    pub const LIGHT_STEEL_BLUE: AnsiColor = AnsiColor(152);
    pub const LIGHT_YELLOW: AnsiColor = AnsiColor(230);
    pub const LIME: AnsiColor = AnsiColor(10);
    pub const LIME_GREEN: AnsiColor = AnsiColor(77);
    pub const LINEN: AnsiColor = AnsiColor(255);
    pub const MAGENTA: AnsiColor = AnsiColor(13);
    pub const MAROON: AnsiColor = AnsiColor(1);
    pub const MEDIUM_AQUAMARINE: AnsiColor = AnsiColor(79);
    pub const MEDIUM_BLUE: AnsiColor = AnsiColor(20);
    pub const MEDIUM_ORCHID: AnsiColor = AnsiColor(134);
    pub const MEDIUM_PURPLE: AnsiColor = AnsiColor(98);
    pub const MEDIUM_SEA_GREEN: AnsiColor = AnsiColor(72);
    pub const MEDIUM_SLATE_BLUE: AnsiColor = AnsiColor(99);
    pub const MEDIUM_SPRING_GREEN: AnsiColor = AnsiColor(48);
    pub const MEDIUM_TURQUOISE: AnsiColor = AnsiColor(80);
    pub const MEDIUM_VIOLET_RED: AnsiColor = AnsiColor(162);
    pub const MIDNIGHT_BLUE: AnsiColor = AnsiColor(17);
    pub const MINT_CREAM: AnsiColor = AnsiColor(15);
    pub const MISTY_ROSE: AnsiColor = AnsiColor(224);
    pub const MOCCASIN: AnsiColor = AnsiColor(223);
    pub const NAVAJO_WHITE: AnsiColor = AnsiColor(223);
    pub const NAVY: AnsiColor = AnsiColor(4);
    pub const OLD_LACE: AnsiColor = AnsiColor(230);
    pub const OLIVE: AnsiColor = AnsiColor(3);
    pub const OLIVE_DRAB: AnsiColor = AnsiColor(64);
    pub const ORANGE: AnsiColor = AnsiColor(214);
    pub const ORANGE_RED: AnsiColor = AnsiColor(202);
    pub const ORCHID: AnsiColor = AnsiColor(170);
    pub const PALE_GOLDENROD: AnsiColor = AnsiColor(223);
    pub const PALE_GREEN: AnsiColor = AnsiColor(120);
    pub const PALE_TURQUOISE: AnsiColor = AnsiColor(159);
    pub const PALE_VIOLET_RED: AnsiColor = AnsiColor(168);
    pub const PAPAYA_WHIP: AnsiColor = AnsiColor(230);
    pub const PEACH_PUFF: AnsiColor = AnsiColor(223);
    pub const PERU: AnsiColor = AnsiColor(173);
    pub const PINK: AnsiColor = AnsiColor(218);
    pub const PLUM: AnsiColor = AnsiColor(182);
    pub const POWDER_BLUE: AnsiColor = AnsiColor(152);
    pub const PURPLE: AnsiColor = AnsiColor(5);
    pub const RED: AnsiColor = AnsiColor(9);
    pub const ROSY_BROWN: AnsiColor = AnsiColor(138);
    pub const ROYAL_BLUE: AnsiColor = AnsiColor(63);
    pub const SADDLE_BROWN: AnsiColor = AnsiColor(94);
    pub const SALMON: AnsiColor = AnsiColor(210);
    pub const SANDY_BROWN: AnsiColor = AnsiColor(215);
    pub const SEA_GREEN: AnsiColor = AnsiColor(29);
    pub const SEA_SHELL: AnsiColor = AnsiColor(15);
    pub const SIENNA: AnsiColor = AnsiColor(131);
    pub const SILVER: AnsiColor = AnsiColor(7);
    pub const SKY_BLUE: AnsiColor = AnsiColor(117);
    pub const SLATE_BLUE: AnsiColor = AnsiColor(62);
    pub const SLATE_GRAY: AnsiColor = AnsiColor(66);
    pub const SNOW: AnsiColor = AnsiColor(15);
    pub const SPRING_GREEN: AnsiColor = AnsiColor(48);
    pub const STEEL_BLUE: AnsiColor = AnsiColor(67);
    pub const TAN: AnsiColor = AnsiColor(180);
    pub const TEAL: AnsiColor = AnsiColor(6);
    pub const THISTLE: AnsiColor = AnsiColor(182);
    pub const TOMATO: AnsiColor = AnsiColor(203);
    pub const TURQUOISE: AnsiColor = AnsiColor(80);
    pub const VIOLET: AnsiColor = AnsiColor(213);
    pub const WHEAT: AnsiColor = AnsiColor(223);
    pub const WHITE: AnsiColor = AnsiColor(15);
    pub const WHITE_SMOKE: AnsiColor = AnsiColor(255);
    pub const YELLOW: AnsiColor = AnsiColor(11);
    pub const YELLOW_GREEN: AnsiColor = AnsiColor(149);
}

// deligates to [`FromStr`]
//
/// # Example
///
/// ```
/// use asciigraph::AnsiColor;
///
/// let color = AnsiColor::AQUA;
/// let color_from_str = AnsiColor::try_from("aqua");
///
/// assert!(color_from_str.is_ok());
/// assert_eq!(color, color_from_str.unwrap());
/// ```
impl TryFrom<&str> for AnsiColor {
    type Error = &'static str;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        AnsiColor::from_str(value)
    }
}

// implementing [`FromStr`] and [`TryFrom`] is the idiomatic way of
// building a Type from &str.
//
/// # Example
///
/// ```
/// use asciigraph::AnsiColor;
/// use std::str::FromStr;
///
/// let color = AnsiColor::AQUA;
/// let color_from_str = AnsiColor::from_str("aqua");
/// let color_parsed = "aqua".parse::<AnsiColor>();
///
/// assert!(color_from_str.is_ok());
/// assert_eq!(color, color_from_str.unwrap());
/// assert_eq!(color, color_parsed.unwrap());
/// ```
///
/// # Error
///
/// return `Err()` on invalid color name.
/// ```
/// use asciigraph::AnsiColor;
/// use std::str::FromStr;
///
/// let not_a_color = AnsiColor::from_str("ironman");
/// assert_eq!(not_a_color, Err("invalid color name"));
/// ```
impl FromStr for AnsiColor {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // sanitizing `&str` will make api more user-friendly
        // now "AliceBlue", "aliceblue", or "ALICEBLUE" will just work.
        match s.to_lowercase().replace('_', "").as_str() {
            "default" => Ok(AnsiColor::DEFAULT),
            "aliceblue" => Ok(AnsiColor::ALICE_BLUE),
            "antiquewhite" => Ok(AnsiColor::ANTIQUE_WHITE),
            "aqua" => Ok(AnsiColor::AQUA),
            "aquamarine" => Ok(AnsiColor::AQUAMARINE),
            "azure" => Ok(AnsiColor::AZURE),
            "beige" => Ok(AnsiColor::BEIGE),
            "bisque" => Ok(AnsiColor::BISQUE),
            "black" => Ok(AnsiColor::BLACK),
            "blanchedalmond" => Ok(AnsiColor::BLANCHED_ALMOND),
            "blue" => Ok(AnsiColor::BLUE),
            "blueviolet" => Ok(AnsiColor::BLUE_VIOLET),
            "brown" => Ok(AnsiColor::BROWN),
            "burlywood" => Ok(AnsiColor::BURLY_WOOD),
            "cadetblue" => Ok(AnsiColor::CADET_BLUE),
            "chartreuse" => Ok(AnsiColor::CHARTREUSE),
            "chocolate" => Ok(AnsiColor::CHOCOLATE),
            "coral" => Ok(AnsiColor::CORAL),
            "cornflowerblue" => Ok(AnsiColor::CORNFLOWER_BLUE),
            "cornsilk" => Ok(AnsiColor::CORNSILK),
            "crimson" => Ok(AnsiColor::CRIMSON),
            "cyan" => Ok(AnsiColor::CYAN),
            "darkblue" => Ok(AnsiColor::DARK_BLUE),
            "darkcyan" => Ok(AnsiColor::DARK_CYAN),
            "darkgoldenrod" => Ok(AnsiColor::DARK_GOLDENROD),
            "darkgray" => Ok(AnsiColor::DARK_GRAY),
            "darkgreen" => Ok(AnsiColor::DARK_GREEN),
            "darkkhaki" => Ok(AnsiColor::DARK_KHAKI),
            "darkmagenta" => Ok(AnsiColor::DARK_MAGENTA),
            "darkolivegreen" => Ok(AnsiColor::DARK_OLIVE_GREEN),
            "darkorange" => Ok(AnsiColor::DARK_ORANGE),
            "darkorchid" => Ok(AnsiColor::DARK_ORCHID),
            "darkred" => Ok(AnsiColor::DARK_RED),
            "darksalmon" => Ok(AnsiColor::DARK_SALMON),
            "darkseagreen" => Ok(AnsiColor::DARK_SEA_GREEN),
            "darkslateblue" => Ok(AnsiColor::DARK_SLATE_BLUE),
            "darkslategray" => Ok(AnsiColor::DARK_SLATE_GRAY),
            "darkturquoise" => Ok(AnsiColor::DARK_TURQUOISE),
            "darkviolet" => Ok(AnsiColor::DARK_VIOLET),
            "deeppink" => Ok(AnsiColor::DEEP_PINK),
            "deepskyblue" => Ok(AnsiColor::DEEP_SKY_BLUE),
            "dimgray" => Ok(AnsiColor::DIM_GRAY),
            "dodgerblue" => Ok(AnsiColor::DODGER_BLUE),
            "firebrick" => Ok(AnsiColor::FIREBRICK),
            "floralwhite" => Ok(AnsiColor::FLORAL_WHITE),
            "forestgreen" => Ok(AnsiColor::FOREST_GREEN),
            "fuchsia" => Ok(AnsiColor::FUCHSIA),
            "gainsboro" => Ok(AnsiColor::GAINSBORO),
            "ghostwhite" => Ok(AnsiColor::GHOST_WHITE),
            "gold" => Ok(AnsiColor::GOLD),
            "goldenrod" => Ok(AnsiColor::GOLDENROD),
            "gray" => Ok(AnsiColor::GRAY),
            "green" => Ok(AnsiColor::GREEN),
            "greenyellow" => Ok(AnsiColor::GREEN_YELLOW),
            "honeydew" => Ok(AnsiColor::HONEYDEW),
            "hotpink" => Ok(AnsiColor::HOT_PINK),
            "indianred" => Ok(AnsiColor::INDIAN_RED),
            "indigo" => Ok(AnsiColor::INDIGO),
            "ivory" => Ok(AnsiColor::IVORY),
            "khaki" => Ok(AnsiColor::KHAKI),
            "lavender" => Ok(AnsiColor::LAVENDER),
            "lavenderblush" => Ok(AnsiColor::LAVENDER_BLUSH),
            "lawngreen" => Ok(AnsiColor::LAWN_GREEN),
            "lemonchiffon" => Ok(AnsiColor::LEMON_CHIFFON),
            "lightblue" => Ok(AnsiColor::LIGHT_BLUE),
            "lightcoral" => Ok(AnsiColor::LIGHT_CORAL),
            "lightcyan" => Ok(AnsiColor::LIGHT_CYAN),
            "lightgoldenrodyellow" => Ok(AnsiColor::LIGHT_GOLDENROD_YELLOW),
            "lightgray" => Ok(AnsiColor::LIGHT_GRAY),
            "lightgreen" => Ok(AnsiColor::LIGHT_GREEN),
            "lightpink" => Ok(AnsiColor::LIGHT_PINK),
            "lightsalmon" => Ok(AnsiColor::LIGHT_SALMON),
            "lightseagreen" => Ok(AnsiColor::LIGHT_SEA_GREEN),
            "lightskyblue" => Ok(AnsiColor::LIGHT_SKY_BLUE),
            "lightslategray" => Ok(AnsiColor::LIGHT_SLATE_GRAY),
            "lightsteelblue" => Ok(AnsiColor::LIGHT_STEEL_BLUE),
            "lightyellow" => Ok(AnsiColor::LIGHT_YELLOW),
            "lime" => Ok(AnsiColor::LIME),
            "limegreen" => Ok(AnsiColor::LIME_GREEN),
            "linen" => Ok(AnsiColor::LINEN),
            "magenta" => Ok(AnsiColor::MAGENTA),
            "maroon" => Ok(AnsiColor::MAROON),
            "mediumaquamarine" => Ok(AnsiColor::MEDIUM_AQUAMARINE),
            "mediumblue" => Ok(AnsiColor::MEDIUM_BLUE),
            "mediumorchid" => Ok(AnsiColor::MEDIUM_ORCHID),
            "mediumpurple" => Ok(AnsiColor::MEDIUM_PURPLE),
            "mediumseagreen" => Ok(AnsiColor::MEDIUM_SEA_GREEN),
            "mediumslateblue" => Ok(AnsiColor::MEDIUM_SLATE_BLUE),
            "mediumspringgreen" => Ok(AnsiColor::MEDIUM_SPRING_GREEN),
            "mediumturquoise" => Ok(AnsiColor::MEDIUM_TURQUOISE),
            "mediumvioletred" => Ok(AnsiColor::MEDIUM_VIOLET_RED),
            "midnightblue" => Ok(AnsiColor::MIDNIGHT_BLUE),
            "mintcream" => Ok(AnsiColor::MINT_CREAM),
            "mistyrose" => Ok(AnsiColor::MISTY_ROSE),
            "moccasin" => Ok(AnsiColor::MOCCASIN),
            "navajowhite" => Ok(AnsiColor::NAVAJO_WHITE),
            "navy" => Ok(AnsiColor::NAVY),
            "oldlace" => Ok(AnsiColor::OLD_LACE),
            "olive" => Ok(AnsiColor::OLIVE),
            "olivedrab" => Ok(AnsiColor::OLIVE_DRAB),
            "orange" => Ok(AnsiColor::ORANGE),
            "orangered" => Ok(AnsiColor::ORANGE_RED),
            "orchid" => Ok(AnsiColor::ORCHID),
            "palegoldenrod" => Ok(AnsiColor::PALE_GOLDENROD),
            "palegreen" => Ok(AnsiColor::PALE_GREEN),
            "paleturquoise" => Ok(AnsiColor::PALE_TURQUOISE),
            "palevioletred" => Ok(AnsiColor::PALE_VIOLET_RED),
            "papayawhip" => Ok(AnsiColor::PAPAYA_WHIP),
            "peachpuff" => Ok(AnsiColor::PEACH_PUFF),
            "peru" => Ok(AnsiColor::PERU),
            "pink" => Ok(AnsiColor::PINK),
            "plum" => Ok(AnsiColor::PLUM),
            "powderblue" => Ok(AnsiColor::POWDER_BLUE),
            "purple" => Ok(AnsiColor::PURPLE),
            "red" => Ok(AnsiColor::RED),
            "rosybrown" => Ok(AnsiColor::ROSY_BROWN),
            "royalblue" => Ok(AnsiColor::ROYAL_BLUE),
            "saddlebrown" => Ok(AnsiColor::SADDLE_BROWN),
            "salmon" => Ok(AnsiColor::SALMON),
            "sandybrown" => Ok(AnsiColor::SANDY_BROWN),
            "seagreen" => Ok(AnsiColor::SEA_GREEN),
            "seashell" => Ok(AnsiColor::SEA_SHELL),
            "sienna" => Ok(AnsiColor::SIENNA),
            "silver" => Ok(AnsiColor::SILVER),
            "skyblue" => Ok(AnsiColor::SKY_BLUE),
            "slateblue" => Ok(AnsiColor::SLATE_BLUE),
            "slategray" => Ok(AnsiColor::SLATE_GRAY),
            "snow" => Ok(AnsiColor::SNOW),
            "springgreen" => Ok(AnsiColor::SPRING_GREEN),
            "steelblue" => Ok(AnsiColor::STEEL_BLUE),
            "tan" => Ok(AnsiColor::TAN),
            "teal" => Ok(AnsiColor::TEAL),
            "thistle" => Ok(AnsiColor::THISTLE),
            "tomato" => Ok(AnsiColor::TOMATO),
            "turquoise" => Ok(AnsiColor::TURQUOISE),
            "violet" => Ok(AnsiColor::VIOLET),
            "wheat" => Ok(AnsiColor::WHEAT),
            "white" => Ok(AnsiColor::WHITE),
            "whitesmoke" => Ok(AnsiColor::WHITE_SMOKE),
            "yellow" => Ok(AnsiColor::YELLOW),
            "yellowgreen" => Ok(AnsiColor::YELLOW_GREEN),
            _ => Err("invalid color name"),
        }
    }
}

// Allow easy conversion from u8 to AnsiColor
impl From<u8> for AnsiColor {
    fn from(value: u8) -> Self {
        AnsiColor(value)
    }
}

// Allow easy conversion from AnsiColor to u8
impl From<AnsiColor> for u8 {
    fn from(color: AnsiColor) -> Self {
        color.0
    }
}

impl fmt::Display for AnsiColor {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if *self == Self::DEFAULT {
            return write!(f, "\x1b[0m");
        }

        let c = if *self == AnsiColor::BLACK {
            0u8
        } else {
            self.0
        };

        if c <= AnsiColor::SILVER.into() {
            write!(f, "\x1b[{}m", 30 + c)
        } else if c <= AnsiColor::WHITE.into() {
            write!(f, "\x1b[{}m", 82 + c)
        } else {
            write!(f, "\x1b[38;5;{}m", c)
        }
    }
}