primitives/foundation/colorspace/
rgb.rs

1use super::{Color, Float};
2use std::fmt;
3
4/// Rgb color representation with u8 components
5#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
6pub struct RgbColor {
7    /// Red component
8    pub red: u8,
9    /// Green component
10    pub green: u8,
11    /// Blue component
12    pub blue: u8,
13}
14
15impl RgbColor {
16    /// Create new Rgb color with parameters
17    pub fn new(red: u8, green: u8, blue: u8) -> Self {
18        Self { red, green, blue }
19    }
20}
21
22impl fmt::Display for RgbColor {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        write!(f, "rgb({}, {}, {})", self.red, self.green, self.blue)
25    }
26}
27
28// RGBu8 -> RGB
29impl From<RgbColor> for Color {
30    fn from(c: RgbColor) -> Self {
31        Color {
32            red: c.red as Float / 255.0,
33            green: c.green as Float / 255.0,
34            blue: c.blue as Float / 255.0,
35            alpha: 1.,
36        }
37    }
38}
39
40// RGB -> RGBu8
41impl From<Color> for RgbColor {
42    fn from(c: Color) -> Self {
43        RgbColor {
44            red: (c.red * 255.0).round() as u8,
45            green: (c.green * 255.0).round() as u8,
46            blue: (c.blue * 255.0).round() as u8,
47        }
48    }
49}