From 5caf2ec90f02b23bf6775339ba3e2dffc86a9308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Fri, 3 Jan 2025 06:45:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=90=91HCT=E9=A2=9C?= =?UTF-8?q?=E8=89=B2=E7=A9=BA=E9=97=B4=E7=9A=84=E5=8F=8C=E5=90=91=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- color-module/src/lib.rs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/color-module/src/lib.rs b/color-module/src/lib.rs index f619430..6f8c944 100644 --- a/color-module/src/lib.rs +++ b/color-module/src/lib.rs @@ -1,8 +1,11 @@ use std::str::FromStr; use palette::{ - color_difference::Wcag21RelativeContrast, color_theory::*, convert::FromColorUnclamped, Darken, - FromColor, Hsl, IsWithinBounds, Lab, Lighten, Mix, Oklch, ShiftHue, Srgb, + cam16::{Cam16Jch, Parameters}, + color_difference::Wcag21RelativeContrast, + color_theory::*, + convert::FromColorUnclamped, + Darken, FromColor, Hsl, IntoColor, IsWithinBounds, Lab, Lighten, Mix, Oklch, ShiftHue, Srgb, }; use wasm_bindgen::prelude::*; @@ -87,6 +90,31 @@ pub fn oklch_to_hex(l: f32, c: f32, h: f32) -> Result())) } +#[wasm_bindgen] +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); + Ok(Box::new([ + hct.hue.into_positive_degrees(), + hct.chroma, + hct.lightness, + ])) +} + +#[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)); + if !srgb.is_within_bounds() { + return Err(errors::ColorError::ComponentOutOfBounds); + } + Ok(format!("{:x}", srgb.into_format::())) +} + #[wasm_bindgen] pub fn shift_hue(color: &str, degree: f32) -> Result { let origin_color = Srgb::from_str(color)