plotters/style/
shape.rs

1use super::color::{Color, RGBAColor};
2use plotters_backend::{BackendColor, BackendStyle};
3
4/// Style for any shape
5#[derive(Copy, Clone, Debug, PartialEq)]
6pub struct ShapeStyle {
7    /// Specification of the color.
8    pub color: RGBAColor,
9    /// Whether the style is filled with color.
10    pub filled: bool,
11    /// Stroke width.
12    pub stroke_width: u32,
13}
14
15impl ShapeStyle {
16    /**
17    Returns a filled style with the same color and stroke width.
18
19    # Example
20
21    ```
22    use plotters::prelude::*;
23    let original_style = ShapeStyle {
24        color: BLUE.mix(0.6),
25        filled: false,
26        stroke_width: 2,
27    };
28    let filled_style = original_style.filled();
29    let drawing_area = SVGBackend::new("shape_style_filled.svg", (400, 200)).into_drawing_area();
30    drawing_area.fill(&WHITE).unwrap();
31    drawing_area.draw(&Circle::new((150, 100), 90, original_style));
32    drawing_area.draw(&Circle::new((250, 100), 90, filled_style));
33    ```
34
35    The result is a figure with two circles, one of them filled:
36
37    ![](https://cdn.jsdelivr.net/gh/facorread/plotters-doc-data@b0b94d5/apidoc/shape_style_filled.svg)
38    */
39    pub fn filled(&self) -> Self {
40        Self {
41            color: self.color.to_rgba(),
42            filled: true,
43            stroke_width: self.stroke_width,
44        }
45    }
46
47    /**
48    Returns a new style with the same color and the specified stroke width.
49
50    # Example
51
52    ```
53    use plotters::prelude::*;
54    let original_style = ShapeStyle {
55        color: BLUE.mix(0.6),
56        filled: false,
57        stroke_width: 2,
58    };
59    let new_style = original_style.stroke_width(5);
60    let drawing_area = SVGBackend::new("shape_style_stroke_width.svg", (400, 200)).into_drawing_area();
61    drawing_area.fill(&WHITE).unwrap();
62    drawing_area.draw(&Circle::new((150, 100), 90, original_style));
63    drawing_area.draw(&Circle::new((250, 100), 90, new_style));
64    ```
65
66    The result is a figure with two circles, one of them thicker than the other:
67
68    ![](https://cdn.jsdelivr.net/gh/facorread/plotters-doc-data@b0b94d5/apidoc/shape_style_stroke_width.svg)
69    */
70    pub fn stroke_width(&self, width: u32) -> Self {
71        Self {
72            color: self.color.to_rgba(),
73            filled: self.filled,
74            stroke_width: width,
75        }
76    }
77}
78
79impl<T: Color> From<T> for ShapeStyle {
80    fn from(f: T) -> Self {
81        ShapeStyle {
82            color: f.to_rgba(),
83            filled: false,
84            stroke_width: 1,
85        }
86    }
87}
88
89impl BackendStyle for ShapeStyle {
90    /// Returns the color as interpreted by the backend.
91    fn color(&self) -> BackendColor {
92        self.color.to_backend_color()
93    }
94    /// Returns the stroke width.
95    fn stroke_width(&self) -> u32 {
96        self.stroke_width
97    }
98}