1use std::str::FromStr;
2
3use css_color::Srgb;
4use thiserror::Error;
5
6#[derive(Clone)]
7pub struct Color {
8 pub r: u8,
9 pub g: u8,
10 pub b: u8,
11}
12
13#[derive(Debug, Error)]
15#[error("Failed to find color by name \"{0}\"")]
16pub struct ParseColorError(String);
17
18impl FromStr for Color {
19 type Err = ParseColorError;
20
21 fn from_str(s: &str) -> Result<Color, Self::Err> {
22 let srgb = s.parse::<Srgb>().map_err(|_| ParseColorError(s.to_owned()))?;
23
24 Ok(Color {
25 r: (srgb.red * srgb.alpha * 255.) as u8,
26 g: (srgb.green * srgb.alpha * 255.) as u8,
27 b: (srgb.blue * srgb.alpha * 255.) as u8,
28 })
29 }
30}