use std::str::FromStr; use palette::{ cam16::{Cam16Jch, Parameters}, FromColor, Hsl, IntoColor, Oklch, Srgb, }; use wasm_bindgen::prelude::wasm_bindgen; use crate::{ color_differ::{self, ColorDifference}, errors, reversing, }; #[wasm_bindgen] pub fn differ_in_rgb( color: &str, other: &str, ) -> Result { let srgb = Srgb::from_str(color) .map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))? .into_format::(); let other_srgb = Srgb::from_str(other) .map_err(|_| errors::ColorError::UnrecogniazedRGB(other.to_string()))? .into_format::(); Ok(srgb.difference(&other_srgb)) } #[wasm_bindgen] pub fn relative_differ_in_rgb( color: &str, other: &str, ) -> Result { let srgb = Srgb::from_str(color) .map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))? .into_format::(); let other_srgb = Srgb::from_str(other) .map_err(|_| errors::ColorError::UnrecogniazedRGB(other.to_string()))? .into_format::(); Ok(srgb.difference_relative(&other_srgb)) } #[wasm_bindgen] pub fn differ_in_hsl( color: &str, other: &str, ) -> Result { let hsl = Hsl::from_color( Srgb::from_str(color) .map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))? .into_format::(), ); let other_hsl = Hsl::from_color( Srgb::from_str(other) .map_err(|_| errors::ColorError::UnrecogniazedRGB(other.to_string()))? .into_format::(), ); Ok(hsl.difference(&other_hsl)) } #[wasm_bindgen] pub fn relative_differ_in_hsl( color: &str, other: &str, ) -> Result { let hsl = Hsl::from_color( Srgb::from_str(color) .map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))? .into_format::(), ); let other_hsl = Hsl::from_color( Srgb::from_str(other) .map_err(|_| errors::ColorError::UnrecogniazedRGB(other.to_string()))? .into_format::(), ); Ok(hsl.difference_relative(&other_hsl)) } #[wasm_bindgen] pub fn differ_in_hct( color: &str, other: &str, ) -> Result { let hct = Cam16Jch::from_xyz( Srgb::from_str(color) .map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))? .into_format::() .into_color(), Parameters::default_static_wp(40.0), ); let other_hct = Cam16Jch::from_xyz( Srgb::from_str(other) .map_err(|_| errors::ColorError::UnrecogniazedRGB(other.to_string()))? .into_format::() .into_color(), Parameters::default_static_wp(40.0), ); Ok(hct.difference(&other_hct)) } #[wasm_bindgen] pub fn relative_differ_in_hct( color: &str, other: &str, ) -> Result { let hct = Cam16Jch::from_xyz( Srgb::from_str(color) .map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))? .into_format::() .into_color(), Parameters::default_static_wp(40.0), ); let other_hct = Cam16Jch::from_xyz( Srgb::from_str(other) .map_err(|_| errors::ColorError::UnrecogniazedRGB(other.to_string()))? .into_format::() .into_color(), Parameters::default_static_wp(40.0), ); Ok(hct.difference_relative(&other_hct)) } #[wasm_bindgen] pub fn differ_in_oklch( color: &str, other: &str, ) -> Result { let oklch = Oklch::from_color( Srgb::from_str(color) .map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))? .into_format::(), ); let other_oklch = Oklch::from_color( Srgb::from_str(other) .map_err(|_| errors::ColorError::UnrecogniazedRGB(other.to_string()))? .into_format::(), ); Ok(oklch.difference(&other_oklch)) } #[wasm_bindgen] pub fn relative_differ_in_oklch( color: &str, other: &str, ) -> Result { let oklch = Oklch::from_color( Srgb::from_str(color) .map_err(|_| errors::ColorError::UnrecogniazedRGB(color.to_string()))? .into_format::(), ); let other_oklch = Oklch::from_color( Srgb::from_str(other) .map_err(|_| errors::ColorError::UnrecogniazedRGB(other.to_string()))? .into_format::(), ); Ok(oklch.difference_relative(&other_oklch)) } #[wasm_bindgen] pub fn tint_scale( basic_color: &str, mixed_color: &str, ) -> Result { let basic_color = Srgb::from_str(basic_color) .map_err(|_| errors::ColorError::UnrecogniazedRGB(basic_color.to_string()))? .into_format::(); let mixed_color = Srgb::from_str(mixed_color) .map_err(|_| errors::ColorError::UnrecogniazedRGB(mixed_color.to_string()))? .into_format::(); Ok(reversing::MixReversing::from_tint_rgb( basic_color, mixed_color, )) } #[wasm_bindgen] pub fn shade_scale( basic_color: &str, mixed_color: &str, ) -> Result { let basic_color = Srgb::from_str(basic_color) .map_err(|_| errors::ColorError::UnrecogniazedRGB(basic_color.to_string()))? .into_format::(); let mixed_color = Srgb::from_str(mixed_color) .map_err(|_| errors::ColorError::UnrecogniazedRGB(mixed_color.to_string()))? .into_format::(); Ok(reversing::MixReversing::from_shade_rgb( basic_color, mixed_color, )) }