增加在RGB颜色空间逆向计算颜色混合比例的功能。
This commit is contained in:
parent
1edc74daaf
commit
7c91f50173
|
@ -15,6 +15,7 @@ use wasm_bindgen::prelude::*;
|
||||||
mod color_card;
|
mod color_card;
|
||||||
mod color_differ;
|
mod color_differ;
|
||||||
mod errors;
|
mod errors;
|
||||||
|
mod reversing;
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn represent_rgb(color: &str) -> Result<Box<[u8]>, errors::ColorError> {
|
pub fn represent_rgb(color: &str) -> Result<Box<[u8]>, errors::ColorError> {
|
||||||
|
@ -496,3 +497,37 @@ pub fn differ_in_oklch(
|
||||||
);
|
);
|
||||||
Ok(oklch.difference(&other_oklch))
|
Ok(oklch.difference(&other_oklch))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn tint_scale(
|
||||||
|
basic_color: &str,
|
||||||
|
mixed_color: &str,
|
||||||
|
) -> Result<reversing::MixReversing, errors::ColorError> {
|
||||||
|
let basic_color = Srgb::from_str(basic_color)
|
||||||
|
.map_err(|_| errors::ColorError::UnrecogniazedRGB(basic_color.to_string()))?
|
||||||
|
.into_format::<f32>();
|
||||||
|
let mixed_color = Srgb::from_str(mixed_color)
|
||||||
|
.map_err(|_| errors::ColorError::UnrecogniazedRGB(mixed_color.to_string()))?
|
||||||
|
.into_format::<f32>();
|
||||||
|
Ok(reversing::MixReversing::from_tint_rgb(
|
||||||
|
basic_color,
|
||||||
|
mixed_color,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn shade_scale(
|
||||||
|
basic_color: &str,
|
||||||
|
mixed_color: &str,
|
||||||
|
) -> Result<reversing::MixReversing, errors::ColorError> {
|
||||||
|
let basic_color = Srgb::from_str(basic_color)
|
||||||
|
.map_err(|_| errors::ColorError::UnrecogniazedRGB(basic_color.to_string()))?
|
||||||
|
.into_format::<f32>();
|
||||||
|
let mixed_color = Srgb::from_str(mixed_color)
|
||||||
|
.map_err(|_| errors::ColorError::UnrecogniazedRGB(mixed_color.to_string()))?
|
||||||
|
.into_format::<f32>();
|
||||||
|
Ok(reversing::MixReversing::from_shade_rgb(
|
||||||
|
basic_color,
|
||||||
|
mixed_color,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
42
color-module/src/reversing.rs
Normal file
42
color-module/src/reversing.rs
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
use palette::rgb::Rgb;
|
||||||
|
use serde::Serialize;
|
||||||
|
use wasm_bindgen::prelude::wasm_bindgen;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Serialize)]
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub struct MixReversing {
|
||||||
|
pub r_factor: f32,
|
||||||
|
pub g_factor: f32,
|
||||||
|
pub b_factor: f32,
|
||||||
|
pub average: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MixReversing {
|
||||||
|
pub fn from_tint_rgb(basic_color: Rgb, mixed_result: Rgb) -> Self {
|
||||||
|
let r_factor = (mixed_result.red - basic_color.red) / (1.0 - basic_color.red);
|
||||||
|
let g_factor = (mixed_result.green - basic_color.green) / (1.0 - basic_color.green);
|
||||||
|
let b_factor = (mixed_result.blue - basic_color.blue) / (1.0 - basic_color.blue);
|
||||||
|
let average = (r_factor + g_factor + b_factor) / 3.0;
|
||||||
|
|
||||||
|
MixReversing {
|
||||||
|
r_factor,
|
||||||
|
g_factor,
|
||||||
|
b_factor,
|
||||||
|
average,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_shade_rgb(basic_color: Rgb, mixed_result: Rgb) -> Self {
|
||||||
|
let r_factor = (basic_color.red - mixed_result.red) / (0.0 - basic_color.red);
|
||||||
|
let g_factor = (basic_color.green - mixed_result.green) / (0.0 - basic_color.green);
|
||||||
|
let b_factor = (basic_color.blue - mixed_result.blue) / (0.0 - basic_color.blue);
|
||||||
|
let average = (r_factor + g_factor + b_factor) / 3.0;
|
||||||
|
|
||||||
|
MixReversing {
|
||||||
|
r_factor,
|
||||||
|
g_factor,
|
||||||
|
b_factor,
|
||||||
|
average,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user