fj_interop/
color.rs

1/// RGBA color
2#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
3pub struct Color(pub [u8; 4]);
4
5impl Default for Color {
6    fn default() -> Self {
7        // The default color is red. This is an arbitrary choice.
8        Self([255, 0, 0, 255])
9    }
10}
11
12impl From<[u8; 4]> for Color {
13    fn from(rgba: [u8; 4]) -> Self {
14        Self(rgba)
15    }
16}
17
18impl From<[u8; 3]> for Color {
19    fn from([r, g, b]: [u8; 3]) -> Self {
20        Self([r, g, b, 255])
21    }
22}
23
24impl From<[f64; 4]> for Color {
25    fn from(rgba: [f64; 4]) -> Self {
26        let rgba = rgba.map(|value| {
27            let value = value.clamp(0., 1.);
28            let value: u8 = (value * 255.0) as u8;
29            value
30        });
31
32        Self(rgba)
33    }
34}
35
36impl From<[f64; 3]> for Color {
37    fn from([r, g, b]: [f64; 3]) -> Self {
38        Self::from([r, g, b, 1.])
39    }
40}