ae_renderable/colour/
mod.rs

1use serde::{Deserialize, Serialize};
2
3struct Hexcode(String);
4
5impl TryFrom<&str> for Hexcode {
6    type Error = &'static str;
7
8    fn try_from(value: &str) -> Result<Self, Self::Error> {
9        if value.chars().nth(0) == Some('#') && value.len() == 7 {
10            Ok(Self(value.to_owned()))
11        } else {
12            Err("Not a valid hex string")
13        }
14    }
15}
16
17impl ToString for Hexcode {
18    fn to_string(&self) -> String {
19        self.0.clone()
20    }
21}
22
23#[derive(Serialize, Deserialize, Clone, Debug)]
24pub enum Colour {
25    Blue,
26    Red,
27    Green,
28    White,
29    Black,
30}
31
32impl ToString for Colour {
33    fn to_string(&self) -> String {
34        match self {
35            Colour::Blue => Hexcode::try_from("#0000ff").unwrap().to_string(),
36            Colour::Red => Hexcode::try_from("#ff0000").unwrap().to_string(),
37            Colour::Green => Hexcode::try_from("#00ff00").unwrap().to_string(),
38            Colour::White => Hexcode::try_from("#ffffff").unwrap().to_string(),
39            Colour::Black => Hexcode::try_from("#000000").unwrap().to_string(),
40        }
41    }
42}