Skip to main content

rocketsplash_rt/color/
cls_color.rs

1// <FILE>crates/rocketsplash-rt/src/color/cls_color.rs</FILE>
2// <DESC>Runtime color input representation</DESC>
3// <VERS>VERSION: 1.0.0</VERS>
4// <WCTX>Runtime library implementation</WCTX>
5// <CLOG>Add Color type with parsing helpers</CLOG>
6
7use rocketsplash_formats::Rgb;
8
9use crate::{parse_color, Error};
10
11#[derive(Clone, Debug)]
12pub enum Color {
13    Rgb(Rgb),
14    Hex(String),
15}
16
17impl Color {
18    pub fn hex(s: &str) -> Result<Self, Error> {
19        let rgb = parse_color(s)?;
20        Ok(Self::Rgb(rgb))
21    }
22
23    pub fn rgb(r: u8, g: u8, b: u8) -> Self {
24        Self::Rgb(Rgb { r, g, b })
25    }
26
27    pub fn to_rgb(&self) -> Result<Rgb, Error> {
28        match self {
29            Self::Rgb(rgb) => Ok(*rgb),
30            Self::Hex(hex) => parse_color(hex),
31        }
32    }
33}
34
35impl From<Rgb> for Color {
36    fn from(value: Rgb) -> Self {
37        Self::Rgb(value)
38    }
39}
40
41impl From<(u8, u8, u8)> for Color {
42    fn from(value: (u8, u8, u8)) -> Self {
43        Self::Rgb(Rgb {
44            r: value.0,
45            g: value.1,
46            b: value.2,
47        })
48    }
49}
50
51impl From<&str> for Color {
52    fn from(value: &str) -> Self {
53        Self::Hex(value.trim().to_string())
54    }
55}
56
57// <FILE>crates/rocketsplash-rt/src/color/cls_color.rs</FILE>
58// <VERS>END OF VERSION: 1.0.0</VERS>