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
// Zero crate colour library
#![no_std]
#![warn(clippy::pedantic, clippy::all)]
use core::fmt;

// Foreground colours
pub const FG_BLACK: &str = "";
pub const FG_RED: &str = "";
pub const FG_GREEN: &str = "";
pub const FG_YELLOW: &str = "";
pub const FG_BLUE: &str = "";
pub const FG_PURPLE: &str = "";
pub const FG_CYAN: &str = "";
pub const FG_WHITE: &str = "";
pub const FG_LIGHTBLACK: &str = "";
pub const FG_LIGHTRED: &str = "";
pub const FG_LIGHTGREEN: &str = "";
pub const FG_LIGHTYELLOW: &str = "";
pub const FG_LIGHTBLUE: &str = "";
pub const FG_LIGHTPURPLE: &str = "";
pub const FG_LIGHTCYAN: &str = "";
pub const FG_LIGHTWHITE: &str = "";

// Background colours
pub const BG_BLACK: &str = "";
pub const BG_RED: &str = "";
pub const BG_GREEN: &str = "";
pub const BG_YELLOW: &str = "";
pub const BG_BLUE: &str = "";
pub const BG_PURPLE: &str = "";
pub const BG_CYAN: &str = "";
pub const BG_WHITE: &str = "";
pub const BG_LIGHTBLACK: &str = "";
pub const BG_LIGHTRED: &str = "";
pub const BG_LIGHTGREEN: &str = "";
pub const BG_LIGHTYELLOW: &str = "";
pub const BG_LIGHTBLUE: &str = "";
pub const BG_LIGHTPURPLE: &str = "";
pub const BG_LIGHTCYAN: &str = "";
pub const BG_LIGHTWHITE: &str = "";

// Resetting of colours
pub const FG_RESET: &str = "";
pub const BG_RESET: &str = "";

// Text styles
pub const BOLD: &str = "";
pub const BOLD_RESET: &str = "";
pub const UNDERLINE: &str = "";
pub const UNDERLINE_RESET: &str = "";
pub const STRIKE: &str = "";
pub const STRIKE_RESET: &str = "";
pub const ITALIC: &str = "";
pub const ITALIC_RESET: &str = "";
pub const INVERSE: &str = "";
pub const INVERSE_RESET: &str = "";
pub const FAINT: &str = "";
pub const FAINT_RESET: &str = "";

// Resetting of everything
pub const RESET: &str = "";

/// Foreground colours for setting text colour
#[derive(Debug, Clone, Copy)]
pub enum Fg {
    Rgb(u8, u8, u8),
    Hex(&'static str),
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Purple,
    Cyan,
    White,
    LightBlack,
    LightRed,
    LightGreen,
    LightYellow,
    LightBlue,
    LightPurple,
    LightCyan,
    LightWhite,
    Reset,
}

// Allow use in format macros
impl fmt::Display for Fg {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Fg::Rgb(r, g, b) => write!(f, "[38;2;{};{};{}m", r, g, b),
            Fg::Hex(h) => {
                let rgb = hex_to_rgb(h);
                write!(f, "[38;2;{};{};{}m", rgb.0, rgb.1, rgb.2)
            }
            col => write!(
                f,
                "{}",
                match col {
                    Fg::Black => FG_BLACK,
                    Fg::Red => FG_RED,
                    Fg::Green => FG_GREEN,
                    Fg::Yellow => FG_YELLOW,
                    Fg::Blue => FG_BLUE,
                    Fg::Purple => FG_PURPLE,
                    Fg::Cyan => FG_CYAN,
                    Fg::White => FG_WHITE,
                    Fg::LightBlack => FG_LIGHTBLACK,
                    Fg::LightRed => FG_LIGHTRED,
                    Fg::LightGreen => FG_LIGHTGREEN,
                    Fg::LightYellow => FG_LIGHTYELLOW,
                    Fg::LightBlue => FG_LIGHTBLUE,
                    Fg::LightPurple => FG_LIGHTPURPLE,
                    Fg::LightCyan => FG_LIGHTCYAN,
                    Fg::LightWhite => FG_LIGHTWHITE,
                    Fg::Reset => FG_RESET,
                    Fg::Rgb(_, _, _) | Fg::Hex(_) => unreachable!(),
                }
            ),
        }
    }
}

