owo_colors/colors/
custom.rs

1use crate::Color;
2
3const U8_TO_STR: [[u8; 3]; 256] = generate_lookup();
4
5const fn generate_lookup() -> [[u8; 3]; 256] {
6    let mut table = [[0, 0, 0]; 256];
7
8    let mut i = 0;
9    while i < 256 {
10        table[i] = [
11            b'0' + (i / 100) as u8,
12            b'0' + (i / 10 % 10) as u8,
13            b'0' + (i % 10) as u8,
14        ];
15        i += 1;
16    }
17
18    table
19}
20
21enum Plane {
22    Fg,
23    Bg,
24}
25
26const fn rgb_to_ansi(r: u8, g: u8, b: u8, plane: Plane) -> [u8; 19] {
27    let mut buf = match plane {
28        Plane::Fg => *b"\x1b[38;2;rrr;ggg;bbbm",
29        Plane::Bg => *b"\x1b[48;2;rrr;ggg;bbbm",
30    };
31
32    let r = U8_TO_STR[r as usize];
33    let g = U8_TO_STR[g as usize];
34    let b = U8_TO_STR[b as usize];
35
36    // r 7
37    buf[7] = r[0];
38    buf[8] = r[1];
39    buf[9] = r[2];
40
41    // g 11
42    buf[11] = g[0];
43    buf[12] = g[1];
44    buf[13] = g[2];
45
46    // b 15
47    buf[15] = b[0];
48    buf[16] = b[1];
49    buf[17] = b[2];
50
51    buf
52}
53
54const fn rgb_to_ansi_color(r: u8, g: u8, b: u8, plane: Plane) -> [u8; 16] {
55    let mut buf = match plane {
56        Plane::Fg => *b"38;2;rrr;ggg;bbb",
57        Plane::Bg => *b"48;2;rrr;ggg;bbb",
58    };
59
60    let r = U8_TO_STR[r as usize];
61    let g = U8_TO_STR[g as usize];
62    let b = U8_TO_STR[b as usize];
63
64    // r 5
65    buf[5] = r[0];
66    buf[6] = r[1];
67    buf[7] = r[2];
68
69    // g 9
70    buf[9] = g[0];
71    buf[10] = g[1];
72    buf[11] = g[2];
73
74    // b 13
75    buf[13] = b[0];
76    buf[14] = b[1];
77    buf[15] = b[2];
78
79    buf
80}
81
82/// A custom RGB color, determined at compile time
83pub struct CustomColor<const R: u8, const G: u8, const B: u8>;
84
85/// This exists since unwrap() isn't const-safe (it invokes formatting infrastructure)
86const fn bytes_to_str(bytes: &'static [u8]) -> &'static str {
87    match core::str::from_utf8(bytes) {
88        Ok(o) => o,
89        Err(_e) => panic!("Const parsing &[u8] to a string failed!"),
90    }
91}
92
93impl<const R: u8, const G: u8, const B: u8> CustomColor<R, G, B> {
94    const ANSI_FG_U8: [u8; 19] = rgb_to_ansi(R, G, B, Plane::Fg);
95    const ANSI_BG_U8: [u8; 19] = rgb_to_ansi(R, G, B, Plane::Bg);
96    const RAW_ANSI_FG_U8: [u8; 16] = rgb_to_ansi_color(R, G, B, Plane::Fg);
97    const RAW_ANSI_BG_U8: [u8; 16] = rgb_to_ansi_color(R, G, B, Plane::Bg);
98}
99
100impl<const R: u8, const G: u8, const B: u8> crate::private::Sealed for CustomColor<R, G, B> {}
101
102impl<const R: u8, const G: u8, const B: u8> Color for CustomColor<R, G, B> {
103    const ANSI_FG: &'static str = bytes_to_str(&Self::ANSI_FG_U8);
104    const ANSI_BG: &'static str = bytes_to_str(&Self::ANSI_BG_U8);
105    const RAW_ANSI_FG: &'static str = bytes_to_str(&Self::RAW_ANSI_FG_U8);
106    const RAW_ANSI_BG: &'static str = bytes_to_str(&Self::RAW_ANSI_BG_U8);
107
108    #[doc(hidden)]
109    type DynEquivalent = crate::Rgb;
110
111    #[doc(hidden)]
112    const DYN_EQUIVALENT: Self::DynEquivalent = crate::Rgb(R, G, B);
113
114    #[doc(hidden)]
115    const DYN_COLORS_EQUIVALENT: crate::DynColors = crate::DynColors::Rgb(R, G, B);
116}