增加一个将RGB颜色拆解成分量色单元的函数。

This commit is contained in:
徐涛 2024-12-27 09:16:50 +08:00
parent 7407133440
commit 09c5a3be78

View File

@ -8,6 +8,14 @@ use wasm_bindgen::prelude::*;
mod errors; mod errors;
#[wasm_bindgen]
pub fn represent_rgb(color: &str) -> Result<Box<[u8]>, errors::ColorError> {
let srgb = Srgb::from_str(color)
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))?
.into_format::<u8>();
Ok(Box::new([srgb.red, srgb.green, srgb.blue]))
}
#[wasm_bindgen] #[wasm_bindgen]
pub fn represent_hsl(color: &str) -> Result<Box<[f32]>, errors::ColorError> { pub fn represent_hsl(color: &str) -> Result<Box<[f32]>, errors::ColorError> {
let origin_color = Srgb::from_str(color) let origin_color = Srgb::from_str(color)