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
53
54
use super::*;
use std::fmt;

#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub struct RgbaColor {
    pub red: u8,
    pub green: u8,
    pub blue: u8,
    pub alpha: u8,
}

impl fmt::Display for RgbaColor {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "rgba({}, {}, {}, {})",
            self.red, self.green, self.blue, self.alpha
        )
    }
}

impl RgbaColor {
    pub fn new(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
        Self {
            red,
            green,
            blue,
            alpha,
        }
    }
}

// RGBAu8 -> RGB
impl From<RgbaColor> for Color {
    fn from(rgba: RgbaColor) -> Self {
        Color {
            red: rgba.red as Float / 255.0,
            green: rgba.green as Float / 255.0,
            blue: rgba.blue as Float / 255.0,
            alpha: rgba.alpha as Float / 255.0,
        }
    }
}

impl From<Color> for RgbaColor {
    fn from(color: Color) -> Self {
        RgbaColor {
            red: (color.red * 255.0).round() as u8,
            green: (color.green * 255.0).round() as u8,
            blue: (color.blue * 255.0).round() as u8,
            alpha: (color.alpha * 255.0).round() as u8,
        }
    }
}