增加利用HCT颜色空间进行亮度变换的功能。

This commit is contained in:
徐涛 2025-01-03 08:42:17 +08:00
parent 5caf2ec90f
commit bf31aa67cd

View File

@ -95,8 +95,10 @@ pub fn represent_hct(color: &str) -> Result<Box<[f32]>, errors::ColorError> {
let origin_color = Srgb::from_str(color)
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))?
.into_format::<f32>();
let extra_parameters = Parameters::default_static_wp(40.0);
let hct = Cam16Jch::from_xyz(origin_color.into_color(), extra_parameters);
let hct = Cam16Jch::from_xyz(
origin_color.into_color(),
Parameters::default_static_wp(40.0),
);
Ok(Box::new([
hct.hue.into_positive_degrees(),
hct.chroma,
@ -106,9 +108,8 @@ pub fn represent_hct(color: &str) -> Result<Box<[f32]>, errors::ColorError> {
#[wasm_bindgen]
pub fn hct_to_hex(hue: f32, chroma: f32, tone: f32) -> Result<String, errors::ColorError> {
let extra_parameters = Parameters::default_static_wp(40.0);
let hct = Cam16Jch::new(tone, chroma, hue);
let srgb = Srgb::from_color_unclamped(hct.into_xyz(extra_parameters));
let srgb = Srgb::from_color_unclamped(hct.into_xyz(Parameters::default_static_wp(40.0)));
if !srgb.is_within_bounds() {
return Err(errors::ColorError::ComponentOutOfBounds);
}
@ -324,3 +325,54 @@ pub fn series(
}
Ok(color_series)
}
#[wasm_bindgen]
pub fn tonal_lighten_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 hct = Cam16Jch::from_xyz(
origin_color.into_color(),
Parameters::default_static_wp(40.0),
);
let mut color_series = Vec::new();
let mut lightness = hct.lightness;
for _ in 1..=expand_amount {
lightness += (1.0 - lightness) * step;
let lightened_color = Cam16Jch::new(lightness, hct.chroma, hct.hue);
let srgb = Srgb::from_color(lightened_color.into_xyz(Parameters::default_static_wp(40.0)));
color_series.push(format!("{:x}", srgb.into_format::<u8>()));
}
Ok(color_series.into_iter().rev().collect())
}
pub fn tonal_darken_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 hct = Cam16Jch::from_xyz(
origin_color.into_color(),
Parameters::default_static_wp(40.0),
);
let mut color_series = Vec::new();
let mut lightness = hct.lightness;
for _ in 1..=expand_amount {
lightness *= step;
let darkened_color = Cam16Jch::new(lightness, hct.chroma, hct.hue);
let srgb = Srgb::from_color(darkened_color.into_xyz(Parameters::default_static_wp(40.0)));
color_series.push(format!("{:x}", srgb.into_format::<u8>()));
}
Ok(color_series)
}