Skip to content

Commit 3ad52da

Browse files
authored
Merge pull request #659 from 10ne1/dev/aratiu/allow-serializing-colors
style/color: derive serialize & deserialize
2 parents 48ed63f + 7f96558 commit 3ad52da

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,22 @@ pub fn register_font(
571571
) -> Result<(), InvalidFont>
572572
```
573573

574+
- (De)serialization features
575+
576+
| Name | Description | Additional Dependency | Default? |
577+
|---------------|------------------------------------------|-----------------------|----------|
578+
| serialization | Enables serde (de)serialization support | serde | No |
579+
580+
`serialization` enables support for serializing and deserializing using the serde crate.
581+
Enable the feature via Cargo.toml then use it like the following:
582+
```rust,ignore
583+
#[cfg(feature = "serialization")]
584+
use serde::{Deserialize, Serialize};
585+
586+
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
587+
pub struct RGBAColor(pub u8, pub u8, pub u8, pub f64);
588+
```
589+
574590
- Coordinate features
575591

576592
| Name | Description | Additional Dependency |Default?|

plotters/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ deprecated = { level = "allow" }
2020
[dependencies]
2121
num-traits = "0.2.14"
2222
chrono = { version = "0.4.32", optional = true }
23+
serde = { version = "1.0.139", optional = true }
2324

2425
[dependencies.plotters-backend]
2526
version = "0.3.6"
@@ -113,6 +114,7 @@ ab_glyph = ["dep:ab_glyph", "once_cell"]
113114

114115
# Misc
115116
datetime = ["chrono"]
117+
serialization = ["serde"]
116118
evcxr = ["svg_backend"]
117119
evcxr_bitmap = ["evcxr", "bitmap_backend", "plotters-svg/bitmap_encoder"]
118120
deprecated_items = [] # Keep some of the deprecated items for backward compatibility
@@ -122,8 +124,8 @@ itertools = "0.10.0"
122124
criterion = "0.5.1"
123125
rayon = "1.5.1"
124126
serde_json = "1.0.82"
125-
serde = "1.0.139"
126127
serde_derive = "1.0.140"
128+
plotters = { path = ".", features = ["serialization"] }
127129

128130
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
129131
rand = "0.8.3"

plotters/src/style/color.rs

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ use super::ShapeStyle;
33

44
use plotters_backend::{BackendColor, BackendStyle};
55

6+
#[cfg(feature = "serialization")]
7+
use serde::{Deserialize, Serialize};
8+
69
use std::marker::PhantomData;
710

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

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

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

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

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

110116
impl BackendStyle for RGBAColor {
@@ -130,6 +136,7 @@ impl BackendStyle for RGBColor {
130136

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

135142
impl Color for HSLColor {

0 commit comments

Comments
 (0)