Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style/color: derive serialize & deserialize #659

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,22 @@ pub fn register_font(
) -> Result<(), InvalidFont>
```

- (De)serialization features

| Name | Description | Additional Dependency | Default? |
|---------------|------------------------------------------|-----------------------|----------|
| serialization | Enables serde (de)serialization support | serde | No |

`serialization` enables support for serializing and deserializing using the serde crate.
Enable the feature via Cargo.toml then use it like the following:
```rust,ignore
#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};

#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
pub struct RGBAColor(pub u8, pub u8, pub u8, pub f64);
```

- Coordinate features

| Name | Description | Additional Dependency |Default?|
Expand Down
4 changes: 3 additions & 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 = { version = "1.0.139", optional = true }

[dependencies.plotters-backend]
version = "0.3.6"
Expand Down Expand Up @@ -113,6 +114,7 @@ ab_glyph = ["dep:ab_glyph", "once_cell"]

# Misc
datetime = ["chrono"]
serialization = ["serde"]
evcxr = ["svg_backend"]
evcxr_bitmap = ["evcxr", "bitmap_backend", "plotters-svg/bitmap_encoder"]
deprecated_items = [] # Keep some of the deprecated items for backward compatibility
Expand All @@ -122,8 +124,8 @@ 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"
plotters = { path = ".", features = ["serialization"] }

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

use plotters_backend::{BackendColor, BackendStyle};

#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};

use std::marker::PhantomData;

/// Any color representation
Expand Down Expand Up @@ -64,6 +67,7 @@ impl<T: Color> Color for &'_ T {
///
/// If you want to directly create a RGB color with transparency use [RGBColor::mix]
#[derive(Copy, Clone, PartialEq, Debug, Default)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
pub struct RGBAColor(pub u8, pub u8, pub u8, pub f64);

impl Color for RGBAColor {
Expand All @@ -84,6 +88,7 @@ impl From<RGBColor> for RGBAColor {

/// A color in the given palette
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
pub struct PaletteColor<P: Palette>(usize, PhantomData<P>);

impl<P: Palette> PaletteColor<P> {
Expand All @@ -105,6 +110,7 @@ impl<P: Palette> Color for PaletteColor<P> {

/// The color described by its RGB value
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
pub struct RGBColor(pub u8, pub u8, pub u8);

impl BackendStyle for RGBAColor {
Expand All @@ -130,6 +136,7 @@ impl BackendStyle for RGBColor {

/// The color described by HSL color space
#[derive(Copy, Clone, PartialEq, Debug, Default)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
pub struct HSLColor(pub f64, pub f64, pub f64);

impl Color for HSLColor {
Expand Down
Loading