增加各种颜色到HEX的转换,以及色彩理论计算。
This commit is contained in:
parent
d8e7a33071
commit
a2b6432121
@ -1,8 +1,8 @@
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use palette::{
|
use palette::{
|
||||||
color_difference::Wcag21RelativeContrast, Darken, FromColor, Hsl, Lab, Lighten, Mix, Oklch,
|
color_difference::Wcag21RelativeContrast, color_theory::*, convert::FromColorUnclamped, Darken,
|
||||||
ShiftHue, Srgb,
|
FromColor, Hsl, IsWithinBounds, Lab, Lighten, Mix, Oklch, ShiftHue, Srgb,
|
||||||
};
|
};
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
@ -16,6 +16,12 @@ pub fn represent_rgb(color: &str) -> Result<Box<[u8]>, errors::ColorError> {
|
|||||||
Ok(Box::new([srgb.red, srgb.green, srgb.blue]))
|
Ok(Box::new([srgb.red, srgb.green, srgb.blue]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn rgb_to_hex(r: u8, g: u8, b: u8) -> Result<String, errors::ColorError> {
|
||||||
|
let srgb = Srgb::new(r, g, b);
|
||||||
|
Ok(format!("{:x}", srgb))
|
||||||
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn represent_hsl(color: &str) -> Result<Box<[f32]>, errors::ColorError> {
|
pub fn represent_hsl(color: &str) -> Result<Box<[f32]>, errors::ColorError> {
|
||||||
let origin_color = Srgb::from_str(color)
|
let origin_color = Srgb::from_str(color)
|
||||||
@ -29,6 +35,16 @@ pub fn represent_hsl(color: &str) -> Result<Box<[f32]>, errors::ColorError> {
|
|||||||
]))
|
]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn hsl_to_hex(h: f32, s: f32, l: f32) -> Result<String, errors::ColorError> {
|
||||||
|
let hsl = Hsl::new(h, s, l);
|
||||||
|
let srgb = Srgb::from_color_unclamped(hsl);
|
||||||
|
if !srgb.is_within_bounds() {
|
||||||
|
return Err(errors::ColorError::ComponentOutOfBounds);
|
||||||
|
}
|
||||||
|
Ok(format!("{:x}", srgb.into_format::<u8>()))
|
||||||
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn represent_lab(color: &str) -> Result<Box<[f32]>, errors::ColorError> {
|
pub fn represent_lab(color: &str) -> Result<Box<[f32]>, errors::ColorError> {
|
||||||
let origin_color = Srgb::from_str(color)
|
let origin_color = Srgb::from_str(color)
|
||||||
@ -38,6 +54,16 @@ pub fn represent_lab(color: &str) -> Result<Box<[f32]>, errors::ColorError> {
|
|||||||
Ok(Box::new([lab.l, lab.a, lab.b]))
|
Ok(Box::new([lab.l, lab.a, lab.b]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn lab_to_hex(l: f32, a: f32, b: f32) -> Result<String, errors::ColorError> {
|
||||||
|
let lab = Lab::new(l, a, b);
|
||||||
|
let srgb = Srgb::from_color_unclamped(lab);
|
||||||
|
if !srgb.is_within_bounds() {
|
||||||
|
return Err(errors::ColorError::ComponentOutOfBounds);
|
||||||
|
}
|
||||||
|
Ok(format!("{:x}", srgb.into_format::<u8>()))
|
||||||
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn represent_oklch(color: &str) -> Result<Box<[f32]>, errors::ColorError> {
|
pub fn represent_oklch(color: &str) -> Result<Box<[f32]>, errors::ColorError> {
|
||||||
let origin_color = Srgb::from_str(color)
|
let origin_color = Srgb::from_str(color)
|
||||||
@ -51,6 +77,16 @@ pub fn represent_oklch(color: &str) -> Result<Box<[f32]>, errors::ColorError> {
|
|||||||
]))
|
]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn oklch_to_hex(l: f32, c: f32, h: f32) -> Result<String, errors::ColorError> {
|
||||||
|
let lch = Oklch::new(l, c, h);
|
||||||
|
let srgb = Srgb::from_color_unclamped(lch);
|
||||||
|
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)
|
||||||
@ -146,3 +182,91 @@ pub fn wacg_relative_contrast(fg_color: &str, bg_color: &str) -> Result<f32, err
|
|||||||
.into_format::<f32>();
|
.into_format::<f32>();
|
||||||
Ok(fg_srgb.relative_contrast(bg_srgb))
|
Ok(fg_srgb.relative_contrast(bg_srgb))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn analogous_30(color: &str) -> Result<Vec<String>, errors::ColorError> {
|
||||||
|
let origin_color = Srgb::from_str(color)
|
||||||
|
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))?
|
||||||
|
.into_format::<f32>();
|
||||||
|
let oklch = Oklch::from_color(origin_color);
|
||||||
|
let (color_n30, color_p30) = oklch.analogous();
|
||||||
|
let srgb_n30 = Srgb::from_color(color_n30);
|
||||||
|
let srgb_p30 = Srgb::from_color(color_p30);
|
||||||
|
Ok(vec![
|
||||||
|
format!("{:x}", srgb_n30.into_format::<u8>()),
|
||||||
|
format!("{:x}", srgb_p30.into_format::<u8>()),
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn analogous_60(color: &str) -> Result<Vec<String>, errors::ColorError> {
|
||||||
|
let origin_color = Srgb::from_str(color)
|
||||||
|
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))?
|
||||||
|
.into_format::<f32>();
|
||||||
|
let oklch = Oklch::from_color(origin_color);
|
||||||
|
let (color_n60, color_p60) = oklch.analogous_secondary();
|
||||||
|
let srgb_n60 = Srgb::from_color(color_n60);
|
||||||
|
let srgb_p60 = Srgb::from_color(color_p60);
|
||||||
|
Ok(vec![
|
||||||
|
format!("{:x}", srgb_n60.into_format::<u8>()),
|
||||||
|
format!("{:x}", srgb_p60.into_format::<u8>()),
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn complementary(color: &str) -> Result<String, errors::ColorError> {
|
||||||
|
let origin_color = Srgb::from_str(color)
|
||||||
|
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))?
|
||||||
|
.into_format::<f32>();
|
||||||
|
let oklch = Oklch::from_color(origin_color);
|
||||||
|
let complementary_color = oklch.complementary();
|
||||||
|
let srgb = Srgb::from_color(complementary_color);
|
||||||
|
Ok(format!("{:x}", srgb.into_format::<u8>()))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn split_complementary(color: &str) -> Result<Vec<String>, errors::ColorError> {
|
||||||
|
let origin_color = Srgb::from_str(color)
|
||||||
|
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))?
|
||||||
|
.into_format::<f32>();
|
||||||
|
let oklch = Oklch::from_color(origin_color);
|
||||||
|
let (color_p150, color_p210) = oklch.split_complementary();
|
||||||
|
let srgb_p150 = Srgb::from_color(color_p150);
|
||||||
|
let srgb_p210 = Srgb::from_color(color_p210);
|
||||||
|
Ok(vec![
|
||||||
|
format!("{:x}", srgb_p150.into_format::<u8>()),
|
||||||
|
format!("{:x}", srgb_p210.into_format::<u8>()),
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn tetradic(color: &str) -> Result<Vec<String>, errors::ColorError> {
|
||||||
|
let origin_color = Srgb::from_str(color)
|
||||||
|
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))?
|
||||||
|
.into_format::<f32>();
|
||||||
|
let oklch = Oklch::from_color(origin_color);
|
||||||
|
let (color_p90, color_p180, color_p270) = oklch.tetradic();
|
||||||
|
let srgb_p90 = Srgb::from_color(color_p90);
|
||||||
|
let srgb_p180 = Srgb::from_color(color_p180);
|
||||||
|
let srgb_p270 = Srgb::from_color(color_p270);
|
||||||
|
Ok(vec![
|
||||||
|
format!("{:x}", srgb_p90.into_format::<u8>()),
|
||||||
|
format!("{:x}", srgb_p180.into_format::<u8>()),
|
||||||
|
format!("{:x}", srgb_p270.into_format::<u8>()),
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn triadic(color: &str) -> Result<Vec<String>, errors::ColorError> {
|
||||||
|
let origin_color = Srgb::from_str(color)
|
||||||
|
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))?
|
||||||
|
.into_format::<f32>();
|
||||||
|
let oklch = Oklch::from_color(origin_color);
|
||||||
|
let (color_p120, color_p240) = oklch.triadic();
|
||||||
|
let srgb_p120 = Srgb::from_color(color_p120);
|
||||||
|
let srgb_p240 = Srgb::from_color(color_p240);
|
||||||
|
Ok(vec![
|
||||||
|
format!("{:x}", srgb_p120.into_format::<u8>()),
|
||||||
|
format!("{:x}", srgb_p240.into_format::<u8>()),
|
||||||
|
])
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user