From bf31aa67cdb6883af06ade16acf35a25873ad3ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Fri, 3 Jan 2025 08:42:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=A9=E7=94=A8HCT?= =?UTF-8?q?=E9=A2=9C=E8=89=B2=E7=A9=BA=E9=97=B4=E8=BF=9B=E8=A1=8C=E4=BA=AE?= =?UTF-8?q?=E5=BA=A6=E5=8F=98=E6=8D=A2=E7=9A=84=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- color-module/src/lib.rs | 60 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/color-module/src/lib.rs b/color-module/src/lib.rs index 6f8c944..429f3c7 100644 --- a/color-module/src/lib.rs +++ b/color-module/src/lib.rs @@ -95,8 +95,10 @@ pub fn represent_hct(color: &str) -> Result, errors::ColorError> { let origin_color = Srgb::from_str(color) .map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))? .into_format::(); - 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, errors::ColorError> { #[wasm_bindgen] pub fn hct_to_hex(hue: f32, chroma: f32, tone: f32) -> Result { - 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, errors::ColorError> { + let origin_color = Srgb::from_str(color) + .map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))? + .into_format::(); + 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::())); + } + + Ok(color_series.into_iter().rev().collect()) +} + +pub fn tonal_darken_series( + color: &str, + expand_amount: i16, + step: f32, +) -> Result, errors::ColorError> { + let origin_color = Srgb::from_str(color) + .map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))? + .into_format::(); + 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::())); + } + + Ok(color_series) +}