增加向HCT颜色空间的双向转换。
This commit is contained in:
parent
d0f9b2af14
commit
5caf2ec90f
|
@ -1,8 +1,11 @@
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use palette::{
|
use palette::{
|
||||||
color_difference::Wcag21RelativeContrast, color_theory::*, convert::FromColorUnclamped, Darken,
|
cam16::{Cam16Jch, Parameters},
|
||||||
FromColor, Hsl, IsWithinBounds, Lab, Lighten, Mix, Oklch, ShiftHue, Srgb,
|
color_difference::Wcag21RelativeContrast,
|
||||||
|
color_theory::*,
|
||||||
|
convert::FromColorUnclamped,
|
||||||
|
Darken, FromColor, Hsl, IntoColor, IsWithinBounds, Lab, Lighten, Mix, Oklch, ShiftHue, Srgb,
|
||||||
};
|
};
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
@ -87,6 +90,31 @@ pub fn oklch_to_hex(l: f32, c: f32, h: f32) -> Result<String, errors::ColorError
|
||||||
Ok(format!("{:x}", srgb.into_format::<u8>()))
|
Ok(format!("{:x}", srgb.into_format::<u8>()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn represent_hct(color: &str) -> Result<Box<[f32]>, errors::ColorError> {
|
||||||
|
let origin_color = Srgb::from_str(color)
|
||||||
|
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))?
|
||||||
|
.into_format::<f32>();
|
||||||
|
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<String, errors::ColorError> {
|
||||||
|
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::<u8>()))
|
||||||
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn shift_hue(color: &str, degree: f32) -> Result<String, errors::ColorError> {
|
pub fn shift_hue(color: &str, degree: f32) -> Result<String, errors::ColorError> {
|
||||||
let origin_color = Srgb::from_str(color)
|
let origin_color = Srgb::from_str(color)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user