color_output/color/
type.rs

1use crate::*;
2
3/// ColorType
4#[derive(Debug, Clone, PartialEq)]
5pub enum ColorType {
6    /// RGB Color (r, g, b),
7    Rgb(u8, u8, u8),
8    /// Color 256
9    Color256(u32),
10    /// Built-in Colors
11    Use(Color),
12}
13
14/// Display Type
15#[derive(Debug, Clone, PartialEq)]
16pub(crate) enum DisplayType {
17    /// Text
18    Text,
19    /// Background
20    Background,
21}
22
23/// Color
24#[derive(Debug, Clone, PartialEq)]
25pub enum Color {
26    /// Default Color
27    Default,
28    /// Black
29    Black,
30    /// Red
31    Red,
32    /// Green
33    Green,
34    /// Yellow
35    Yellow,
36    /// Blue
37    Blue,
38    /// Magenta
39    Magenta,
40    /// Cyan
41    Cyan,
42    /// White
43    White,
44}
45
46pub(crate) trait ColorDisplay {
47    /// Gets the display string
48    ///
49    /// # Parameters
50    /// - `&Self`: &self
51    /// - `DisplayType`: display_type
52    ///
53    /// # Returns
54    /// - `String`: The displayed string
55    fn get_str(&self, display_type: DisplayType) -> String;
56}
57
58impl Default for Color {
59    fn default() -> Self {
60        Color::Default
61    }
62}
63
64impl ColorDisplay for Color {
65    fn get_str(&self, display_type: DisplayType) -> String {
66        let str: &str = match display_type {
67            DisplayType::Text => match self {
68                Color::Red => RED,
69                Color::Green => GREEN,
70                Color::Blue => BLUE,
71                Color::Yellow => YELLOW,
72                Color::Black => BLACK,
73                Color::White => WHITE,
74                Color::Default => DEFAULT,
75                Color::Magenta => MAGENTA,
76                Color::Cyan => CYAN,
77            },
78            DisplayType::Background => match self {
79                Color::Red => BG_RED,
80                Color::Green => BG_GREEN,
81                Color::Blue => BG_BLUE,
82                Color::Yellow => BG_YELLOW,
83                Color::Black => BG_BLACK,
84                Color::White => BG_WHITE,
85                Color::Default => DEFAULT,
86                Color::Magenta => BG_MAGENTA,
87                Color::Cyan => BG_CYAN,
88            },
89        };
90        str.to_string()
91    }
92}
93
94impl Display for Color {
95    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96        write!(f, "{}", self.get_str(DisplayType::Text))
97    }
98}
99
100impl Display for ColorType {
101    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
102        write!(f, "{}", self.get_str(DisplayType::Text))
103    }
104}
105
106impl ColorDisplay for ColorType {
107    fn get_str(&self, display_type: DisplayType) -> String {
108        match self {
109            ColorType::Color256(fg) => match display_type {
110                DisplayType::Text => color256_fg_color(*fg),
111                DisplayType::Background => color256_bg_color(*fg),
112            },
113            ColorType::Rgb(r, g, b) => match display_type {
114                DisplayType::Text => rgb_fg_color(*r, *g, *b),
115                DisplayType::Background => rgb_bg_color(*r, *g, *b),
116            },
117            ColorType::Use(color) => color.get_str(display_type.clone()),
118        }
119    }
120}
121
122impl Default for ColorType {
123    fn default() -> Self {
124        ColorType::Use(Color::Default)
125    }
126}