将M3 Scheme的颜色生成从Cam16Jch重构为Lch。

This commit is contained in:
徐涛
2025-02-08 16:38:51 +08:00
parent c60aefaaff
commit 0369f238f2
10 changed files with 296 additions and 285 deletions

View File

@@ -2,9 +2,10 @@ use palette::{
cam16::{Cam16Jch, Parameters},
convert::FromColorUnclamped,
luma::Luma,
Hsl, IntoColor, IsWithinBounds, Lchuv, Oklab, Oklch, Srgb,
Hsl, IntoColor, IsWithinBounds, Lch, Lchuv, Oklab, Oklch, Srgb,
};
#[allow(dead_code)]
pub fn map_cam16jch_to_srgb(origin: &Cam16Jch<f32>) -> Srgb {
let original_xyz = origin.into_xyz(Parameters::default_static_wp(40.0));
let mut new_srgb = Srgb::from_color_unclamped(original_xyz);
@@ -28,10 +29,36 @@ pub fn map_cam16jch_to_srgb(origin: &Cam16Jch<f32>) -> Srgb {
}
}
#[allow(dead_code)]
pub fn map_cam16jch_to_srgb_hex(origin: &Cam16Jch<f32>) -> String {
format!("{:x}", map_cam16jch_to_srgb(origin).into_format::<u8>())
}
pub fn map_lch_to_srgb(origin: &Lch) -> Srgb {
let mut new_srgb: Srgb = (*origin).into_color();
if new_srgb.is_within_bounds() {
return new_srgb;
}
let mut c: f32 = origin.chroma;
let original_c = c;
let h = origin.hue;
let l = origin.l;
loop {
let new_lch = Lch::new(l, c, h);
new_srgb = new_lch.into_color();
c -= original_c / 1000.0;
if c > 0.0 && (new_srgb.is_within_bounds()) {
break new_srgb;
}
}
}
pub fn map_lch_to_srgb_hex(origin: &Lch) -> String {
format!("{:x}", map_lch_to_srgb(origin).into_format::<u8>())
}
pub fn map_hsl_to_srgb(origin: &Hsl) -> Srgb {
let mut new_original = Hsl::new(origin.hue, origin.saturation, origin.lightness);
const FACTOR: f32 = 0.99;