From e2806a0cc5277772d8d008badd4206fe056d9bca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Sat, 8 Feb 2025 13:34:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3Cam16Jch=E5=88=B0sRGB?= =?UTF-8?q?=E7=9A=84=E8=BD=AC=E6=8D=A2=E7=AE=97=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- color-module/src/convert/mod.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) 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, - ); } }