rocketsplash_rt/color/
cls_color.rs1use 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