lc_render/
color.rs

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