1use crate::color::Color;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum PaintStyle {
10 Fill,
12 Stroke,
14 FillAndStroke,
16}
17
18#[derive(Debug, Clone, PartialEq)]
20pub enum DashStyle {
21 Solid,
23 ShortDash,
25 LongDash,
27 Dot,
29 DashDot,
31 DashDotDot,
33 Custom(Vec<f32>),
35}
36
37impl DashStyle {
38 pub fn pattern(&self) -> Option<&[f32]> {
40 match self {
41 DashStyle::Solid => None,
42 DashStyle::ShortDash => Some(&[10.0, 5.0]),
43 DashStyle::LongDash => Some(&[20.0, 10.0]),
44 DashStyle::Dot => Some(&[3.0, 5.0]),
45 DashStyle::DashDot => Some(&[15.0, 5.0, 3.0, 5.0]),
46 DashStyle::DashDotDot => Some(&[15.0, 5.0, 3.0, 5.0, 3.0, 5.0]),
47 DashStyle::Custom(pattern) => Some(pattern.as_slice()),
48 }
49 }
50
51 pub fn is_solid(&self) -> bool {
53 matches!(self, DashStyle::Solid)
54 }
55}
56
57impl Default for DashStyle {
58 fn default() -> Self {
59 DashStyle::Solid
60 }
61}
62
63#[derive(Debug, Clone)]
65pub struct Paint {
66 color: Color,
68 stroke_width: f32,
70 style: PaintStyle,
72 anti_alias: bool,
74 dash_style: DashStyle,
76}
77
78impl Paint {
79 pub fn new() -> Self {
81 Self {
82 color: Color::BLACK,
83 stroke_width: 1.0,
84 style: PaintStyle::Fill,
85 anti_alias: true,
86 dash_style: DashStyle::Solid,
87 }
88 }
89
90 pub fn set_color(&mut self, color: Color) {
92 self.color = color;
93 }
94
95 pub fn color(&self) -> Color {
97 self.color
98 }
99
100 pub fn set_stroke_width(&mut self, width: f32) {
102 self.stroke_width = width.max(0.0);
103 }
104
105 pub fn stroke_width(&self) -> f32 {
107 self.stroke_width
108 }
109
110 pub fn set_style(&mut self, style: PaintStyle) {
112 self.style = style;
113 }
114
115 pub fn style(&self) -> PaintStyle {
117 self.style
118 }
119
120 pub fn set_anti_alias(&mut self, anti_alias: bool) {
122 self.anti_alias = anti_alias;
123 }
124
125 pub fn anti_alias(&self) -> bool {
127 self.anti_alias
128 }
129
130 pub fn fill(color: Color) -> Self {
132 let mut paint = Self::new();
133 paint.set_color(color);
134 paint.set_style(PaintStyle::Fill);
135 paint
136 }
137
138 pub fn stroke(color: Color, width: f32) -> Self {
140 let mut paint = Self::new();
141 paint.set_color(color);
142 paint.set_stroke_width(width);
143 paint.set_style(PaintStyle::Stroke);
144 paint
145 }
146
147 pub fn set_dash_style(&mut self, dash_style: DashStyle) {
149 self.dash_style = dash_style;
150 }
151
152 pub fn dash_style(&self) -> &DashStyle {
154 &self.dash_style
155 }
156
157 pub fn set_dash_pattern(&mut self, pattern: Vec<f32>) {
159 self.dash_style = DashStyle::Custom(pattern);
160 }
161
162 pub fn dash_pattern(&self) -> Option<&[f32]> {
164 self.dash_style.pattern()
165 }
166
167 pub fn dashed_stroke(color: Color, width: f32, dash_style: DashStyle) -> Self {
169 let mut paint = Self::stroke(color, width);
170 paint.set_dash_style(dash_style);
171 paint
172 }
173}
174
175impl Default for Paint {
176 fn default() -> Self {
177 Self::new()
178 }
179}
180
181#[derive(Debug, Clone, Copy, PartialEq, Eq)]
183pub enum StrokeCap {
184 Butt,
186 Round,
188 Square,
190}
191
192#[derive(Debug, Clone, Copy, PartialEq, Eq)]
194pub enum StrokeJoin {
195 Miter,
197 Round,
199 Bevel,
201}
202
203#[derive(Debug, Clone, Copy, PartialEq, Eq)]
207pub enum FilterMode {
208 Nearest,
210 Linear,
212}
213
214impl Default for FilterMode {
215 fn default() -> Self {
216 FilterMode::Linear
217 }
218}