Skip to content

Commit bd479fd

Browse files
committed
Allow passing colors by value and reference
This fixes a bit of a usability issue with the API of plotters where certain functions only take references to colors, even if an owned value is easily available. Especially if the same builder pattern takes multiple colors, it's extremely annoying if the color cannot be initialized once and then copied around.
1 parent 8e120a1 commit bd479fd

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/style/color.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ pub trait Color: BackendStyle {
5050
}
5151
}
5252

53+
impl<T: Color> Color for &'_ T {}
54+
5355
/// The RGBA representation of the color, Plotters use RGBA as the internal representation
5456
/// of color
55-
#[derive(Clone, PartialEq, Debug)]
57+
#[derive(Copy, Clone, PartialEq, Debug)]
5658
pub struct RGBAColor(pub(crate) u8, pub(crate) u8, pub(crate) u8, pub(crate) f64);
5759

5860
impl BackendStyle for RGBAColor {
@@ -89,7 +91,7 @@ impl<P: Palette> BackendStyle for PaletteColor<P> {
8991
impl<P: Palette> Color for PaletteColor<P> {}
9092

9193
/// The color described by its RGB value
92-
#[derive(Debug)]
94+
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
9395
pub struct RGBColor(pub u8, pub u8, pub u8);
9496

9597
impl BackendStyle for RGBColor {

src/style/shape.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ impl ShapeStyle {
2828
}
2929
}
3030

31-
impl<'a, T: Color> From<&'a T> for ShapeStyle {
32-
fn from(f: &'a T) -> Self {
31+
impl<T: Color> From<T> for ShapeStyle {
32+
fn from(f: T) -> Self {
3333
ShapeStyle {
3434
color: f.to_rgba(),
3535
filled: false,

0 commit comments

Comments
 (0)