rocketsplash-rt 0.2.2

Runtime library for loading and rendering Rocketsplash assets (.rst, .rsf)
Documentation
// <FILE>crates/rocketsplash-rt/src/color/cls_color.rs</FILE>
// <DESC>Runtime color input representation</DESC>
// <VERS>VERSION: 1.0.0</VERS>
// <WCTX>Runtime library implementation</WCTX>
// <CLOG>Add Color type with parsing helpers</CLOG>

use rocketsplash_formats::Rgb;

use crate::{parse_color, Error};

#[derive(Clone, Debug)]
pub enum Color {
    Rgb(Rgb),
    Hex(String),
}

impl Color {
    pub fn hex(s: &str) -> Result<Self, Error> {
        let rgb = parse_color(s)?;
        Ok(Self::Rgb(rgb))
    }

    pub fn rgb(r: u8, g: u8, b: u8) -> Self {
        Self::Rgb(Rgb { r, g, b })
    }

    pub fn to_rgb(&self) -> Result<Rgb, Error> {
        match self {
            Self::Rgb(rgb) => Ok(*rgb),
            Self::Hex(hex) => parse_color(hex),
        }
    }
}

impl From<Rgb> for Color {
    fn from(value: Rgb) -> Self {
        Self::Rgb(value)
    }
}

impl From<(u8, u8, u8)> for Color {
    fn from(value: (u8, u8, u8)) -> Self {
        Self::Rgb(Rgb {
            r: value.0,
            g: value.1,
            b: value.2,
        })
    }
}

impl From<&str> for Color {
    fn from(value: &str) -> Self {
        Self::Hex(value.trim().to_string())
    }
}

// <FILE>crates/rocketsplash-rt/src/color/cls_color.rs</FILE>
// <VERS>END OF VERSION: 1.0.0</VERS>