From 52ecd0d3cb43e013473864ebf49ab5b2ae092a4f Mon Sep 17 00:00:00 2001 From: Allan Zhang Date: Wed, 13 Mar 2024 13:51:03 -0400 Subject: [PATCH] feat: optional serde feature `Serialize` is implemented for `Theme` and related structs. --- Cargo.lock | 1 + Cargo.toml | 2 ++ src/hct.rs | 4 ++++ src/palettes/tonal.rs | 11 +++++------ src/scheme.rs | 3 +++ src/utils/theme.rs | 8 ++++++++ 6 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0fbce87..8c24532 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -736,6 +736,7 @@ dependencies = [ "image", "indexmap", "reqwest", + "serde", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index 518bcf4..386d4df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,9 +14,11 @@ keywords = ["palette", "color-scheme", "material-you"] ahash = "0.8.11" indexmap = "2.2.5" image = { version = "0.25.0", optional = true } +serde = { version = "1.0.197", features = ["derive"], optional = true } [features] image = ["dep:image"] +serde = ["dep:serde"] [profile.dev] opt-level = 1 diff --git a/src/hct.rs b/src/hct.rs index b9b5e66..2ea0742 100644 --- a/src/hct.rs +++ b/src/hct.rs @@ -3,6 +3,9 @@ use core::fmt; use core::hash::Hash; use core::hash::Hasher; +#[cfg(feature = "serde")] +use serde::Serialize; + use crate::utils::color::lstar_from_argb; use crate::utils::color::lstar_from_y; use crate::utils::color::Argb; @@ -16,6 +19,7 @@ pub mod solver; pub mod viewing_conditions; #[derive(Clone, Copy, Debug, PartialOrd)] +#[cfg_attr(feature = "serde", derive(Serialize))] pub struct Hct { _hue: f64, _chroma: f64, diff --git a/src/palettes/tonal.rs b/src/palettes/tonal.rs index 9279b15..2760b75 100644 --- a/src/palettes/tonal.rs +++ b/src/palettes/tonal.rs @@ -1,4 +1,6 @@ use core::fmt; +#[cfg(feature = "serde")] +use serde::Serialize; use crate::hct::Hct; use crate::utils::color::Argb; @@ -13,6 +15,7 @@ use crate::utils::color::Argb; /// is not enforced. [get] will only return the input colors, corresponding to /// [commonTones]. #[derive(Clone, Copy, Debug, PartialOrd)] +#[cfg_attr(feature = "serde", derive(Serialize))] pub struct TonalPalette { _hue: f64, _chroma: f64, @@ -54,11 +57,7 @@ impl TonalPalette { /// Create a Tonal Palette from hue and chroma, which generates a key color. pub fn from_hue_and_chroma(hue: f64, chroma: f64) -> Self { - Self::new( - hue, - chroma, - TonalPalette::create_key_color(hue, chroma), - ) + Self::new(hue, chroma, TonalPalette::create_key_color(hue, chroma)) } /// Create colors using [hue] and [chroma]. @@ -119,7 +118,7 @@ impl TonalPalette { Hct::from(self.hue(), self.chroma(), tone as f64).into() } - pub fn get_hct(&self, tone: f64) -> Hct { + pub fn get_hct(&self, tone: f64) -> Hct { Hct::from(self.hue(), self.chroma(), tone) } } diff --git a/src/scheme.rs b/src/scheme.rs index 1e0ec49..c2d5fc2 100644 --- a/src/scheme.rs +++ b/src/scheme.rs @@ -1,4 +1,6 @@ #![allow(clippy::too_many_arguments)] +#[cfg(feature = "serde")] +use serde::Serialize; use core::array::IntoIter; use core::fmt; @@ -20,6 +22,7 @@ pub mod tonal_spot; pub mod vibrant; #[derive(Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(Serialize))] pub struct Scheme { pub primary: Argb, pub on_primary: Argb, diff --git a/src/utils/theme.rs b/src/utils/theme.rs index 5717608..cc87ab1 100644 --- a/src/utils/theme.rs +++ b/src/utils/theme.rs @@ -3,11 +3,14 @@ use crate::palettes::core::CorePalette; use crate::palettes::tonal::TonalPalette; use crate::scheme::tonal_spot::SchemeTonalSpot; use crate::scheme::Scheme; +#[cfg(feature = "serde")] +use serde::Serialize; use super::color::Argb; /// Custom color used to pair with a theme #[derive(Debug)] +#[cfg_attr(feature = "serde", derive(Serialize))] pub struct CustomColor { pub value: Argb, pub name: String, @@ -16,6 +19,7 @@ pub struct CustomColor { /// Color group #[derive(Debug)] +#[cfg_attr(feature = "serde", derive(Serialize))] pub struct ColorGroup { pub color: Argb, pub on_color: Argb, @@ -25,6 +29,7 @@ pub struct ColorGroup { /// Custom Color Group #[derive(Debug)] +#[cfg_attr(feature = "serde", derive(Serialize))] pub struct CustomColorGroup { pub color: CustomColor, pub value: Argb, @@ -33,12 +38,14 @@ pub struct CustomColorGroup { } #[derive(Debug)] +#[cfg_attr(feature = "serde", derive(Serialize))] pub struct Schemes { pub light: Scheme, pub dark: Scheme, } #[derive(Debug)] +#[cfg_attr(feature = "serde", derive(Serialize))] pub struct Palettes { pub primary: TonalPalette, pub secondary: TonalPalette, @@ -50,6 +57,7 @@ pub struct Palettes { // Theme #[derive(Debug)] +#[cfg_attr(feature = "serde", derive(Serialize))] pub struct Theme { pub source: Argb, pub schemes: Schemes,