基本完成WASM模块内容的编写。

This commit is contained in:
徐涛 2024-12-26 14:56:27 +08:00
parent c5f0bb32aa
commit e9a12b34a5
5 changed files with 162 additions and 3 deletions

View File

@ -3,8 +3,16 @@ name = "color-module"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies] [dependencies]
color-name = "1.1.0" color-name = "1.1.0"
palette = { version = "0.7.6", features = ["serde"] } palette = { version = "0.7.6", features = ["serde"] }
serde = { version = "1.0.216", features = ["derive"] } serde = { version = "1.0.216", features = ["derive"] }
serde_json = "1.0.134" serde_json = "1.0.134"
thiserror = "2.0.9"
wasm-bindgen = { version = "0.2.99", features = ["serde", "serde_json", "serde-serialize"] }
[dev-dependencies]
wasm-bindgen-test = "0.3.49"

View File

@ -0,0 +1,14 @@
use thiserror::Error;
use wasm_bindgen::JsValue;
#[derive(Debug, Error)]
pub enum ColorError {
#[error("Invalid RGB value: {0}")]
UnrecogniazedRGB(String),
}
impl Into<JsValue> for ColorError {
fn into(self) -> JsValue {
JsValue::from_str(&self.to_string())
}
}

140
color-module/src/lib.rs Normal file
View File

@ -0,0 +1,140 @@
use std::str::FromStr;
use palette::{
color_difference::Wcag21RelativeContrast, Darken, FromColor, Hsl, Lab, Lighten, Mix, Oklch,
ShiftHue, Srgb,
};
use wasm_bindgen::prelude::*;
mod errors;
#[wasm_bindgen]
pub fn represent_hsl(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 hsl = Hsl::from_color(origin_color);
Ok(Box::new([
hsl.hue.into_positive_degrees(),
hsl.saturation,
hsl.lightness,
]))
}
#[wasm_bindgen]
pub fn represent_lab(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 lab = Lab::from_color(origin_color);
Ok(Box::new([lab.l, lab.a, lab.b]))
}
#[wasm_bindgen]
pub fn represent_oklch(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 lch = Oklch::from_color(origin_color);
Ok(Box::new([
lch.l,
lch.chroma,
lch.hue.into_positive_degrees(),
]))
}
#[wasm_bindgen]
pub fn shift_hue(color: &str, degree: f32) -> Result<String, errors::ColorError> {
let origin_color = Srgb::from_str(color)
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))?
.into_format::<f32>();
let hsl = Hsl::from_color(origin_color);
let shifted_color = hsl.shift_hue(degree);
let srgb = Srgb::from_color(shifted_color);
Ok(format!("{:x}", srgb.into_format::<u8>()))
}
#[wasm_bindgen]
pub fn lighten(color: &str, percent: f32) -> Result<String, errors::ColorError> {
let origin_color = Srgb::from_str(color)
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))?
.into_format::<f32>();
let hsl = Hsl::from_color(origin_color);
let lightened_color = hsl.lighten(percent);
let srgb = Srgb::from_color(lightened_color);
Ok(format!("{:x}", srgb.into_format::<u8>()))
}
#[wasm_bindgen]
pub fn lighten_absolute(color: &str, value: f32) -> Result<String, errors::ColorError> {
let origin_color = Srgb::from_str(color)
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))?
.into_format::<f32>();
let hsl = Hsl::from_color(origin_color);
let lightened_color = hsl.lighten_fixed(value);
let srgb = Srgb::from_color(lightened_color);
Ok(format!("{:x}", srgb.into_format::<u8>()))
}
#[wasm_bindgen]
pub fn darken(color: &str, percent: f32) -> Result<String, errors::ColorError> {
let origin_color = Srgb::from_str(color)
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))?
.into_format::<f32>();
let hsl = Hsl::from_color(origin_color);
let darkened_color = hsl.darken(percent);
let srgb = Srgb::from_color(darkened_color);
Ok(format!("{:x}", srgb.into_format::<u8>()))
}
#[wasm_bindgen]
pub fn darken_absolute(color: &str, value: f32) -> Result<String, errors::ColorError> {
let origin_color = Srgb::from_str(color)
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))?
.into_format::<f32>();
let hsl = Hsl::from_color(origin_color);
let darkened_color = hsl.darken_fixed(value);
let srgb = Srgb::from_color(darkened_color);
Ok(format!("{:x}", srgb.into_format::<u8>()))
}
#[wasm_bindgen]
pub fn mix(color1: &str, color2: &str, percent: f32) -> Result<String, errors::ColorError> {
let srgb1 = Srgb::from_str(color1)
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color1.to_string()))?
.into_format::<f32>();
let srgb2 = Srgb::from_str(color2)
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color2.to_string()))?
.into_format::<f32>();
let mixed_color = srgb1.mix(srgb2, percent);
Ok(format!("{:x}", mixed_color.into_format::<u8>()))
}
#[wasm_bindgen]
pub fn tint(color: &str, percent: f32) -> Result<String, errors::ColorError> {
let origin_color = Srgb::from_str(color)
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))?
.into_format::<f32>();
let tinted_color = origin_color.mix(Srgb::new(1.0, 1.0, 1.0), percent);
Ok(format!("{:x}", tinted_color.into_format::<u8>()))
}
#[wasm_bindgen]
pub fn shade(color: &str, percent: f32) -> Result<String, errors::ColorError> {
let origin_color = Srgb::from_str(color)
.map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))?
.into_format::<f32>();
let shaded_color = origin_color.mix(Srgb::new(0.0, 0.0, 0.0), percent);
Ok(format!("{:x}", shaded_color.into_format::<u8>()))
}
#[wasm_bindgen]
pub fn wacg_relative_contrast(fg_color: &str, bg_color: &str) -> Result<f32, errors::ColorError> {
let fg_srgb = Srgb::from_str(fg_color)
.map_err(|_| errors::ColorError::UnrecogniazedRGB(fg_color.to_string()))?
.into_format::<f32>();
let bg_srgb = Srgb::from_str(bg_color)
.map_err(|_| errors::ColorError::UnrecogniazedRGB(bg_color.to_string()))?
.into_format::<f32>();
Ok(fg_srgb.relative_contrast(bg_srgb))
}

View File

@ -1,3 +0,0 @@
fn main() {
println!("Hello, world!");
}

View File