primitives/foundation/colorspace/
rgba.rs1use super::*;
2use std::fmt;
3
4#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
6pub struct RgbaColor {
7 pub red: u8,
9 pub green: u8,
11 pub blue: u8,
13 pub alpha: u8,
15}
16
17impl fmt::Display for RgbaColor {
18 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19 write!(
20 f,
21 "rgba({}, {}, {}, {})",
22 self.red, self.green, self.blue, self.alpha
23 )
24 }
25}
26
27impl RgbaColor {
28 pub fn new(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
30 Self {
31 red,
32 green,
33 blue,
34 alpha,
35 }
36 }
37}
38
39impl From<RgbaColor> for Color {
41 fn from(rgba: RgbaColor) -> Self {
42 Color {
43 red: rgba.red as Float / 255.0,
44 green: rgba.green as Float / 255.0,
45 blue: rgba.blue as Float / 255.0,
46 alpha: rgba.alpha as Float / 255.0,
47 }
48 }
49}
50
51impl From<Color> for RgbaColor {
52 fn from(color: Color) -> Self {
53 RgbaColor {
54 red: (color.red * 255.0).round() as u8,
55 green: (color.green * 255.0).round() as u8,
56 blue: (color.blue * 255.0).round() as u8,
57 alpha: (color.alpha * 255.0).round() as u8,
58 }
59 }
60}