use std::{str::FromStr, sync::Arc}; use palette::{ cam16::{Cam16Jch, Parameters}, Darken, FromColor, IntoColor, Lighten, Oklch, Srgb, }; use wasm_bindgen::prelude::wasm_bindgen; use crate::errors; #[wasm_bindgen] pub fn series( color: &str, expand_amount: i16, step: f32, ) -> Result, errors::ColorError> { let origin_color = Srgb::from_str(color) .map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))? .into_format::(); let oklch = Arc::new(Oklch::from_color(origin_color.clone())); let mut color_series = Vec::new(); for s in (1..=expand_amount).rev() { let darkened_color = Arc::clone(&oklch).darken(s as f32 * step); let srgb = Srgb::from_color(darkened_color); color_series.push(format!("{:x}", srgb.into_format::())); } color_series.push(format!("{:x}", origin_color.into_format::())); for s in 1..=expand_amount { let lightened_color = Arc::clone(&oklch).lighten(s as f32 * step); let srgb = Srgb::from_color(lightened_color); color_series.push(format!("{:x}", srgb.into_format::())); } Ok(color_series) } #[wasm_bindgen] pub fn tonal_lighten_series( color: &str, expand_amount: i16, step: f32, ) -> Result, errors::ColorError> { let origin_color = Srgb::from_str(color) .map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))? .into_format::(); let hct = Cam16Jch::from_xyz( origin_color.into_color(), Parameters::default_static_wp(40.0), ); let mut color_series = Vec::new(); let mut lightness = hct.lightness; for _ in 1..=expand_amount { lightness += (100.0 - lightness) * step; let lightened_color = Cam16Jch::new(lightness, hct.chroma, hct.hue); let srgb = Srgb::from_color(lightened_color.into_xyz(Parameters::default_static_wp(40.0))); color_series.push(format!("{:x}", srgb.into_format::())); } Ok(color_series) } #[wasm_bindgen] pub fn tonal_darken_series( color: &str, expand_amount: i16, step: f32, ) -> Result, errors::ColorError> { let origin_color = Srgb::from_str(color) .map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))? .into_format::(); let hct = Cam16Jch::from_xyz( origin_color.into_color(), Parameters::default_static_wp(40.0), ); let mut color_series = Vec::new(); let mut lightness = hct.lightness; for _ in 1..=expand_amount { lightness *= 1.0 - step; let darkened_color = Cam16Jch::new(lightness, hct.chroma, hct.hue); let srgb = Srgb::from_color(darkened_color.into_xyz(Parameters::default_static_wp(40.0))); color_series.push(format!("{:x}", srgb.into_format::())); } Ok(color_series.into_iter().rev().collect()) }