调整基于Oklch的色相分界。

This commit is contained in:
徐涛 2025-01-10 14:50:14 +08:00
parent 9fec4a31e9
commit 67164e35fa
2 changed files with 10 additions and 10 deletions

View File

@ -20,7 +20,7 @@ pub struct ColorDescription {
const COLOR_CARDS_JSON: &str = include_str!("colorcards.json"); const COLOR_CARDS_JSON: &str = include_str!("colorcards.json");
pub const COLOR_CARDS: LazyLock<Vec<ColorDescription>> = pub const COLOR_CARDS: LazyLock<Vec<ColorDescription>> =
LazyLock::new(|| serde_json::from_str(COLOR_CARDS_JSON).expect("Failed to parse color cards")); LazyLock::new(|| serde_json::from_str(COLOR_CARDS_JSON).expect("Failed to parse color cards"));
const CHROMA_EPSILON: f32 = 0.0001; const CHROMA_EPSILON: f32 = 0.02;
#[derive(Debug, Clone, PartialEq, Display, EnumString, EnumIter)] #[derive(Debug, Clone, PartialEq, Display, EnumString, EnumIter)]
#[strum(serialize_all = "lowercase")] #[strum(serialize_all = "lowercase")]
@ -42,7 +42,7 @@ pub enum Category {
impl Category { impl Category {
pub fn from_oklch_components(lightness: f32, chroma: f32, hue: f32) -> Self { pub fn from_oklch_components(lightness: f32, chroma: f32, hue: f32) -> Self {
if chroma < CHROMA_EPSILON { if chroma < CHROMA_EPSILON {
if lightness < 0.1 { if lightness < 0.15 {
Category::Black Category::Black
} else if lightness > 0.9 { } else if lightness > 0.9 {
Category::White Category::White
@ -52,15 +52,15 @@ impl Category {
} else { } else {
let processed_hue = hue % 360.0; let processed_hue = hue % 360.0;
match processed_hue { match processed_hue {
0.0..=30.0 => Category::Red, 0.0..=15.0 => Category::Magenta,
30.0..=60.0 => Category::Orange, 15.0..=45.0 => Category::Red,
60.0..=90.0 => Category::Yellow, 45.0..=75.0 => Category::Orange,
90.0..=150.0 => Category::Green, 75.0..=120.0 => Category::Yellow,
150.0..=210.0 => Category::Cyan, 120.0..=180.0 => Category::Green,
180.0..=210.0 => Category::Cyan,
210.0..=270.0 => Category::Blue, 210.0..=270.0 => Category::Blue,
270.0..=300.0 => Category::Purple, 270.0..=345.0 => Category::Purple,
300.0..=330.0 => Category::Magenta, 345.0..=360.0 => Category::Magenta,
330.0..=360.0 => Category::Red,
_ => Category::Unknown, _ => Category::Unknown,
} }
} }