mod types;
mod xml;
pub use types::{HslColor, PresetColor, SystemColor, ThemeColor};
use crate::enums::dml::{MsoColorType, MsoThemeColorIndex, PresetColorVal, SystemColorVal};
use crate::text::font::RgbColor;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub enum ColorFormat {
Rgb(RgbColor),
Theme(ThemeColor),
Hsl(HslColor),
System(SystemColor),
Preset(PresetColor),
}
impl ColorFormat {
#[must_use]
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
Self::Rgb(RgbColor::new(r, g, b))
}
#[must_use]
pub const fn theme(theme_color: MsoThemeColorIndex) -> Self {
Self::Theme(ThemeColor {
theme_color,
brightness: None,
})
}
#[must_use]
pub const fn theme_with_brightness(theme_color: MsoThemeColorIndex, brightness: f64) -> Self {
Self::Theme(ThemeColor {
theme_color,
brightness: Some(brightness),
})
}
#[must_use]
pub const fn hsl(hue: f64, saturation: f64, luminance: f64) -> Self {
Self::Hsl(HslColor {
hue,
saturation,
luminance,
})
}
#[must_use]
pub fn system(val: &str) -> Self {
Self::System(SystemColor {
val: SystemColorVal::from_xml_str(val),
last_color: None,
})
}
#[must_use]
pub fn system_with_last_color(val: &str, last_color: &str) -> Self {
Self::System(SystemColor {
val: SystemColorVal::from_xml_str(val),
last_color: Some(last_color.to_string()),
})
}
#[must_use]
pub const fn color_type(&self) -> MsoColorType {
match self {
Self::Rgb(_) => MsoColorType::Rgb,
Self::Theme(_) => MsoColorType::Scheme,
Self::Hsl(_) => MsoColorType::Hsl,
Self::System(_) => MsoColorType::System,
Self::Preset(_) => MsoColorType::Preset,
}
}
#[must_use]
pub fn preset(val: &str) -> Self {
Self::Preset(PresetColor {
val: PresetColorVal::from_xml_str(val),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_rgb_convenience() {
let c = ColorFormat::rgb(0, 128, 255);
match &c {
ColorFormat::Rgb(rgb) => {
assert_eq!(rgb.r, 0);
assert_eq!(rgb.g, 128);
assert_eq!(rgb.b, 255);
}
_ => panic!("expected Rgb variant"), }
}
}