From a71a635eb8f24d35040e18fc691038a0a4e1ec79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Fri, 18 Jul 2025 09:09:58 +0800 Subject: [PATCH] =?UTF-8?q?refactor(color-module):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8C=96=E5=AE=9E=E7=8E=B0=E5=B9=B6=E6=94=B9?= =?UTF-8?q?=E8=BF=9B=E9=A2=9C=E8=89=B2=E8=AE=A1=E7=AE=97=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用serde_json简化Swatch和ColorSet的序列化实现 - 修改Swatch.get()方法以使用0-1范围的亮度值 - 改进search_for_common_wacg_color算法,使用平均值替代最小值 - 为ColorSet添加hover字段的序列化 --- .../src/schemes/q_style_2/color_set.rs | 30 +++++++++++-------- color-module/src/schemes/q_style_2/swatch.rs | 23 +++++++------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/color-module/src/schemes/q_style_2/color_set.rs b/color-module/src/schemes/q_style_2/color_set.rs index a3dcc66..f67ed92 100644 --- a/color-module/src/schemes/q_style_2/color_set.rs +++ b/color-module/src/schemes/q_style_2/color_set.rs @@ -2,8 +2,9 @@ use core::f32; use std::sync::Arc; use linked_hash_map::LinkedHashMap; -use palette::{color_difference::Wcag21RelativeContrast, luma::Luma, Oklch}; -use serde::{ser::SerializeStruct, Serialize}; +use palette::{Oklch, color_difference::Wcag21RelativeContrast, luma::Luma}; +use serde::Serialize; +use serde_json::json; use crate::{ convert::{map_oklch_to_luma, map_oklch_to_srgb_hex}, @@ -44,11 +45,11 @@ fn search_for_common_wacg_color( for scan_lightness in (0..=100).map(|x| x as f32 / 100.0) { let new_target = neutral_swatch.get(scan_lightness); let new_target_luma = map_oklch_to_luma(&new_target); - let reference_wacgs = reference_colors + let reference_wacgs_sum: f32 = reference_colors .iter() .map(|ref_color| match_wacg(&ref_color, &new_target_luma) - minium_ratio) - .min_by(|a, b| a.abs().total_cmp(&b.abs())) - .unwrap_or(f32::NEG_INFINITY); + .sum(); + let reference_wacgs = reference_wacgs_sum / reference_colors.len() as f32; if reference_wacgs.abs() < closest_match.1.abs() { closest_match = (scan_lightness, reference_wacgs); } @@ -273,19 +274,22 @@ impl Serialize for ColorSet { S: serde::Serializer, { let root = map_oklch_to_srgb_hex(&self.root); + let hover = map_oklch_to_srgb_hex(&self.hover); let active = map_oklch_to_srgb_hex(&self.active); let focus = map_oklch_to_srgb_hex(&self.focus); let disabled = map_oklch_to_srgb_hex(&self.disabled); let on_root = map_oklch_to_srgb_hex(&self.on_root); let on_disabled = map_oklch_to_srgb_hex(&self.on_disabled); - let mut state = serializer.serialize_struct("ColorSet", 6)?; - state.serialize_field("root", &root)?; - state.serialize_field("active", &active)?; - state.serialize_field("focus", &focus)?; - state.serialize_field("disabled", &disabled)?; - state.serialize_field("onRoot", &on_root)?; - state.serialize_field("onDisabled", &on_disabled)?; - state.end() + let color_set_object = json!({ + "root": root, + "hover": hover, + "active": active, + "focus": focus, + "disabled": disabled, + "onRoot": on_root, + "onDisabled": on_disabled, + }); + color_set_object.serialize(serializer) } } diff --git a/color-module/src/schemes/q_style_2/swatch.rs b/color-module/src/schemes/q_style_2/swatch.rs index 6b20c9f..453cf34 100644 --- a/color-module/src/schemes/q_style_2/swatch.rs +++ b/color-module/src/schemes/q_style_2/swatch.rs @@ -1,6 +1,7 @@ use linked_hash_map::LinkedHashMap; use palette::Oklch; -use serde::{ser::SerializeMap, Serialize}; +use serde::Serialize; +use serde_json::Value; use crate::convert::map_oklch_to_srgb_hex; @@ -17,9 +18,9 @@ impl Swatch { } pub fn get>(&self, lightness: L) -> Oklch { - let request_lightness: f32 = lightness.into(); + let request_lightness: f32 = lightness.into() / 100.0; Oklch { - l: request_lightness.clamp(10.0, 98.0), + l: request_lightness.clamp(0.1, 0.98), ..self.0.clone() } } @@ -81,13 +82,15 @@ impl Serialize for Swatch { where S: serde::Serializer, { - let mut state = serializer.serialize_map(Some(SWATCH_LIGHTINGS.len()))?; + let swatch_object = SWATCH_LIGHTINGS + .iter() + .map(|l| { + let color = self.get_hex(*l); + let key = format!("{l:02}"); + (key, color) + }) + .collect::(); - for l in SWATCH_LIGHTINGS { - let color = self.get_hex(l); - state.serialize_entry(&format!("{l:02}"), &color)?; - } - - state.end() + swatch_object.serialize(serializer) } }