WASM包中增加自动生成色板的功能。
This commit is contained in:
@@ -15,6 +15,7 @@ use wasm_bindgen::prelude::*;
|
||||
mod color_card;
|
||||
mod color_differ;
|
||||
mod errors;
|
||||
mod palettes;
|
||||
mod reversing;
|
||||
|
||||
#[wasm_bindgen]
|
||||
|
94
color-module/src/palettes/auto_palette.rs
Normal file
94
color-module/src/palettes/auto_palette.rs
Normal file
@@ -0,0 +1,94 @@
|
||||
use palette::{FromColor, Oklch, Srgb};
|
||||
use std::str::FromStr;
|
||||
use wasm_bindgen::prelude::wasm_bindgen;
|
||||
|
||||
use crate::errors;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn generate_palette_from_color(
|
||||
reference_color: &str,
|
||||
swatch_amount: i16,
|
||||
minimum_lightness: f32,
|
||||
maximum_lightness: f32,
|
||||
use_reference_color: Option<bool>,
|
||||
reference_color_bias: Option<i16>,
|
||||
) -> Result<Vec<String>, errors::ColorError> {
|
||||
let reference_color_bias = reference_color_bias.unwrap_or(0);
|
||||
let original_color = Oklch::from_color(
|
||||
Srgb::from_str(reference_color)
|
||||
.map_err(|_| errors::ColorError::UnrecogniazedRGB(reference_color.to_string()))?
|
||||
.into_format::<f32>(),
|
||||
);
|
||||
match use_reference_color {
|
||||
Some(true) => Ok(generate_incontinuous_palette(
|
||||
&original_color,
|
||||
swatch_amount,
|
||||
minimum_lightness,
|
||||
maximum_lightness,
|
||||
reference_color_bias,
|
||||
)),
|
||||
Some(false) | None => Ok(generate_continuous_palette(
|
||||
&original_color,
|
||||
swatch_amount,
|
||||
minimum_lightness,
|
||||
maximum_lightness,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_continuous_palette(
|
||||
original_color: &Oklch,
|
||||
swatch_amount: i16,
|
||||
minimum_lightness: f32,
|
||||
maximum_lightness: f32,
|
||||
) -> Vec<String> {
|
||||
let mut palette = Vec::new();
|
||||
|
||||
let step = (maximum_lightness - minimum_lightness) / (swatch_amount - 1) as f32;
|
||||
for i in 0..swatch_amount {
|
||||
let lightness = minimum_lightness + step * i as f32;
|
||||
let color = Oklch {
|
||||
l: lightness,
|
||||
..*original_color
|
||||
};
|
||||
palette.push(format!("{:x}", Srgb::from_color(color).into_format::<u8>()));
|
||||
}
|
||||
|
||||
palette
|
||||
}
|
||||
|
||||
fn generate_incontinuous_palette(
|
||||
original_color: &Oklch,
|
||||
swatch_amount: i16,
|
||||
minimum_lightness: f32,
|
||||
maximum_lightness: f32,
|
||||
original_place: i16,
|
||||
) -> Vec<String> {
|
||||
let mut palette = Vec::new();
|
||||
|
||||
let midpoint = swatch_amount / 2;
|
||||
let dark_side_amount = midpoint + original_place;
|
||||
|
||||
let step = (original_color.l - minimum_lightness) / dark_side_amount as f32;
|
||||
for i in 0..dark_side_amount {
|
||||
let lightness = minimum_lightness + step * i as f32;
|
||||
let color = Oklch {
|
||||
l: lightness,
|
||||
..*original_color
|
||||
};
|
||||
palette.push(format!("{:x}", Srgb::from_color(color).into_format::<u8>()));
|
||||
}
|
||||
|
||||
let light_side_amount = swatch_amount - dark_side_amount;
|
||||
let step = (maximum_lightness - original_color.l) / (light_side_amount - 1) as f32;
|
||||
for i in 0..light_side_amount {
|
||||
let lightness = original_color.l + step * i as f32;
|
||||
let color = Oklch {
|
||||
l: lightness,
|
||||
..*original_color
|
||||
};
|
||||
palette.push(format!("{:x}", Srgb::from_color(color).into_format::<u8>()));
|
||||
}
|
||||
|
||||
palette
|
||||
}
|
1
color-module/src/palettes/mod.rs
Normal file
1
color-module/src/palettes/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod auto_palette;
|
Reference in New Issue
Block a user