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) +}