primitives/foundation/colorspace/
rgb.rs1use super::{Color, Float};
2use std::fmt;
3
4#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
6pub struct RgbColor {
7 pub red: u8,
9 pub green: u8,
11 pub blue: u8,
13}
14
15impl RgbColor {
16 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
28impl 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
40impl 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}