增加使用亮度生成系列颜色的功能。

This commit is contained in:
徐涛 2025-01-03 05:52:02 +08:00
parent 118b6605d7
commit 025d92e552

View File

@ -270,3 +270,29 @@ pub fn triadic(color: &str) -> Result<Vec<String>, errors::ColorError> {
format!("{:x}", srgb_p240.into_format::<u8>()),
])
}
#[wasm_bindgen]
pub fn series(
color: &str,
expand_amount: i16,
step: f32,
) -> Result<Vec<String>, errors::ColorError> {
let origin_color = Srgb::from_str(color)
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))?
.into_format::<f32>();
let oklch = Oklch::from_color(origin_color.clone());
let mut color_series = Vec::new();
for s in expand_amount..1 {
let darkened_color = oklch.darken(s as f32 * step);
let srgb = Srgb::from_color(darkened_color);
color_series.push(format!("{:x}", srgb.into_format::<u8>()));
}
color_series.push(format!("{:x}", origin_color.into_format::<u8>()));
for s in 1..expand_amount {
let lightened_color = oklch.lighten(s as f32 * step);
let srgb = Srgb::from_color(lightened_color);
color_series.push(format!("{:x}", srgb.into_format::<u8>()));
}
Ok(color_series)
}