Compare commits

...

3 Commits

Author SHA1 Message Date
徐涛
3b0600e64a 修复Swatch中顺序的保持。 2025-03-09 10:19:32 +08:00
徐涛
21b538af99 调整CSS自动Scheme的输出。 2025-03-09 10:16:35 +08:00
徐涛
e170e3c11d 增加可保持顺序的Map和Set数据结构。 2025-03-09 10:16:15 +08:00
13 changed files with 67 additions and 76 deletions

View File

@ -9,6 +9,8 @@ crate-type = ["cdylib"]
[dependencies] [dependencies]
color-name = "1.1.0" color-name = "1.1.0"
enum-iterator = "2.1.0" enum-iterator = "2.1.0"
linked-hash-map = { version = "0.5.6", features = ["serde", "serde_impl"] }
linked_hash_set = { version = "0.1.5", features = ["serde"] }
palette = { version = "0.7.6", features = ["serde"] } palette = { version = "0.7.6", features = ["serde"] }
serde = { version = "1.0.216", features = ["derive"] } serde = { version = "1.0.216", features = ["derive"] }
serde-wasm-bindgen = "0.6.5" serde-wasm-bindgen = "0.6.5"

View File

@ -1,5 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use linked_hash_map::LinkedHashMap;
use serde::{ser::SerializeStruct, Serialize}; use serde::{ser::SerializeStruct, Serialize};
use crate::{convert::map_hsl_to_srgb_hex, errors, schemes::material_design_2::swatch::M2Swatch}; use crate::{convert::map_hsl_to_srgb_hex, errors, schemes::material_design_2::swatch::M2Swatch};
@ -81,8 +82,8 @@ impl M2BaselineColors {
variable_lines variable_lines
} }
pub fn to_css_auto_scheme_collection(&self) -> HashMap<String, String> { pub fn to_css_auto_scheme_collection(&self) -> LinkedHashMap<String, String> {
let mut variables = HashMap::new(); let mut variables = LinkedHashMap::new();
variables.extend(self.primary.to_css_auto_scheme_collection("primary")); variables.extend(self.primary.to_css_auto_scheme_collection("primary"));
variables.extend(self.secondary.to_css_auto_scheme_collection("secondary")); variables.extend(self.secondary.to_css_auto_scheme_collection("secondary"));

View File

@ -1,5 +1,4 @@
use std::collections::HashMap; use linked_hash_map::LinkedHashMap;
use serde::Serialize; use serde::Serialize;
use crate::{convert::map_hsl_to_srgb_hex, errors}; use crate::{convert::map_hsl_to_srgb_hex, errors};
@ -84,8 +83,8 @@ impl M2ColorSet {
variable_lines variable_lines
} }
pub fn to_css_auto_scheme_collection(&self, name: &str) -> HashMap<String, String> { pub fn to_css_auto_scheme_collection(&self, name: &str) -> LinkedHashMap<String, String> {
let mut variables = HashMap::new(); let mut variables = LinkedHashMap::new();
variables.insert(format!("{}", name), self.root.clone()); variables.insert(format!("{}", name), self.root.clone());
variables.insert(format!("{}-variant", name), self.variant.clone()); variables.insert(format!("{}-variant", name), self.variant.clone());

View File

@ -1,6 +1,5 @@
use std::collections::HashSet;
use baseline::M2BaselineColors; use baseline::M2BaselineColors;
use linked_hash_set::LinkedHashSet;
use palette::Hsl; use palette::Hsl;
use serde::Serialize; use serde::Serialize;
@ -52,20 +51,20 @@ impl SchemeExport for MaterialDesign2Scheme {
fn output_css_auto_scheme_variables(&self) -> String { fn output_css_auto_scheme_variables(&self) -> String {
let mut auto_scheme_variables = Vec::new(); let mut auto_scheme_variables = Vec::new();
let mut keys = HashSet::new(); let mut keys = LinkedHashSet::new();
let light_baseline = self.light.to_css_auto_scheme_collection(); let light_baseline = self.light.to_css_auto_scheme_collection();
let dark_baseline = self.dark.to_css_auto_scheme_collection(); let dark_baseline = self.dark.to_css_auto_scheme_collection();
auto_scheme_variables.push(format!("--color-white: #{};", self.white)); auto_scheme_variables.push(format!("--color-white: #{};", self.white));
auto_scheme_variables.push(format!("--color-black: #{};", self.black)); auto_scheme_variables.push(format!("--color-black: #{};", self.black));
keys.extend(light_baseline.keys()); keys.extend(light_baseline.keys().cloned());
keys.extend(dark_baseline.keys()); keys.extend(dark_baseline.keys().cloned());
for key in keys { for key in keys {
match (light_baseline.get(key), dark_baseline.get(key)) { match (light_baseline.get(&key), dark_baseline.get(&key)) {
(Some(light), Some(dark)) => { (Some(light), Some(dark)) => {
auto_scheme_variables.push(format!( auto_scheme_variables.push(format!(
"--color-{}: light-dark(#{}, #{}):", "--color-{}: light-dark(#{}, #{});",
key, light, dark key, light, dark
)); ));
} }

View File

@ -1,5 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use linked_hash_map::LinkedHashMap;
use serde::Serialize; use serde::Serialize;
use crate::convert::map_lch_to_srgb_hex; use crate::convert::map_lch_to_srgb_hex;
@ -131,8 +132,8 @@ impl M3BaselineColors {
css_variables css_variables
} }
pub fn to_css_auto_scheme_collection(&self) -> HashMap<String, String> { pub fn to_css_auto_scheme_collection(&self) -> LinkedHashMap<String, String> {
let mut collection = HashMap::new(); let mut collection = LinkedHashMap::new();
collection.extend(self.primary.to_css_auto_scheme_collection("primary")); collection.extend(self.primary.to_css_auto_scheme_collection("primary"));
collection.extend(self.secondary.to_css_auto_scheme_collection("secondary")); collection.extend(self.secondary.to_css_auto_scheme_collection("secondary"));

View File

@ -1,5 +1,4 @@
use std::collections::HashMap; use linked_hash_map::LinkedHashMap;
use serde::Serialize; use serde::Serialize;
use crate::convert::map_lch_to_srgb_hex; use crate::convert::map_lch_to_srgb_hex;
@ -108,8 +107,8 @@ impl M3ColorSet {
variable_lines variable_lines
} }
pub fn to_css_auto_scheme_collection(&self, name: &str) -> HashMap<String, String> { pub fn to_css_auto_scheme_collection(&self, name: &str) -> LinkedHashMap<String, String> {
let mut variables = HashMap::new(); let mut variables = LinkedHashMap::new();
variables.insert(format!("{}", name), self.root.clone()); variables.insert(format!("{}", name), self.root.clone());
variables.insert(format!("on-{}", name), self.on_root.clone()); variables.insert(format!("on-{}", name), self.on_root.clone());

View File

@ -1,8 +1,9 @@
use std::collections::{HashMap, HashSet}; use std::collections::HashMap;
use std::str::FromStr; use std::str::FromStr;
pub use baseline::M3BaselineColors; pub use baseline::M3BaselineColors;
pub use color_set::M3ColorSet; pub use color_set::M3ColorSet;
use linked_hash_set::LinkedHashSet;
use palette::{IntoColor, Lch, Srgb}; use palette::{IntoColor, Lch, Srgb};
use serde::Serialize; use serde::Serialize;
pub use surface::M3SurfaceSet; pub use surface::M3SurfaceSet;
@ -113,19 +114,19 @@ impl SchemeExport for MaterialDesign3Scheme {
fn output_css_auto_scheme_variables(&self) -> String { fn output_css_auto_scheme_variables(&self) -> String {
let mut auto_scheme_variables = Vec::new(); let mut auto_scheme_variables = Vec::new();
let mut keys = HashSet::new(); let mut keys = LinkedHashSet::new();
let light_baseline = self.light_baseline.to_css_auto_scheme_collection(); let light_baseline = self.light_baseline.to_css_auto_scheme_collection();
let dark_baseline = self.dark_baseline.to_css_auto_scheme_collection(); let dark_baseline = self.dark_baseline.to_css_auto_scheme_collection();
auto_scheme_variables.push(format!("--color-white: #{};", self.white)); auto_scheme_variables.push(format!("--color-white: #{};", self.white));
auto_scheme_variables.push(format!("--color-black: #{};", self.black)); auto_scheme_variables.push(format!("--color-black: #{};", self.black));
keys.extend(light_baseline.keys()); keys.extend(light_baseline.keys().cloned());
keys.extend(dark_baseline.keys()); keys.extend(dark_baseline.keys().cloned());
for key in keys { for key in keys {
match (light_baseline.get(key), dark_baseline.get(key)) { match (light_baseline.get(&key), dark_baseline.get(&key)) {
(Some(light), Some(dark)) => { (Some(light), Some(dark)) => {
auto_scheme_variables.push(format!( auto_scheme_variables.push(format!(
"--color-{}: light-dark(#{}, #{}):", "--color-{}: light-dark(#{}, #{});",
key, light, dark key, light, dark
)); ));
} }

View File

@ -1,5 +1,4 @@
use std::collections::HashMap; use linked_hash_map::LinkedHashMap;
use serde::Serialize; use serde::Serialize;
use crate::convert::map_lch_to_srgb_hex; use crate::convert::map_lch_to_srgb_hex;
@ -138,8 +137,8 @@ impl M3SurfaceSet {
css_variables css_variables
} }
pub fn to_css_auto_scheme_collection(&self) -> HashMap<String, String> { pub fn to_css_auto_scheme_collection(&self) -> LinkedHashMap<String, String> {
let mut auto_scheme_collection = HashMap::new(); let mut auto_scheme_collection = LinkedHashMap::new();
auto_scheme_collection.insert(format!("surface"), self.root.clone()); auto_scheme_collection.insert(format!("surface"), self.root.clone());
auto_scheme_collection.insert(format!("surface-dim"), self.dim.clone()); auto_scheme_collection.insert(format!("surface-dim"), self.dim.clone());

View File

@ -1,5 +1,4 @@
use std::collections::HashMap; use linked_hash_map::LinkedHashMap;
use palette::{ use palette::{
color_theory::{Analogous, Complementary, SplitComplementary, Tetradic, Triadic}, color_theory::{Analogous, Complementary, SplitComplementary, Tetradic, Triadic},
Oklch, ShiftHue, Oklch, ShiftHue,
@ -160,8 +159,8 @@ impl Baseline {
variables variables
} }
pub fn to_css_auto_scheme_collection(&self) -> HashMap<String, String> { pub fn to_css_auto_scheme_collection(&self) -> LinkedHashMap<String, String> {
let mut collection = HashMap::new(); let mut collection = LinkedHashMap::new();
collection.extend(self.primary.to_css_auto_scheme_collection("primary")); collection.extend(self.primary.to_css_auto_scheme_collection("primary"));
if let Some(secondary) = &self.secondary { if let Some(secondary) = &self.secondary {

View File

@ -1,5 +1,4 @@
use std::collections::HashMap; use linked_hash_map::LinkedHashMap;
use palette::{color_difference::Wcag21RelativeContrast, luma::Luma, Oklch}; use palette::{color_difference::Wcag21RelativeContrast, luma::Luma, Oklch};
use serde::{ser::SerializeStruct, Serialize}; use serde::{ser::SerializeStruct, Serialize};
@ -181,8 +180,8 @@ impl ColorSet {
variables variables
} }
pub fn to_css_auto_scheme_collection(&self, name: &str) -> HashMap<String, String> { pub fn to_css_auto_scheme_collection(&self, name: &str) -> LinkedHashMap<String, String> {
let mut collection = HashMap::new(); let mut collection = LinkedHashMap::new();
collection.insert(format!("{}", name), map_oklch_to_srgb_hex(&self.root)); collection.insert(format!("{}", name), map_oklch_to_srgb_hex(&self.root));
collection.insert( collection.insert(

View File

@ -1,6 +1,7 @@
use std::{collections::HashSet, str::FromStr}; use std::str::FromStr;
use baseline::Baseline; use baseline::Baseline;
use linked_hash_set::LinkedHashSet;
use palette::FromColor; use palette::FromColor;
use scheme_setting::{ColorExpand, WACGSetting}; use scheme_setting::{ColorExpand, WACGSetting};
use serde::Serialize; use serde::Serialize;
@ -132,14 +133,14 @@ impl SchemeExport for QScheme {
fn output_css_auto_scheme_variables(&self) -> String { fn output_css_auto_scheme_variables(&self) -> String {
let mut collection = Vec::new(); let mut collection = Vec::new();
let mut keys = HashSet::new(); let mut keys = LinkedHashSet::new();
let light_collection = self.light.to_css_auto_scheme_collection(); let light_collection = self.light.to_css_auto_scheme_collection();
let dark_collection = self.dark.to_css_auto_scheme_collection(); let dark_collection = self.dark.to_css_auto_scheme_collection();
keys.extend(light_collection.keys()); keys.extend(light_collection.keys().cloned());
keys.extend(dark_collection.keys()); keys.extend(dark_collection.keys().cloned());
for key in keys { for key in keys {
match (light_collection.get(key), dark_collection.get(key)) { match (light_collection.get(&key), dark_collection.get(&key)) {
(Some(light), Some(dark)) => { (Some(light), Some(dark)) => {
collection.push(format!( collection.push(format!(
"--color-{}: light-dark(#{}, #{});", "--color-{}: light-dark(#{}, #{});",

View File

@ -1,6 +1,8 @@
use linked_hash_map::LinkedHashMap;
use linked_hash_set::LinkedHashSet;
use palette::FromColor; use palette::FromColor;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet}; use std::collections::HashMap;
use std::str::FromStr; use std::str::FromStr;
pub use setting::SwatchSchemeSetting; pub use setting::SwatchSchemeSetting;
@ -95,42 +97,32 @@ impl SchemeExport for SwatchScheme {
fn output_css_auto_scheme_variables(&self) -> String { fn output_css_auto_scheme_variables(&self) -> String {
let mut variables = Vec::new(); let mut variables = Vec::new();
let mut keys = HashSet::new(); let mut keys = LinkedHashSet::new();
let mut names = HashSet::new(); let mut light_collections = LinkedHashMap::new();
let mut light_collections = HashMap::new(); let mut dark_collections = LinkedHashMap::new();
let mut dark_collections = HashMap::new();
names.extend(self.light.keys());
names.extend(self.dark.keys());
for (name, swatch) in &self.light { for (name, swatch) in &self.light {
let collection = swatch.to_css_auto_scheme_collection(&name); light_collections.extend(swatch.to_css_auto_scheme_collection(&name.to_lowercase()));
keys.extend(collection.keys().cloned());
light_collections.insert(name.clone(), collection.clone());
} }
for (name, swatch) in &self.dark { for (name, swatch) in &self.dark {
let collection = swatch.to_css_auto_scheme_collection(&name); dark_collections.extend(swatch.to_css_auto_scheme_collection(&name.to_lowercase()));
keys.extend(collection.keys().cloned());
dark_collections.insert(name.clone(), collection.clone());
} }
for name in names { keys.extend(light_collections.keys().cloned());
for key in keys.iter() { keys.extend(dark_collections.keys().cloned());
match (
light_collections.get(name).and_then(|lc| lc.get(key)), for key in keys.iter() {
dark_collections.get(name).and_then(|dc| dc.get(key)), match (light_collections.get(key), dark_collections.get(key)) {
) { (Some(light), Some(dark)) => {
(Some(light), Some(dark)) => { variables.push(format!(
variables.push(format!( "--color-{}: light-dark(#{}, #{});",
"--color-{}-{}: light-dark(#{}, #{});", key, light, dark
name, key, light, dark ));
));
}
(Some(color), None) | (None, Some(color)) => {
variables.push(format!("--color-{}-{}: #{};", name, key, color));
}
(None, None) => {}
} }
(Some(color), None) | (None, Some(color)) => {
variables.push(format!("--color-{}: #{};", key, color));
}
(None, None) => {}
} }
} }
variables.join("\n") variables.join("\n")

View File

@ -1,5 +1,4 @@
use std::collections::HashMap; use linked_hash_map::LinkedHashMap;
use palette::Oklch; use palette::Oklch;
use crate::convert::map_oklch_to_srgb_hex; use crate::convert::map_oklch_to_srgb_hex;
@ -96,8 +95,8 @@ impl Swatch {
variables variables
} }
pub fn to_css_auto_scheme_collection(&self, name: &str) -> HashMap<String, String> { pub fn to_css_auto_scheme_collection(&self, name: &str) -> LinkedHashMap<String, String> {
let mut collection = HashMap::new(); let mut collection = LinkedHashMap::new();
for (i, color) in self.swatch().iter().enumerate() { for (i, color) in self.swatch().iter().enumerate() {
collection.insert( collection.insert(
format!("{}-{}", name, i * 100), format!("{}-{}", name, i * 100),