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
use super::*;

/// # hex! macro
/// to convert hex color to ansi color
/// example: hex!(#0099FF) -> "\x1b[38;2;0;99;255m"
#[macro_export]
macro_rules! hex {
($hex:expr) => {
    {
        let hex = $hex.trim_start_matches("#");
        let r = u8::from_str_radix(&hex[0..2], 16).unwrap();
        let g = u8::from_str_radix(&hex[2..4], 16).unwrap();
        let b = u8::from_str_radix(&hex[4..6], 16).unwrap();
        format!("\x1b[38;2;{};{};{}m", r, g, b)
    }
};
}

///hexbg! macro to convert hex color to ansi background color
/// example: hexbg!(#0099FF) -> "\x1b[48;2;0;99;255m\x1b[30m"
/// example: hexbg!(#0099FF, 255) -> "\x1b[48;2;0;99;255m\x1b[30m"
/// example: hexbg!(#0099FF, 255, 255, 255) -> "\x1b[48;2;0;99;255m\x1b[30m"
#[macro_export]
macro_rules! hexbg {
($hex:expr) => {
    {
        let hex = $hex.trim_start_matches("#");
        let r = u8::from_str_radix(&hex[0..2], 16).unwrap();
        let g = u8::from_str_radix(&hex[2..4], 16).unwrap();
        let b = u8::from_str_radix(&hex[4..6], 16).unwrap();
        format!("\x1b[48;2;{};{};{}m\x1b[30m", r, g, b)
    }
};
($hex:expr, $r:expr) => {
    {
        let hex = $hex.trim_start_matches("#");
        let r = u8::from_str_radix(&hex[0..2], 16).unwrap();
        let g = u8::from_str_radix(&hex[2..4], 16).unwrap();
        let b = u8::from_str_radix(&hex[4..6], 16).unwrap();
        format!("\x1b[48;2;{};{};{}m\x1b[30m", r, g, b)
    }
};
($hex:expr, $r:expr, $g:expr, $b:expr) => {
    {
        let hex = $hex.trim_start_matches("#");
        let r = u8::from_str_radix(&hex[0..2], 16).unwrap();
        let g = u8::from_str_radix(&hex[2..4], 16).unwrap();
        let b = u8::from_str_radix(&hex[4..6], 16).unwrap();
        format!("\x1b[48;2;{};{};{}m\x1b[30m", r, g, b)
    }
};
}