Skip to content

Commit 2cf740b

Browse files
committed
Cleanup the color trait requirements
1 parent d76398f commit 2cf740b

File tree

9 files changed

+50
-34
lines changed

9 files changed

+50
-34
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exclude = ["doc-template/*"]
1515
[dependencies]
1616
num-traits = "0.2.11"
1717
chrono = { version = "0.4.11", optional = true }
18-
plotters-backend = { git = "https://github.com/chrisduerr/plotters-backend", rev = "529f08f3aa5ede8e71950c1a5d3be4683330f549" }
18+
plotters-backend = "^0.3"
1919
plotters-svg = {version = "^0.3.*", optional = true}
2020

2121
[dependencies.plotters-bitmap]

src/drawing/area.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ impl<DB: DrawingBackend, CT: CoordTranslate> DrawingArea<DB, CT> {
286286
backend.draw_rect(
287287
(self.rect.x0, self.rect.y0),
288288
(self.rect.x1 - 1, self.rect.y1 - 1),
289-
color,
289+
&color.to_backend_color(),
290290
true,
291291
)
292292
})
@@ -299,7 +299,7 @@ impl<DB: DrawingBackend, CT: CoordTranslate> DrawingArea<DB, CT> {
299299
color: &ColorType,
300300
) -> Result<(), DrawingAreaError<DB>> {
301301
let pos = self.coord.translate(&pos);
302-
self.backend_ops(|b| b.draw_pixel(pos, color.color()))
302+
self.backend_ops(|b| b.draw_pixel(pos, color.to_backend_color()))
303303
}
304304

305305
/// Present all the pending changes to the backend

src/element/basic_shapes.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{Drawable, PointCollection};
2-
use crate::style::{ShapeStyle, SizeDesc};
3-
use plotters_backend::{BackendCoord, BackendStyle, DrawingBackend, DrawingErrorKind};
2+
use crate::style::{Color, ShapeStyle, SizeDesc};
3+
use plotters_backend::{BackendCoord, DrawingBackend, DrawingErrorKind};
44

55
/// An element of a single pixel
66
pub struct Pixel<Coord> {
@@ -33,7 +33,7 @@ impl<Coord, DB: DrawingBackend> Drawable<DB> for Pixel<Coord> {
3333
_: (u32, u32),
3434
) -> Result<(), DrawingErrorKind<DB::ErrorType>> {
3535
if let Some((x, y)) = points.next() {
36-
return backend.draw_pixel((x, y), self.style.color());
36+
return backend.draw_pixel((x, y), self.style.color.to_backend_color());
3737
}
3838
Ok(())
3939
}
@@ -320,7 +320,7 @@ impl<Coord, DB: DrawingBackend> Drawable<DB> for Polygon<Coord> {
320320
backend: &mut DB,
321321
_: (u32, u32),
322322
) -> Result<(), DrawingErrorKind<DB::ErrorType>> {
323-
backend.fill_polygon(points, &self.style.color)
323+
backend.fill_polygon(points, &self.style.color.to_backend_color())
324324
}
325325
}
326326

src/element/boxplot.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::marker::PhantomData;
22

33
use crate::data::Quartiles;
44
use crate::element::{Drawable, PointCollection};
5-
use crate::style::{ShapeStyle, BLACK};
5+
use crate::style::{Color, ShapeStyle, BLACK};
66
use plotters_backend::{BackendCoord, DrawingBackend, DrawingErrorKind};
77

88
/// The boxplot orientation trait
@@ -218,7 +218,12 @@ impl<K, DB: DrawingBackend, O: BoxplotOrient<K, f32>> Drawable<DB> for Boxplot<K
218218

219219
// |---[ | ]----|
220220
// _^^^_____________
221-
backend.draw_line(moved(points[0]), moved(points[1]), &self.style.color)?;
221+
222+
backend.draw_line(
223+
moved(points[0]),
224+
moved(points[1]),
225+
&self.style.color.to_backend_color(),
226+
)?;
222227

223228
// |---[ | ]----|
224229
// ____^______^_____

src/element/points.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::*;
22
use super::{Drawable, PointCollection};
3-
use crate::style::{ShapeStyle, SizeDesc};
3+
use crate::style::{Color, ShapeStyle, SizeDesc};
44
use plotters_backend::{BackendCoord, DrawingBackend, DrawingErrorKind};
55

66
/// The element that used to describe a point
@@ -94,7 +94,7 @@ impl<Coord, DB: DrawingBackend, Size: SizeDesc> Drawable<DB> for TriangleMarker<
9494
(rad.sin() * f64::from(size) + f64::from(y)).ceil() as i32,
9595
)
9696
});
97-
backend.fill_polygon(points, &self.style.color)?;
97+
backend.fill_polygon(points, &self.style.color.to_backend_color())?;
9898
}
9999
Ok(())
100100
}

src/style/color.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ use plotters_backend::{BackendColor, BackendStyle};
66
use std::marker::PhantomData;
77

