drawing_api/common/display_list/paint/
paint.rs

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