wasm_game_lib/graphics/
color.rs

1/// A color.
2#[derive(Debug)]
3pub struct Color {
4    pub red: u8,
5    pub green: u8,
6    pub blue: u8,
7    pub alpha: u8
8}
9
10impl Color {
11    /// Create a color with a alpha value of 255
12    pub fn new(red: u8, green: u8, blue: u8) -> Color {
13        Color {
14            red,
15            green,
16            blue,
17            alpha: 255
18        }
19    }
20
21    /// Create a color
22    pub fn new_with_alpha(red: u8, green: u8, blue: u8, alpha: u8) -> Color {
23        Color {
24            red,
25            green,
26            blue,
27            alpha
28        }
29    }
30
31    /// Green color
32    pub fn green() -> Color {
33        Color {
34            red: 0,
35            green: 128,
36            blue: 0,
37            alpha: 255
38        }
39    }
40
41    /// Yellow color
42    pub fn yellow() -> Color {
43        Color {
44            red: 255,
45            green: 255,
46            blue: 0,
47            alpha: 255
48        }
49    }
50
51    /// Orange color
52    pub fn orange() -> Color {
53        Color {
54            red: 255,
55            green: 165,
56            blue: 0,
57            alpha: 255
58        }
59    }
60
61    /// Red color
62    pub fn red() -> Color {
63        Color {
64            red: 255,
65            green: 0,
66            blue: 0,
67            alpha: 255
68        }
69    }
70
71    /// Blue color
72    pub fn blue() -> Color {
73        Color {
74            red: 0,
75            green: 0,
76            blue: 255,
77            alpha: 255
78        }
79    }
80
81    /// Cyan color
82    pub fn cyan() -> Color {
83        Color {
84            red: 0,
85            green: 255,
86            blue: 255,
87            alpha: 255
88        }
89    }
90
91    /// Grey color
92    pub fn grey() -> Color {
93        Color {
94            red: 128,
95            green: 128,
96            blue: 128,
97            alpha: 255
98        }
99    }
100
101    /// Pink color
102    pub fn pink() -> Color {
103        Color {
104            red: 255,
105            green: 192,
106            blue: 203,
107            alpha: 255
108        }
109    }
110
111    /// Purple color
112    pub fn purple() -> Color {
113        Color {
114            red: 128,
115            green: 0,
116            blue: 128,
117            alpha: 255
118        }
119    }
120
121    /// No color
122    pub fn black() -> Color {
123        Color {
124            red: 0,
125            green: 0,
126            blue: 0,
127            alpha: 255
128        }
129    }
130
131    /// Pure white
132    pub fn white() -> Color {
133        Color {
134            red: 255,
135            green: 255,
136            blue: 255,
137            alpha: 255
138        }
139    }
140}
141
142impl ToString for Color {
143    fn to_string(&self) -> String {
144        let mut color = String::from("rgba(");
145        color.push_str(&self.red.to_string());
146        color.push_str(",");
147        color.push_str(&self.green.to_string());
148        color.push_str(",");
149        color.push_str(&self.blue.to_string());
150        color.push_str(",");
151        color.push_str(&(self.alpha as f64 / 255.0).to_string());
152        color.push_str(")");
153
154        color
155    }
156}
157
158#[cfg(test)]
159mod test {
160    #[test]
161    fn color_to_string() {
162        use super::Color;
163
164        assert_eq!(&Color::purple().to_string(), "rgba(128,0,128,1)");
165    }
166}