nvim_gtk/
color.rs

1use std;
2use std::borrow::Cow;
3
4use gdk;
5
6#[derive(Clone, PartialEq, Debug)]
7pub struct Color(pub f64, pub f64, pub f64);
8
9pub const COLOR_BLACK: Color = Color(0.0, 0.0, 0.0);
10pub const COLOR_WHITE: Color = Color(1.0, 1.0, 1.0);
11pub const COLOR_RED: Color = Color(1.0, 0.0, 0.0);
12
13impl<'a> From<&'a Color> for gdk::RGBA {
14    fn from(color: &Color) -> Self {
15        gdk::RGBA {
16            red: color.0,
17            green: color.1,
18            blue: color.2,
19            alpha: 1.0,
20        }
21    }
22}
23
24impl Color {
25    pub fn from_cterm(idx: u8) -> Color {
26        let color = TERMINAL_COLORS[usize::from(idx)];
27        Color(
28            color.0 as f64 / 255.0,
29            color.1 as f64 / 255.0,
30            color.2 as f64 / 255.0,
31        )
32    }
33
34    pub fn from_indexed_color(indexed_color: u64) -> Color {
35        let r = ((indexed_color >> 16) & 0xff) as f64;
36        let g = ((indexed_color >> 8) & 0xff) as f64;
37        let b = (indexed_color & 0xff) as f64;
38        Color(r / 255.0, g / 255.0, b / 255.0)
39    }
40
41    pub fn to_u16(&self) -> (u16, u16, u16) {
42        (
43            (std::u16::MAX as f64 * self.0) as u16,
44            (std::u16::MAX as f64 * self.1) as u16,
45            (std::u16::MAX as f64 * self.2) as u16,
46        )
47    }
48
49    pub fn to_hex(&self) -> String {
50        format!(
51            "#{:02X}{:02X}{:02X}",
52            (self.0 * 255.0) as u8,
53            (self.1 * 255.0) as u8,
54            (self.2 * 255.0) as u8
55        )
56    }
57
58    pub fn inverse(&self, inverse_level: f64) -> Cow<Color> {
59        debug_assert!(inverse_level >= 0.0 && inverse_level <= 1.0);
60
61        if inverse_level <= 0.000001 {
62            Cow::Borrowed(self)
63        } else {
64            Cow::Owned(Color(
65                (inverse_level - self.0).abs(),
66                (inverse_level - self.1).abs(),
67                (inverse_level - self.2).abs(),
68            ))
69        }
70    }
71}
72
73
74/// From https://jonasjacek.github.io/colors/ 
75const TERMINAL_COLORS: [(u8, u8, u8); 256] = [
76    (0, 0, 0),
77    (128, 0, 0),
78    (0, 128, 0),
79    (128, 128, 0),
80    (0, 0, 128),
81    (128, 0, 128),
82    (0, 128, 128),
83    (192, 192, 192),
84    (128, 128, 128),
85    (255, 0, 0),
86    (0, 255, 0),
87    (255, 255, 0),
88    (0, 0, 255),
89    (255, 0, 255),
90    (0, 255, 255),
91    (255, 255, 255),
92    (0, 0, 0),
93    (0, 0, 95),
94    (0, 0, 135),
95    (0, 0, 175),
96    (0, 0, 215),
97    (0, 0, 255),
98    (0, 95, 0),
99    (0, 95, 95),
100    (0, 95, 135),
101    (0, 95, 175),
102    (0, 95, 215),
103    (0, 95, 255),
104    (0, 135, 0),
105    (0, 135, 95),
106    (0, 135, 135),
107    (0, 135, 175),
108    (0, 135, 215),
109    (0, 135, 255),
110    (0, 175, 0),
111    (0, 175, 95),
112    (0, 175, 135),
113    (0, 175, 175),
114    (0, 175, 215),
115    (0, 175, 255),
116    (0, 215, 0),
117    (0, 215, 95),
118    (0, 215, 135),
119    (0, 215, 175),
120    (0, 215, 215),
121    (0, 215, 255),
122    (0, 255, 0),
123    (0, 255, 95),
124    (0, 255, 135),
125    (0, 255, 175),
126    (0, 255, 215),
127    (0, 255, 255),
128    (95, 0, 0),
129    (95, 0, 95),
130    (95, 0, 135),
131    (95, 0, 175),
132    (95, 0, 215),
133    (95, 0, 255),
134    (95, 95, 0),
135    (95, 95, 95),
136    (95, 95, 135),
137    (95, 95, 175),
138    (95, 95, 215),
139    (95, 95, 255),
140    (95, 135, 0),
141    (95, 135, 95),
142    (95, 135, 135),
143    (95, 135, 175),
144    (95, 135, 215),
145    (95, 135, 255),
146    (95, 175, 0),
147    (95, 175, 95),
148    (95, 175, 135),
149    (95, 175, 175),
150    (95, 175, 215),
151    (95, 175, 255),
152    (95, 215, 0),
153    (95, 215, 95),
154    (95, 215, 135),
155    (95, 215, 175),
156    (95, 215, 215),
157    (95, 215, 255),
158    (95, 255, 0),
159    (95, 255, 95),
160    (95, 255, 135),
161    (95, 255, 175),
162    (95, 255, 215),
163    (95, 255, 255),
164    (135, 0, 0),
165    (135, 0, 95),
166    (135, 0, 135),
167    (135, 0, 175),
168    (135, 0, 215),
169    (135, 0, 255),
170    (135, 95, 0),
171    (135, 95, 95),
172    (135, 95, 135),
173    (135, 95, 175),
174    (135, 95, 215),
175    (135, 95, 255),
176    (135, 135, 0),
177    (135, 135, 95),
178    (135, 135, 135),
179    (135, 135, 175),
180    (135, 135, 215),
181    (135, 135, 255),
182    (135, 175, 0),
183    (135, 175, 95),
184    (135, 175, 135),
185    (135, 175, 175),
186    (135, 175, 215),
187    (135, 175, 255),
188    (135, 215, 0),
189    (135, 215, 95),
190    (135, 215, 135),
191    (135, 215, 175),
192    (135, 215, 215),
193    (135, 215, 255),
194    (135, 255, 0),
195    (135, 255, 95),
196    (135, 255, 135),
197    (135, 255, 175),
198    (135, 255, 215),
199    (135, 255, 255),
200    (175, 0, 0),
201    (175, 0, 95),
202    (175, 0, 135),
203    (175, 0, 175),
204    (175, 0, 215),
205    (175, 0, 255),
206    (175, 95, 0),
207    (175, 95, 95),
208    (175, 95, 135),
209    (175, 95, 175),
210    (175, 95, 215),
211    (175, 95, 255),
212    (175, 135, 0),
213    (175, 135, 95),
214    (175, 135, 135),
215    (175, 135, 175),
216    (175, 135, 215),
217    (175, 135, 255),
218    (175, 175, 0),
219    (175, 175, 95),
220    (175, 175, 135),
221    (175, 175, 175),
222    (175, 175, 215),
223    (175, 175, 255),
224    (175, 215, 0),
225    (175, 215, 95),
226    (175, 215, 135),
227    (175, 215, 175),
228    (175, 215, 215),
229    (175, 215, 255),
230    (175, 255, 0),
231    (175, 255, 95),
232    (175, 255, 135),
233    (175, 255, 175),
234    (175, 255, 215),
235    (175, 255, 255),
236    (215, 0, 0),
237    (215, 0, 95),
238    (215, 0, 135),
239    (215, 0, 175),
240    (215, 0, 215),
241    (215, 0, 255),
242    (215, 95, 0),
243    (215, 95, 95),
244    (215, 95, 135),
245    (215, 95, 175),
246    (215, 95, 215),
247    (215, 95, 255),
248    (215, 135, 0),
249    (215, 135, 95),
250    (215, 135, 135),
251    (215, 135, 175),
252    (215, 135, 215),
253    (215, 135, 255),
254    (215, 175, 0),
255    (215, 175, 95),
256    (215, 175, 135),
257    (215, 175, 175),
258    (215, 175, 215),
259    (215, 175, 255),
260    (215, 215, 0),
261    (215, 215, 95),
262    (215, 215, 135),
263    (215, 215, 175),
264    (215, 215, 215),
265    (215, 215, 255),
266    (215, 255, 0),
267    (215, 255, 95),
268    (215, 255, 135),
269    (215, 255, 175),
270    (215, 255, 215),
271    (215, 255, 255),
272    (255, 0, 0),
273    (255, 0, 95),
274    (255, 0, 135),
275    (255, 0, 175),
276    (255, 0, 215),
277    (255, 0, 255),
278    (255, 95, 0),
279    (255, 95, 95),
280    (255, 95, 135),
281    (255, 95, 175),
282    (255, 95, 215),
283    (255, 95, 255),
284    (255, 135, 0),
285    (255, 135, 95),
286    (255, 135, 135),
287    (255, 135, 175),
288    (255, 135, 215),
289    (255, 135, 255),
290    (255, 175, 0),
291    (255, 175, 95),
292    (255, 175, 135),
293    (255, 175, 175),
294    (255, 175, 215),
295    (255, 175, 255),
296    (255, 215, 0),
297    (255, 215, 95),
298    (255, 215, 135),
299    (255, 215, 175),
300    (255, 215, 215),
301    (255, 215, 255),
302    (255, 255, 0),
303    (255, 255, 95),
304    (255, 255, 135),
305    (255, 255, 175),
306    (255, 255, 215),
307    (255, 255, 255),
308    (8, 8, 8),
309    (18, 18, 18),
310    (28, 28, 28),
311    (38, 38, 38),
312    (48, 48, 48),
313    (58, 58, 58),
314    (68, 68, 68),
315    (78, 78, 78),
316    (88, 88, 88),
317    (98, 98, 98),
318    (108, 108, 108),
319    (118, 118, 118),
320    (128, 128, 128),
321    (138, 138, 138),
322    (148, 148, 148),
323    (158, 158, 158),
324    (168, 168, 168),
325    (178, 178, 178),
326    (188, 188, 188),
327    (198, 198, 198),
328    (208, 208, 208),
329    (218, 218, 218),
330    (228, 228, 228),
331    (238, 238, 238),
332];
333
334#[cfg(test)]
335mod tests {
336    use super::*;
337
338    #[test]
339    fn test_to_hex() {
340        let col = Color(0.0, 1.0, 0.0);
341        assert_eq!("#00FF00", &col.to_hex());
342    }
343}