diff --git a/color-module/src/convert/mod.rs b/color-module/src/convert/mod.rs index 5eb9cee..e365909 100644 --- a/color-module/src/convert/mod.rs +++ b/color-module/src/convert/mod.rs @@ -2,23 +2,29 @@ use palette::{ cam16::{Cam16Jch, Parameters}, convert::FromColorUnclamped, luma::Luma, - Hsl, IntoColor, IsWithinBounds, Oklab, Oklch, Srgb, + Hsl, IntoColor, IsWithinBounds, Lchuv, Oklab, Oklch, Srgb, }; pub fn map_cam16jch_to_srgb(origin: &Cam16Jch) -> Srgb { - let mut new_original = Cam16Jch::new(origin.lightness, origin.chroma, origin.hue); - const FACTOR: f32 = 0.99; + let original_xyz = origin.into_xyz(Parameters::default_static_wp(40.0)); + let mut new_srgb = Srgb::from_color_unclamped(original_xyz); + if new_srgb.is_within_bounds() { + return new_srgb; + } + + let lchuv: Lchuv = Lchuv::from_color_unclamped(original_xyz); + let mut c: f32 = lchuv.chroma; + let original_c = c; + let h = lchuv.hue; + let l = lchuv.l; + loop { - let new_srgb = - Srgb::from_color_unclamped(new_original.into_xyz(Parameters::default_static_wp(40.0))); - if new_srgb.is_within_bounds() { + let new_lchuv = Lchuv::new(l, c, h); + new_srgb = new_lchuv.into_color(); + c -= original_c / 1000.0; + if c > 0.0 && (new_srgb.is_within_bounds()) { break new_srgb; } - new_original = Cam16Jch::new( - new_original.lightness, - new_original.chroma * FACTOR, - new_original.hue, - ); } }