8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
+ //! Defines how a path is to be filled.
12
+
11
13
use crate :: allocator:: { AllocationMode , TextureAllocator } ;
12
14
use crate :: gpu_data:: { ColorCombineMode , RenderCommand , TextureLocation , TextureMetadataEntry } ;
13
15
use crate :: gpu_data:: { TexturePageDescriptor , TexturePageId , TileBatchTexture } ;
@@ -48,34 +50,46 @@ struct RenderTargetData {
48
50
metadata : RenderTargetMetadata ,
49
51
}
50
52
53
+ /// Defines how a path is to be filled: with a solid color, gradient, or pattern.
51
54
#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
52
55
pub struct Paint {
53
56
base_color : ColorU ,
54
57
overlay : Option < PaintOverlay > ,
55
58
}
56
59
60
+ /// What is to be overlaid on top of a base color.
61
+ ///
62
+ /// An overlay is a gradient or a pattern, plus a composite operation which determines how the
63
+ /// gradient or pattern is to be combined with the base color.
57
64
#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
58
65
pub struct PaintOverlay {
59
66
composite_op : PaintCompositeOp ,
60
67
contents : PaintContents ,
61
68
}
62
69
70
+ /// The contents of an overlay: either a gradient or a pattern.
63
71
#[ derive( Clone , PartialEq , Eq , Hash ) ]
64
- pub enum PaintContents {
72
+ pub ( crate ) enum PaintContents {
73
+ /// A gradient, either linear or radial.
65
74
Gradient ( Gradient ) ,
75
+ /// A raster image pattern.
66
76
Pattern ( Pattern ) ,
67
77
}
68
78
79
+ /// The ID of a paint, unique to a scene.
69
80
#[ derive( Clone , Copy , PartialEq , Eq , Hash , Debug ) ]
70
81
pub struct PaintId ( pub u16 ) ;
71
82
83
+ /// The ID of a gradient, unique to a scene.
72
84
#[ derive( Clone , Copy , PartialEq , Eq , Hash , Debug ) ]
73
85
pub struct GradientId ( pub u32 ) ;
74
86
75
- /// How a paint is to be composited over a base color, or vice versa .
87
+ /// How an overlay is to be composited over a base color.
76
88
#[ derive( Clone , Copy , PartialEq , Eq , Hash , Debug ) ]
77
89
pub enum PaintCompositeOp {
90
+ /// The source that overlaps the destination, replaces the destination.
78
91
SrcIn ,
92
+ /// Destination which overlaps the source, replaces the source.
79
93
DestIn ,
80
94
}
81
95
@@ -102,11 +116,13 @@ impl Palette {
102
116
}
103
117
104
118
impl Paint {
119
+ /// Creates a simple paint from a single base color.
105
120
#[ inline]
106
121
pub fn from_color ( color : ColorU ) -> Paint {
107
122
Paint { base_color : color, overlay : None }
108
123
}
109
124
125
+ /// Creates a paint from a gradient.
110
126
#[ inline]
111
127
pub fn from_gradient ( gradient : Gradient ) -> Paint {
112
128
Paint {
@@ -118,6 +134,7 @@ impl Paint {
118
134
}
119
135
}
120
136
137
+ /// Creates a paint from a raster pattern.
121
138
#[ inline]
122
139
pub fn from_pattern ( pattern : Pattern ) -> Paint {
123
140
Paint {
@@ -129,16 +146,21 @@ impl Paint {
129
146
}
130
147
}
131
148
149
+ /// A convenience function to create a solid black paint.
132
150
#[ inline]
133
151
pub fn black ( ) -> Paint {
134
152
Paint :: from_color ( ColorU :: black ( ) )
135
153
}
136
154
155
+ /// A convenience function to create a transparent paint with all channels set to zero.
137
156
#[ inline]
138
157
pub fn transparent_black ( ) -> Paint {
139
158
Paint :: from_color ( ColorU :: transparent_black ( ) )
140
159
}
141
160
161
+ /// Returns true if this paint is obviously opaque, via a quick check.
162
+ ///
163
+ /// Even if the paint is opaque, this function might return false.
142
164
pub fn is_opaque ( & self ) -> bool {
143
165
if !self . base_color . is_opaque ( ) {
144
166
return false ;
@@ -155,6 +177,9 @@ impl Paint {
155
177
}
156
178
}
157
179
180
+ /// Returns true if this paint is fully transparent, via a quick check.
181
+ ///
182
+ /// Even if the paint is fully transparent, this function might return false.
158
183
pub fn is_fully_transparent ( & self ) -> bool {
159
184
if !self . base_color . is_fully_transparent ( ) {
160
185
return false ;
@@ -171,11 +196,15 @@ impl Paint {
171
196
}
172
197
}
173
198
199
+ /// Returns true if this paint represents a solid color.
174
200
#[ inline]
175
201
pub fn is_color ( & self ) -> bool {
176
202
self . overlay . is_none ( )
177
203
}
178
204
205
+ /// Applies an affine transform to this paint.
206
+ ///
207
+ /// This has no effect if this paint is a solid color.
179
208
pub fn apply_transform ( & mut self , transform : & Transform2F ) {
180
209
if transform. is_identity ( ) {
181
210
return ;
@@ -189,26 +218,36 @@ impl Paint {
189
218
}
190
219
}
191
220
221
+ /// Returns the *base color* of this paint.
222
+ ///
223
+ /// The base color is the color that goes underneath the gradient or pattern, if there is one.
192
224
#[ inline]
193
225
pub fn base_color ( & self ) -> ColorU {
194
226
self . base_color
195
227
}
196
228
229
+ /// Changes the *base color* of this paint.
230
+ ///
231
+ /// The base color is the color that goes underneath the gradient or pattern, if there is one.
197
232
#[ inline]
198
233
pub fn set_base_color ( & mut self , new_base_color : ColorU ) {
199
234
self . base_color = new_base_color;
200
235
}
201
236
237
+ /// Returns the paint overlay, which is the portion of the paint on top of the base color.
202
238
#[ inline]
203
239
pub fn overlay ( & self ) -> & Option < PaintOverlay > {
204
240
& self . overlay
205
241
}
206
242
243
+ /// Returns a mutable reference to the paint overlay, which is the portion of the paint on top
244
+ /// of the base color.
207
245
#[ inline]
208
246
pub fn overlay_mut ( & mut self ) -> & mut Option < PaintOverlay > {
209
247
& mut self . overlay
210
248
}
211
249
250
+ /// Returns the pattern, if this paint represents one.
212
251
#[ inline]
213
252
pub fn pattern ( & self ) -> Option < & Pattern > {
214
253
match self . overlay {
@@ -222,6 +261,7 @@ impl Paint {
222
261
}
223
262
}
224
263
264
+ /// Returns a mutable reference to the pattern, if this paint represents one.
225
265
#[ inline]
226
266
pub fn pattern_mut ( & mut self ) -> Option < & mut Pattern > {
227
267
match self . overlay {
@@ -235,6 +275,7 @@ impl Paint {
235
275
}
236
276
}
237
277
278
+ /// Returns the gradient, if this paint represents one.
238
279
#[ inline]
239
280
pub fn gradient ( & self ) -> Option < & Gradient > {
240
281
match self . overlay {
@@ -251,18 +292,22 @@ impl Paint {
251
292
252
293
impl PaintOverlay {
253
294
#[ inline]
254
- pub fn contents ( & self ) -> & PaintContents {
295
+ pub ( crate ) fn contents ( & self ) -> & PaintContents {
255
296
& self . contents
256
297
}
257
298
299
+ /// Returns the composite operation, which defines how the overlay is to be composited on top
300
+ /// of the base color.
258
301
#[ inline]
259
302
pub fn composite_op ( & self ) -> PaintCompositeOp {
260
303
self . composite_op
261
304
}
262
305
306
+ /// Changes the composite operation, which defines how the overlay is to be composited on top
307
+ /// of the base color.
263
308
#[ inline]
264
309
pub fn set_composite_op ( & mut self , new_composite_op : PaintCompositeOp ) {
265
- self . composite_op = new_composite_op
310
+ self . composite_op = new_composite_op;
266
311
}
267
312
}
268
313
0 commit comments