owo_colors/colors/
custom.rs

1use crate::Color;
2
3const U8_TO_STR: [[u8; 3]; 256] = [
4    [48, 48, 48],
5    [48, 48, 49],
6    [48, 48, 50],
7    [48, 48, 51],
8    [48, 48, 52],
9    [48, 48, 53],
10    [48, 48, 54],
11    [48, 48, 55],
12    [48, 48, 56],
13    [48, 48, 57],
14    [48, 49, 48],
15    [48, 49, 49],
16    [48, 49, 50],
17    [48, 49, 51],
18    [48, 49, 52],
19    [48, 49, 53],
20    [48, 49, 54],
21    [48, 49, 55],
22    [48, 49, 56],
23    [48, 49, 57],
24    [48, 50, 48],
25    [48, 50, 49],
26    [48, 50, 50],
27    [48, 50, 51],
28    [48, 50, 52],
29    [48, 50, 53],
30    [48, 50, 54],
31    [48, 50, 55],
32    [48, 50, 56],
33    [48, 50, 57],
34    [48, 51, 48],
35    [48, 51, 49],
36    [48, 51, 50],
37    [48, 51, 51],
38    [48, 51, 52],
39    [48, 51, 53],
40    [48, 51, 54],
41    [48, 51, 55],
42    [48, 51, 56],
43    [48, 51, 57],
44    [48, 52, 48],
45    [48, 52, 49],
46    [48, 52, 50],
47    [48, 52, 51],
48    [48, 52, 52],
49    [48, 52, 53],
50    [48, 52, 54],
51    [48, 52, 55],
52    [48, 52, 56],
53    [48, 52, 57],
54    [48, 53, 48],
55    [48, 53, 49],
56    [48, 53, 50],
57    [48, 53, 51],
58    [48, 53, 52],
59    [48, 53, 53],
60    [48, 53, 54],
61    [48, 53, 55],
62    [48, 53, 56],
63    [48, 53, 57],
64    [48, 54, 48],
65    [48, 54, 49],
66    [48, 54, 50],
67    [48, 54, 51],
68    [48, 54, 52],
69    [48, 54, 53],
70    [48, 54, 54],
71    [48, 54, 55],
72    [48, 54, 56],
73    [48, 54, 57],
74    [48, 55, 48],
75    [48, 55, 49],
76    [48, 55, 50],
77    [48, 55, 51],
78    [48, 55, 52],
79    [48, 55, 53],
80    [48, 55, 54],
81    [48, 55, 55],
82    [48, 55, 56],
83    [48, 55, 57],
84    [48, 56, 48],
85    [48, 56, 49],
86    [48, 56, 50],
87    [48, 56, 51],
88    [48, 56, 52],
89    [48, 56, 53],
90    [48, 56, 54],
91    [48, 56, 55],
92    [48, 56, 56],
93    [48, 56, 57],
94    [48, 57, 48],
95    [48, 57, 49],
96    [48, 57, 50],
97    [48, 57, 51],
98    [48, 57, 52],
99    [48, 57, 53],
100    [48, 57, 54],
101    [48, 57, 55],
102    [48, 57, 56],
103    [48, 57, 57],
104    [49, 48, 48],
105    [49, 48, 49],
106    [49, 48, 50],
107    [49, 48, 51],
108    [49, 48, 52],
109    [49, 48, 53],
110    [49, 48, 54],
111    [49, 48, 55],
112    [49, 48, 56],
113    [49, 48, 57],
114    [49, 49, 48],
115    [49, 49, 49],
116    [49, 49, 50],
117    [49, 49, 51],
118    [49, 49, 52],
119    [49, 49, 53],
120    [49, 49, 54],
121    [49, 49, 55],
122    [49, 49, 56],
123    [49, 49, 57],
124    [49, 50, 48],
125    [49, 50, 49],
126    [49, 50, 50],
127    [49, 50, 51],
128    [49, 50, 52],
129    [49, 50, 53],
130    [49, 50, 54],
131    [49, 50, 55],
132    [49, 50, 56],
133    [49, 50, 57],
134    [49, 51, 48],
135    [49, 51, 49],
136    [49, 51, 50],
137    [49, 51, 51],
138    [49, 51, 52],
139    [49, 51, 53],
140    [49, 51, 54],
141    [49, 51, 55],
142    [49, 51, 56],
143    [49, 51, 57],
144    [49, 52, 48],
145    [49, 52, 49],
146    [49, 52, 50],
147    [49, 52, 51],
148    [49, 52, 52],
149    [49, 52, 53],
150    [49, 52, 54],
151    [49, 52, 55],
152    [49, 52, 56],
153    [49, 52, 57],
154    [49, 53, 48],
155    [49, 53, 49],
156    [49, 53, 50],
157    [49, 53, 51],
158    [49, 53, 52],
159    [49, 53, 53],
160    [49, 53, 54],
161    [49, 53, 55],
162    [49, 53, 56],
163    [49, 53, 57],
164    [49, 54, 48],
165    [49, 54, 49],
166    [49, 54, 50],
167    [49, 54, 51],
168    [49, 54, 52],
169    [49, 54, 53],
170    [49, 54, 54],
171    [49, 54, 55],
172    [49, 54, 56],
173    [49, 54, 57],
174    [49, 55, 48],
175    [49, 55, 49],
176    [49, 55, 50],
177    [49, 55, 51],
178    [49, 55, 52],
179    [49, 55, 53],
180    [49, 55, 54],
181    [49, 55, 55],
182    [49, 55, 56],
183    [49, 55, 57],
184    [49, 56, 48],
185    [49, 56, 49],
186    [49, 56, 50],
187    [49, 56, 51],
188    [49, 56, 52],
189    [49, 56, 53],
190    [49, 56, 54],
191    [49, 56, 55],
192    [49, 56, 56],
193    [49, 56, 57],
194    [49, 57, 48],
195    [49, 57, 49],
196    [49, 57, 50],
197    [49, 57, 51],
198    [49, 57, 52],
199    [49, 57, 53],
200    [49, 57, 54],
201    [49, 57, 55],
202    [49, 57, 56],
203    [49, 57, 57],
204    [50, 48, 48],
205    [50, 48, 49],
206    [50, 48, 50],
207    [50, 48, 51],
208    [50, 48, 52],
209    [50, 48, 53],
210    [50, 48, 54],
211    [50, 48, 55],
212    [50, 48, 56],
213    [50, 48, 57],
214    [50, 49, 48],
215    [50, 49, 49],
216    [50, 49, 50],
217    [50, 49, 51],
218    [50, 49, 52],
219    [50, 49, 53],
220    [50, 49, 54],
221    [50, 49, 55],
222    [50, 49, 56],
223    [50, 49, 57],
224    [50, 50, 48],
225    [50, 50, 49],
226    [50, 50, 50],
227    [50, 50, 51],
228    [50, 50, 52],
229    [50, 50, 53],
230    [50, 50, 54],
231    [50, 50, 55],
232    [50, 50, 56],
233    [50, 50, 57],
234    [50, 51, 48],
235    [50, 51, 49],
236    [50, 51, 50],
237    [50, 51, 51],
238    [50, 51, 52],
239    [50, 51, 53],
240    [50, 51, 54],
241    [50, 51, 55],
242    [50, 51, 56],
243    [50, 51, 57],
244    [50, 52, 48],
245    [50, 52, 49],
246    [50, 52, 50],
247    [50, 52, 51],
248    [50, 52, 52],
249    [50, 52, 53],
250    [50, 52, 54],
251    [50, 52, 55],
252    [50, 52, 56],
253    [50, 52, 57],
254    [50, 53, 48],
255    [50, 53, 49],
256    [50, 53, 50],
257    [50, 53, 51],
258    [50, 53, 52],
259    [50, 53, 53],
260];
261
262const fn rgb_to_ansi(r: u8, g: u8, b: u8, is_fg: bool) -> [u8; 19] {
263    let mut buf = if is_fg {
264        *b"\x1b[38;2;rrr;ggg;bbbm"
265    } else {
266        *b"\x1b[48;2;rrr;ggg;bbbm"
267    };
268
269    let r = U8_TO_STR[r as usize];
270    let g = U8_TO_STR[g as usize];
271    let b = U8_TO_STR[b as usize];
272
273    // r 7
274    buf[7] = r[0];
275    buf[8] = r[1];
276    buf[9] = r[2];
277
278    // g 11
279    buf[11] = g[0];
280    buf[12] = g[1];
281    buf[13] = g[2];
282
283    // b 15
284    buf[15] = b[0];
285    buf[16] = b[1];
286    buf[17] = b[2];
287
288    buf
289}
290
291const fn rgb_to_ansi_color(r: u8, g: u8, b: u8, is_fg: bool) -> [u8; 16] {
292    let mut buf = if is_fg {
293        *b"38;2;rrr;ggg;bbb"
294    } else {
295        *b"48;2;rrr;ggg;bbb"
296    };
297
298    let r = U8_TO_STR[r as usize];
299    let g = U8_TO_STR[g as usize];
300    let b = U8_TO_STR[b as usize];
301
302    // r 5
303    buf[5] = r[0];
304    buf[6] = r[1];
305    buf[7] = r[2];
306
307    // g 9
308    buf[9] = g[0];
309    buf[10] = g[1];
310    buf[11] = g[2];
311
312    // b 13
313    buf[13] = b[0];
314    buf[14] = b[1];
315    buf[15] = b[2];
316
317    buf
318}
319
320/// A custom RGB color, determined at compile time
321pub struct CustomColor<const R: u8, const G: u8, const B: u8>;
322
323#[allow(clippy::transmute_bytes_to_str)]
324impl<const R: u8, const G: u8, const B: u8> Color for CustomColor<R, G, B> {
325    const ANSI_FG: &'static str =
326        unsafe { core::mem::transmute(&rgb_to_ansi(R, G, B, true) as &[u8]) };
327    const ANSI_BG: &'static str =
328        unsafe { core::mem::transmute(&rgb_to_ansi(R, G, B, false) as &[u8]) };
329
330    const RAW_ANSI_FG: &'static str =
331        unsafe { core::mem::transmute(&rgb_to_ansi_color(R, G, B, true) as &[u8]) };
332    const RAW_ANSI_BG: &'static str =
333        unsafe { core::mem::transmute(&rgb_to_ansi_color(R, G, B, false) as &[u8]) };
334
335    #[doc(hidden)]
336    type DynEquivelant = crate::Rgb;
337
338    #[doc(hidden)]
339    const DYN_EQUIVELANT: Self::DynEquivelant = crate::Rgb(R, G, B);
340
341    #[doc(hidden)]
342    fn into_dyncolors() -> crate::DynColors {
343        crate::DynColors::Rgb(R, G, B)
344    }
345}