drawing_api/common/display_list/paint/
paint.rs

1use crate::{
2    smart_pointers::{OptRef, Owned},
3    Color,
4};
5
6use super::{
7    BlendMode, ColorFilter, ColorSource, DrawStyle, ImageFilter, MaskFilter, StrokeCap, StrokeJoin,
8};
9
10pub trait Paint: Clone + Default + 'static {
11    type ColorSourceFragment: crate::ColorSourceFragment;
12    type ImageFilterFragment: crate::ImageFilterFragment;
13    type Texture: crate::Texture;
14
15    fn color(color: impl Into<Color>) -> Self {
16        Self::default().with_color(color)
17    }
18
19    fn stroke_color(color: impl Into<Color>, stroke_width: f32) -> Self {
20        Self::default()
21            .with_color(color)
22            .with_draw_style(DrawStyle::Stroke)
23            .with_stroke_width(stroke_width)
24    }
25
26    fn color_source(color_source: ColorSource<Self::Texture, Self::ColorSourceFragment>) -> Self {
27        Self::default().with_color_source(color_source)
28    }
29
30    /// Sets the paint color for stroking or filling.
31    fn set_color(&mut self, color: impl Into<Color>);
32
33    fn with_color(mut self, color: impl Into<Color>) -> Self {
34        self.set_color(color);
35        self
36    }
37
38    /// Sets the paint blend mode.
39    fn set_blend_mode(&mut self, blend_mode: BlendMode);
40
41    fn with_blend_mode(mut self, blend_mode: BlendMode) -> Self {
42        self.set_blend_mode(blend_mode);
43        self
44    }
45
46    /// Set the paint draw style.
47    fn set_draw_style(&mut self, draw_style: DrawStyle);
48
49    fn with_draw_style(mut self, draw_style: DrawStyle) -> Self {
50        self.set_draw_style(draw_style);
51        self
52    }
53
54    /// Sets how strokes rendered using this paint are capped.
55    fn set_stroke_cap(&mut self, cap: StrokeCap);
56
57    fn with_stroke_cap(mut self, cap: StrokeCap) -> Self {
58        self.set_stroke_cap(cap);
59        self
60    }
61
62    /// Sets how strokes rendered using this paint are joined.
63    fn set_stroke_join(&mut self, join: StrokeJoin);
64
65    fn with_stroke_join(mut self, join: StrokeJoin) -> Self {
66        self.set_stroke_join(join);
67        self
68    }
69
70    /// Sets the width of the strokes rendered using this paint.
71    fn set_stroke_width(&mut self, width: f32);
72
73    fn with_stroke_width(mut self, width: f32) -> Self {
74        self.set_stroke_width(width);
75        self
76    }
77
78    /// Sets the miter limit of the strokes rendered using this paint.
79    fn set_stroke_miter(&mut self, miter: f32);
80
81    fn with_stroke_miter(mut self, miter: f32) -> Self {
82        self.set_stroke_miter(miter);
83        self
84    }
85
86    /// Sets the color source of the paint.
87    fn set_color_source(
88        &mut self,
89        color_source: ColorSource<Self::Texture, Self::ColorSourceFragment>,
90    );
91
92    fn with_color_source(
93        mut self,
94        color_source: ColorSource<Self::Texture, Self::ColorSourceFragment>,
95    ) -> Self {
96        self.set_color_source(color_source);
97        self
98    }
99
100    /// Sets the color filter of the paint.
101    fn set_color_filter(&mut self, color_filter: ColorFilter);
102
103    fn with_color_filter(mut self, color_filter: ColorFilter) -> Self {
104        self.set_color_filter(color_filter);
105        self
106    }
107
108    /// Sets the image filter of a paint.
109    ///
110    /// Image filters are functions that are applied to regions of a texture to produce a single color.
111    fn set_image_filter(&mut self, image_filter: ImageFilter<Self::ImageFilterFragment>);
112
113    fn with_image_filter(mut self, image_filter: ImageFilter<Self::ImageFilterFragment>) -> Self {
114        self.set_image_filter(image_filter);
115        self
116    }
117
118    /// Sets the mask filter of a paint.
119    ///
120    /// Mask filters are functions that are applied over a shape after it has been drawn but before it has been blended into the final image.
121    fn set_mask_filter(&mut self, mask_filter: MaskFilter);
122
123    fn with_mask_filter(mut self, mask_filter: MaskFilter) -> Self {
124        self.set_mask_filter(mask_filter);
125        self
126    }
127}
128
129impl<P: Paint> From<P> for Owned<P> {
130    fn from(value: P) -> Self {
131        Owned(value)
132    }
133}
134
135impl<P: Paint> From<Color> for Owned<P> {
136    fn from(value: Color) -> Self {
137        Owned(P::color(value))
138    }
139}
140
141impl<P: Paint> From<&Color> for Owned<P> {
142    fn from(value: &Color) -> Self {
143        Owned(P::color(value.clone()))
144    }
145}
146
147impl<P: Paint> From<(f32, f32, f32)> for Owned<P> {
148    fn from(value: (f32, f32, f32)) -> Self {
149        Owned(P::color(value))
150    }
151}
152
153impl<P: Paint> From<[f32; 3]> for Owned<P> {
154    fn from(value: [f32; 3]) -> Self {
155        Owned(P::color(value))
156    }
157}
158
159impl<P: Paint> From<(f32, f32, f32, f32)> for Owned<P> {
160    fn from(value: (f32, f32, f32, f32)) -> Self {
161        Owned(P::color(value))
162    }
163}
164
165impl<P: Paint> From<[f32; 4]> for Owned<P> {
166    fn from(value: [f32; 4]) -> Self {
167        Owned(P::color(value))
168    }
169}
170
171impl<P: Paint> From<u32> for Owned<P> {
172    fn from(value: u32) -> Self {
173        Owned(P::color(value))
174    }
175}
176
177impl<P: Paint> From<&str> for Owned<P> {
178    fn from(value: &str) -> Self {
179        Owned(P::color(value))
180    }
181}
182
183impl<P: Paint> From<ColorSource<P::Texture, P::ColorSourceFragment>> for Owned<P> {
184    fn from(value: ColorSource<P::Texture, P::ColorSourceFragment>) -> Self {
185        Owned(P::color_source(value))
186    }
187}
188
189impl<'a, P: Paint> From<&'a P> for OptRef<'a, P> {
190    fn from(value: &'a P) -> Self {
191        OptRef::Borrowed(value)
192    }
193}
194
195impl<'a, P: Paint> From<P> for OptRef<'a, P> {
196    fn from(value: P) -> Self {
197        OptRef::Owned(value)
198    }
199}
200
201impl<'a, P: Paint> From<Color> for OptRef<'a, P> {
202    fn from(value: Color) -> Self {
203        OptRef::Owned(P::color(value))
204    }
205}
206
207impl<'a, P: Paint> From<&Color> for OptRef<'a, P> {
208    fn from(value: &Color) -> Self {
209        OptRef::Owned(P::color(value.clone()))
210    }
211}
212
213impl<'a, P: Paint> From<(f32, f32, f32)> for OptRef<'a, P> {
214    fn from(value: (f32, f32, f32)) -> Self {
215        OptRef::Owned(P::color(value))
216    }
217}
218
219impl<'a, P: Paint> From<[f32; 3]> for OptRef<'a, P> {
220    fn from(value: [f32; 3]) -> Self {
221        OptRef::Owned(P::color(value))
222    }
223}
224
225impl<'a, P: Paint> From<(f32, f32, f32, f32)> for OptRef<'a, P> {
226    fn from(value: (f32, f32, f32, f32)) -> Self {
227        OptRef::Owned(P::color(value))
228    }
229}
230
231impl<'a, P: Paint> From<[f32; 4]> for OptRef<'a, P> {
232    fn from(value: [f32; 4]) -> Self {
233        OptRef::Owned(P::color(value))
234    }
235}
236
237impl<'a, P: Paint> From<u32> for OptRef<'a, P> {
238    fn from(value: u32) -> Self {
239        OptRef::Owned(P::color(value))
240    }
241}
242
243impl<'a, P: Paint> From<&str> for OptRef<'a, P> {
244    fn from(value: &str) -> Self {
245        OptRef::Owned(P::color(value))
246    }
247}
248
249impl<'a, P: Paint> From<ColorSource<P::Texture, P::ColorSourceFragment>> for OptRef<'a, P> {
250    fn from(value: ColorSource<P::Texture, P::ColorSourceFragment>) -> Self {
251        OptRef::Owned(P::color_source(value))
252    }
253}