/// Background colours for setting text background colour
#[derive(Debug, Clone, Copy)]
pub enum Bg {
    Rgb(u8, u8, u8),
    Hex(&'static str),
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Purple,
    Cyan,
    White,
    LightBlack,
    LightRed,
    LightGreen,
    LightYellow,
    LightBlue,
    LightPurple,
    LightCyan,
    LightWhite,
    Reset,
}

// Allow use in format macros
impl fmt::Display for Bg {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Bg::Rgb(r, g, b) => write!(f, "[48;2;{};{};{}m", r, g, b),
            Bg::Hex(h) => {
                let rgb = hex_to_rgb(h);
                write!(f, "[48;2;{};{};{}m", rgb.0, rgb.1, rgb.2)
            }
            col => write!(
                f,
                "{}",
                match col {
                    Bg::Black => BG_BLACK,
                    Bg::Red => BG_RED,
                    Bg::Green => BG_GREEN,
                    Bg::Yellow => BG_YELLOW,
                    Bg::Blue => BG_BLUE,
                    Bg::Purple => BG_PURPLE,
                    Bg::Cyan => BG_CYAN,
                    Bg::White => BG_WHITE,
                    Bg::LightBlack => BG_LIGHTBLACK,
                    Bg::LightRed => BG_LIGHTRED,
                    Bg::LightGreen => BG_LIGHTGREEN,
                    Bg::LightYellow => BG_LIGHTYELLOW,
                    Bg::LightBlue => BG_LIGHTBLUE,
                    Bg::LightPurple => BG_LIGHTPURPLE,
                    Bg::LightCyan => BG_LIGHTCYAN,
                    Bg::LightWhite => BG_LIGHTWHITE,
                    Bg::Reset => BG_RESET,
                    Bg::Rgb(_, _, _) | Bg::Hex(_) => unreachable!(),
                }
            ),
        }
    }
}

/// Style enum to style text
#[derive(Debug, Clone, Copy)]
pub enum Style {
    Bold,
    NoBold,
    Underline,
    NoUnderline,
    Strike,
    NoStrike,
    Italic,
    NoItalic,
    Inverse,
    NoInverse,
    Faint,
    NoFaint,
}

// Allow use in format macros
impl fmt::Display for Style {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Style::Bold => BOLD,
                Style::NoBold => BOLD_RESET,
                Style::Underline => UNDERLINE,
                Style::NoUnderline => UNDERLINE_RESET,
                Style::Strike => STRIKE,
                Style::NoStrike => STRIKE_RESET,
                Style::Italic => ITALIC,
                Style::NoItalic => ITALIC_RESET,
                Style::Inverse => INVERSE,
                Style::NoInverse => INVERSE_RESET,
                Style::Faint => FAINT,
                Style::NoFaint => FAINT_RESET,
            }
        )
    }
}

/// A reset type that clears all styling at once
#[derive(Debug, Clone, Copy)]
pub struct Reset;

// Allow use in format macros
impl fmt::Display for Reset {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", RESET)
    }
}

// Function to conver hex code to rgb
fn hex_to_rgb(hex: &str) -> (u8, u8, u8) {
    let mut hex = hex;
    // The '#' isn't necessary
    if hex.starts_with("#") {
        hex = &hex[1..];
    }
    // If the hex-code is invalid it defaults to black
    let mut rgb: (u8, u8, u8) = (0, 0, 0);
    if hex.len() == 6 {
        rgb.0 = hex_to_dec(&hex[0..2]);
        rgb.1 = hex_to_dec(&hex[2..4]);
        rgb.2 = hex_to_dec(&hex[4..6]);
    }
    rgb
}

fn hex_to_dec(hex: &str) -> u8 {
    u8::from_str_radix(hex, 16).unwrap_or(0)
}