/rust/registry/src/index.crates.io-6f17d22bba15001f/plotters-0.3.7/src/style/shape.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use super::color::{Color, RGBAColor}; |
2 | | use plotters_backend::{BackendColor, BackendStyle}; |
3 | | |
4 | | /// Style for any shape |
5 | | #[derive(Copy, Clone, Debug, PartialEq)] |
6 | | pub 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 | | |
15 | | impl 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 | |  |
38 | | */ |
39 | 0 | pub fn filled(&self) -> Self { |
40 | 0 | Self { |
41 | 0 | color: self.color.to_rgba(), |
42 | 0 | filled: true, |
43 | 0 | stroke_width: self.stroke_width, |
44 | 0 | } |
45 | 0 | } |
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 | |  |
69 | | */ |
70 | 0 | pub fn stroke_width(&self, width: u32) -> Self { |
71 | 0 | Self { |
72 | 0 | color: self.color.to_rgba(), |
73 | 0 | filled: self.filled, |
74 | 0 | stroke_width: width, |
75 | 0 | } |
76 | 0 | } |
77 | | } |
78 | | |
79 | | impl<T: Color> From<T> for ShapeStyle { |
80 | 0 | fn from(f: T) -> Self { |
81 | 0 | ShapeStyle { |
82 | 0 | color: f.to_rgba(), |
83 | 0 | filled: false, |
84 | 0 | stroke_width: 1, |
85 | 0 | } |
86 | 0 | } Unexecuted instantiation: <plotters::style::shape::ShapeStyle as core::convert::From<plotters::style::color::RGBColor>>::from Unexecuted instantiation: <plotters::style::shape::ShapeStyle as core::convert::From<plotters::style::color::RGBAColor>>::from Unexecuted instantiation: <plotters::style::shape::ShapeStyle as core::convert::From<&plotters::style::color::RGBColor>>::from Unexecuted instantiation: <plotters::style::shape::ShapeStyle as core::convert::From<&plotters::style::color::RGBAColor>>::from Unexecuted instantiation: <plotters::style::shape::ShapeStyle as core::convert::From<_>>::from |
87 | | } |
88 | | |
89 | | impl BackendStyle for ShapeStyle { |
90 | | /// Returns the color as interpreted by the backend. |
91 | 0 | fn color(&self) -> BackendColor { |
92 | 0 | self.color.to_backend_color() |
93 | 0 | } |
94 | | /// Returns the stroke width. |
95 | 0 | fn stroke_width(&self) -> u32 { |
96 | 0 | self.stroke_width |
97 | 0 | } |
98 | | } |