Skip to content

Commit

Permalink
feat: optional serde feature
Browse files Browse the repository at this point in the history
`Serialize` is implemented for `Theme` and related structs.
  • Loading branch information
allan2 committed Mar 13, 2024
1 parent 5f6ce69 commit 52ecd0d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/hct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
11 changes: 5 additions & 6 deletions src/palettes/tonal.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use core::fmt;
#[cfg(feature = "serde")]
use serde::Serialize;

use crate::hct::Hct;
use crate::utils::color::Argb;
Expand All @@ -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,
Expand Down Expand Up @@ -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].
Expand Down Expand Up @@ -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)
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/scheme.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![allow(clippy::too_many_arguments)]
#[cfg(feature = "serde")]
use serde::Serialize;

use core::array::IntoIter;
use core::fmt;
Expand All @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions src/utils/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 52ecd0d

Please sign in to comment.