88
/// Any color representation
9-
pub trait Color: BackendStyle {
9+
pub trait Color {
10+
/// Normalize this color representation to the backend color
11+
fn to_backend_color(&self) -> BackendColor;
12+
1013
/// Convert the RGB representation to the standard RGB tuple
1114
#[inline(always)]
1215
fn rgb(&self) -> (u8, u8, u8) {
13-
self.color().rgb
16+
self.to_backend_color().rgb
1417
}
1518

1619
/// Get the alpha channel of the color
1720
#[inline(always)]
1821
fn alpha(&self) -> f64 {
19-
self.color().alpha
22+
self.to_backend_color().alpha
2023
}
2124

2225
/// Mix the color with given opacity
@@ -50,23 +53,26 @@ pub trait Color: BackendStyle {
5053
}
5154
}
5255

53-
impl<T: Color> Color for &'_ T {}
56+
impl<T: Color> Color for &'_ T {
57+
fn to_backend_color(&self) -> BackendColor {
58+
<T as Color>::to_backend_color(*self)
59+
}
60+
}
5461

5562
/// The RGBA representation of the color, Plotters use RGBA as the internal representation
5663
/// of color
5764
#[derive(Copy, Clone, PartialEq, Debug)]
5865
pub struct RGBAColor(pub(crate) u8, pub(crate) u8, pub(crate) u8, pub(crate) f64);
5966

60-
impl BackendStyle for RGBAColor {
67+
impl Color for RGBAColor {
6168
#[inline(always)]
62-
fn color(&self) -> BackendColor {
69+
fn to_backend_color(&self) -> BackendColor {
6370
BackendColor {
6471
rgb: (self.0, self.1, self.2),
6572
alpha: self.3,
6673
}
6774
}
6875
}
69-
impl Color for RGBAColor {}
7076

7177
/// A color in the given palette
7278
pub struct PaletteColor<P: Palette>(usize, PhantomData<P>);
@@ -78,41 +84,48 @@ impl<P: Palette> PaletteColor<P> {
7884
}
7985
}
8086

81-
impl<P: Palette> BackendStyle for PaletteColor<P> {
87+
impl<P: Palette> Color for PaletteColor<P> {
8288
#[inline(always)]
83-
fn color(&self) -> BackendColor {
89+
fn to_backend_color(&self) -> BackendColor {
8490
BackendColor {
8591
rgb: P::COLORS[self.0],
8692
alpha: 1.0,
8793
}
8894
}
8995
}
9096

91-
impl<P: Palette> Color for PaletteColor<P> {}
92-
9397
/// The color described by its RGB value
9498
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
9599
pub struct RGBColor(pub u8, pub u8, pub u8);
96100

97-
impl BackendStyle for RGBColor {
98-
#[inline(always)]
101+
impl BackendStyle for RGBAColor {
99102
fn color(&self) -> BackendColor {
103+
self.to_backend_color()
104+
}
105+
}
106+
107+
impl Color for RGBColor {
108+
#[inline(always)]
109+
fn to_backend_color(&self) -> BackendColor {
100110
BackendColor {
101111
rgb: (self.0, self.1, self.2),
102112
alpha: 1.0,
103113
}
104114
}
105115
}
106-
107-
impl Color for RGBColor {}
116+
impl BackendStyle for RGBColor {
117+
fn color(&self) -> BackendColor {
118+
self.to_backend_color()
119+
}
120+
}
108121

109122
/// The color described by HSL color space
110123
pub struct HSLColor(pub f64, pub f64, pub f64);
111124

112-
impl BackendStyle for HSLColor {
125+
impl Color for HSLColor {
113126
#[inline(always)]
114127
#[allow(clippy::many_single_char_names)]
115-
fn color(&self) -> BackendColor {
128+
fn to_backend_color(&self) -> BackendColor {
116129
let (h, s, l) = (
117130
self.0.min(1.0).max(0.0),
118131
self.1.min(1.0).max(0.0),
@@ -159,5 +172,3 @@ impl BackendStyle for HSLColor {
159172
}
160173
}
161174
}
162-
163-
impl Color for HSLColor {}

src/style/font/font_desc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl<'a> FontDesc<'a> {
138138
pub fn color<C: Color>(&self, color: &C) -> TextStyle<'a> {
139139
TextStyle {
140140
font: self.clone(),
141-
color: color.color(),
141+
color: color.to_backend_color(),
142142
pos: Pos::default(),
143143
}
144144
}

src/style/shape.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<T: Color> From<T> for ShapeStyle {
4040

4141
impl BackendStyle for ShapeStyle {
4242
fn color(&self) -> BackendColor {
43-
self.color.color()
43+
self.color.to_backend_color()
4444
}
4545
fn stroke_width(&self) -> u32 {
4646
self.stroke_width

src/style/text.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl<'a> TextStyle<'a> {
7676
pub fn color<C: Color>(&self, color: &'a C) -> Self {
7777
Self {
7878
font: self.font.clone(),
79-
color: color.color(),
79+
color: color.to_backend_color(),
8080
pos: self.pos,
8181
}
8282
}
@@ -131,7 +131,7 @@ impl<'a, T: Into<FontDesc<'a>>> From<T> for TextStyle<'a> {
131131
fn from(font: T) -> Self {
132132
Self {
133133
font: font.into(),
134-
color: BLACK.color(),
134+
color: BLACK.to_backend_color(),
135135
pos: text_anchor::Pos::default(),
136136
}
137137
}

0 commit comments

Comments
 (0)