1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
pub const COLOR_HEX_BLUE_1: &str = "#0e3569";
pub const COLOR_HEX_BLUE_2: &str = "#1960b2";
pub const COLOR_HEX_BLUE_3: &str = "#3a88e2";
pub const COLOR_HEX_BLUE_4: &str = "#5095e5";
pub const COLOR_HEX_BLUE_5: &str = "#a5c9f2";
pub const COLOR_HEX_GREEN_1: &str = "#0c3300";
pub const COLOR_HEX_GREEN_2: &str = "#00400e";
pub const COLOR_HEX_GREEN_3: &str = "#005813";
pub const COLOR_HEX_GREEN_4: &str = "#117401";
pub const COLOR_HEX_GREEN_5: &str = "#038d05";
pub struct Color {
value: String,
}
impl Color {
pub fn new_from_hex(hex: &str) -> Self {
Color {
value: String::from(hex),
}
}
pub fn new_from_rgb(r: u8, g: u8, b: u8) -> Self {
let value = format!("rgb({},{},{})", r, g, b);
Color { value }
}
}
impl std::fmt::Display for Color {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.value)
}
}
impl Default for Color {
fn default() -> Self {
Self::new_from_hex(COLOR_HEX_BLUE_2)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_from_hex() {
let color = Color::new_from_hex(COLOR_HEX_GREEN_5);
assert_eq!(color.value, COLOR_HEX_GREEN_5);
}
#[test]
fn new_from_rgb() {
let color = Color::new_from_rgb(253, 185, 200);
assert_eq!(color.value, "rgb(253,185,200)".to_string());
}
}