Skip to content

Commit

Permalink
style/color: derive serialize & deserialize
Browse files Browse the repository at this point in the history
Using serde is useful in production, not just during development,
so move it to normal dependencies instead of dev-dependencies.

This allows to derive serialize/deserialize for the color structs
which are very simple using the default implementation, allowing
to pass colors along via serialization channels and formats.
  • Loading branch information
10ne1 committed Nov 1, 2024
1 parent cb7791f commit 3df125c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion plotters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ deprecated = { level = "allow" }
[dependencies]
num-traits = "0.2.14"
chrono = { version = "0.4.32", optional = true }
serde = "1.0.139"

[dependencies.plotters-backend]
version = "0.3.6"
Expand Down Expand Up @@ -122,7 +123,6 @@ itertools = "0.10.0"
criterion = "0.5.1"
rayon = "1.5.1"
serde_json = "1.0.82"
serde = "1.0.139"
serde_derive = "1.0.140"

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
Expand Down
10 changes: 6 additions & 4 deletions plotters/src/style/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use super::ShapeStyle;

use plotters_backend::{BackendColor, BackendStyle};

use serde::{Deserialize, Serialize};

use std::marker::PhantomData;

/// Any color representation
Expand Down Expand Up @@ -63,7 +65,7 @@ impl<T: Color> Color for &'_ T {
/// of color
///
/// If you want to directly create a RGB color with transparency use [RGBColor::mix]
#[derive(Copy, Clone, PartialEq, Debug, Default)]
#[derive(Copy, Clone, PartialEq, Debug, Default, Serialize, Deserialize)]

Check failure on line 68 in plotters/src/style/color.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest)

cannot find derive macro `Serialize` in this scope

Check failure on line 68 in plotters/src/style/color.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest)

cannot find derive macro `Deserialize` in this scope
pub struct RGBAColor(pub u8, pub u8, pub u8, pub f64);

impl Color for RGBAColor {
Expand All @@ -83,7 +85,7 @@ impl From<RGBColor> for RGBAColor {
}

/// A color in the given palette
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default, Serialize, Deserialize)]

Check failure on line 88 in plotters/src/style/color.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest)

cannot find derive macro `Serialize` in this scope

Check failure on line 88 in plotters/src/style/color.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest)

cannot find derive macro `Deserialize` in this scope
pub struct PaletteColor<P: Palette>(usize, PhantomData<P>);

impl<P: Palette> PaletteColor<P> {
Expand All @@ -104,7 +106,7 @@ impl<P: Palette> Color for PaletteColor<P> {
}

/// The color described by its RGB value
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default, Serialize, Deserialize)]

Check failure on line 109 in plotters/src/style/color.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest)

cannot find derive macro `Serialize` in this scope

Check failure on line 109 in plotters/src/style/color.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest)

cannot find derive macro `Deserialize` in this scope
pub struct RGBColor(pub u8, pub u8, pub u8);

impl BackendStyle for RGBAColor {
Expand All @@ -129,7 +131,7 @@ impl BackendStyle for RGBColor {
}

/// The color described by HSL color space
#[derive(Copy, Clone, PartialEq, Debug, Default)]
#[derive(Copy, Clone, PartialEq, Debug, Default, Serialize, Deserialize)]

Check failure on line 134 in plotters/src/style/color.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest)

cannot find derive macro `Serialize` in this scope

Check failure on line 134 in plotters/src/style/color.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest)

cannot find derive macro `Deserialize` in this scope
pub struct HSLColor(pub f64, pub f64, pub f64);

impl Color for HSLColor {
Expand Down

0 comments on commit 3df125c

Please sign in to comment.