turtle_svg/
color.rs

1/// The unique Color model for this crate
2#[derive(Clone, Copy)]
3pub struct Color {
4    /// Red
5    pub r: u8,
6    /// Green
7    pub g: u8,
8    /// Blue
9    pub b: u8,
10    /// Alpha
11    pub a: u8
12}
13
14/// Pre-defined colors
15pub enum ColorPre {
16    Red,
17    Green,
18    Blue,
19    Black,
20    White,
21    Grey,
22    Default,
23    None
24}
25
26/// Convert a pre-defined color into Color
27impl Into<Color> for ColorPre {
28    fn into(self) -> Color {
29        match self {
30            ColorPre::Red => Color::from((255, 0, 0, 255)),
31            ColorPre::Green => Color::from((0, 255, 0, 255)),
32            ColorPre::Blue => Color::from((0, 0, 255, 255)),
33            ColorPre::Black => Color::from((0, 0, 0, 255)),
34            ColorPre::White => Color::from((255, 255, 255, 255)),
35            ColorPre::Grey => Color::from((127, 127, 127, 255)),
36            ColorPre::Default => Color::from((0, 0, 0, 255)),
37            ColorPre::None => Color::from((0, 0, 0, 0)),
38        }
39    }
40}
41
42/// SVG color format
43pub type ColorSvg = String;
44
45impl Default for Color {
46    fn default() -> Self {
47        ColorPre::Default.into()
48    }
49}
50
51impl From<(u8, u8, u8, u8)> for Color {
52    fn from(color: (u8, u8, u8, u8)) -> Self {
53        let (r, g, b, a) = color;
54
55        Self { r, g, b, a }
56    }
57}
58
59impl Into<ColorSvg> for Color {
60    fn into(self) -> ColorSvg {
61        let alpha = (self.a / 255) as f64;
62
63        format!("rgba({}, {}, {}, {})", self.r, self.g, self.b, alpha)
64    }
65}