@@ -10,48 +10,66 @@ use color::{AlphaColor, ColorSpace, DynamicColor, OpaqueColor, Srgb};
10
10
/// Describes the color content of a filled or stroked shape.
11
11
///
12
12
/// See also [`BrushRef`] which can be used to avoid allocations.
13
- #[ derive( Clone , Debug , PartialEq ) ]
13
+ #[ derive( Copy , Clone , Debug , PartialEq ) ]
14
14
#[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
15
- pub enum Brush {
15
+ pub enum Brush < I = ImageBrush , G = Gradient > {
16
16
/// Solid color brush.
17
17
Solid ( AlphaColor < Srgb > ) ,
18
18
/// Gradient brush.
19
- Gradient ( Gradient ) ,
19
+ Gradient ( G ) ,
20
20
/// Image brush.
21
- Image ( ImageBrush ) ,
21
+ Image ( I ) ,
22
22
}
23
23
24
- impl < CS : ColorSpace > From < AlphaColor < CS > > for Brush {
24
+ impl < CS : ColorSpace , I , G > From < AlphaColor < CS > > for Brush < I , G > {
25
25
fn from ( c : AlphaColor < CS > ) -> Self {
26
26
Self :: Solid ( c. convert ( ) )
27
27
}
28
28
}
29
29
30
- impl From < DynamicColor > for Brush {
30
+ impl < I , G > From < DynamicColor > for Brush < I , G > {
31
31
fn from ( c : DynamicColor ) -> Self {
32
32
Self :: Solid ( c. to_alpha_color :: < Srgb > ( ) )
33
33
}
34
34
}
35
35
36
- impl < CS : ColorSpace > From < OpaqueColor < CS > > for Brush {
36
+ impl < CS : ColorSpace , I , G > From < OpaqueColor < CS > > for Brush < I , G > {
37
37
fn from ( c : OpaqueColor < CS > ) -> Self {
38
38
Self :: Solid ( c. with_alpha ( 1. ) . convert ( ) )
39
39
}
40
40
}
41
41
42
- impl From < Gradient > for Brush {
42
+ impl < CS : ColorSpace , I , G > From < & AlphaColor < CS > > for Brush < I , G > {
43
+ fn from ( c : & AlphaColor < CS > ) -> Self {
44
+ Self :: Solid ( ( * c) . convert ( ) )
45
+ }
46
+ }
47
+
48
+ impl < I , G > From < & DynamicColor > for Brush < I , G > {
49
+ fn from ( c : & DynamicColor ) -> Self {
50
+ Self :: Solid ( ( * c) . to_alpha_color :: < Srgb > ( ) )
51
+ }
52
+ }
53
+
54
+ impl < CS : ColorSpace , I , G > From < & OpaqueColor < CS > > for Brush < I , G > {
55
+ fn from ( c : & OpaqueColor < CS > ) -> Self {
56
+ Self :: Solid ( ( * c) . with_alpha ( 1. ) . convert ( ) )
57
+ }
58
+ }
59
+
60
+ impl < I > From < Gradient > for Brush < I , Gradient > {
43
61
fn from ( g : Gradient ) -> Self {
44
62
Self :: Gradient ( g)
45
63
}
46
64
}
47
65
48
- impl From < ImageBrush > for Brush {
66
+ impl < G > From < ImageBrush > for Brush < ImageBrush , G > {
49
67
fn from ( value : ImageBrush ) -> Self {
50
68
Self :: Image ( value)
51
69
}
52
70
}
53
71
54
- impl Default for Brush {
72
+ impl < I , G > Default for Brush < I , G > {
55
73
fn default ( ) -> Self {
56
74
Self :: Solid ( AlphaColor :: < Srgb > :: TRANSPARENT )
57
75
}
@@ -97,15 +115,7 @@ impl Brush {
97
115
/// This is useful for methods that would like to accept brushes by reference. Defining
98
116
/// the type as `impl<Into<BrushRef>>` allows accepting types like `&LinearGradient`
99
117
/// directly without cloning or allocating.
100
- #[ derive( Copy , Clone , Debug , PartialEq ) ]
101
- pub enum BrushRef < ' a > {
102
- /// Solid color brush.
103
- Solid ( AlphaColor < Srgb > ) ,
104
- /// Gradient brush.
105
- Gradient ( & ' a Gradient ) ,
106
- /// Image brush.
107
- Image ( ImageBrushRef < ' a > ) ,
108
- }
118
+ pub type BrushRef < ' a > = Brush < ImageBrushRef < ' a > , & ' a Gradient > ;
109
119
110
120
impl BrushRef < ' _ > {
111
121
/// Converts the reference to an owned brush.
@@ -119,42 +129,6 @@ impl BrushRef<'_> {
119
129
}
120
130
}
121
131
122
- impl < CS : ColorSpace > From < AlphaColor < CS > > for BrushRef < ' _ > {
123
- fn from ( color : AlphaColor < CS > ) -> Self {
124
- Self :: Solid ( color. convert ( ) )
125
- }
126
- }
127
-
128
- impl From < DynamicColor > for BrushRef < ' _ > {
129
- fn from ( color : DynamicColor ) -> Self {
130
- Self :: Solid ( color. to_alpha_color :: < Srgb > ( ) )
131
- }
132
- }
133
-
134
- impl < CS : ColorSpace > From < OpaqueColor < CS > > for BrushRef < ' _ > {
135
- fn from ( color : OpaqueColor < CS > ) -> Self {
136
- Self :: Solid ( color. with_alpha ( 1. ) . convert ( ) )
137
- }
138
- }
139
-
140
- impl < ' a , CS : ColorSpace > From < & ' a AlphaColor < CS > > for BrushRef < ' _ > {
141
- fn from ( color : & ' a AlphaColor < CS > ) -> Self {
142
- Self :: Solid ( ( * color) . convert ( ) )
143
- }
144
- }
145
-
146
- impl < ' a > From < & ' a DynamicColor > for BrushRef < ' _ > {
147
- fn from ( color : & ' a DynamicColor ) -> Self {
148
- Self :: Solid ( ( * color) . to_alpha_color :: < Srgb > ( ) )
149
- }
150
- }
151
-
152
- impl < ' a , CS : ColorSpace > From < & ' a OpaqueColor < CS > > for BrushRef < ' _ > {
153
- fn from ( color : & ' a OpaqueColor < CS > ) -> Self {
154
- Self :: Solid ( ( * color) . with_alpha ( 1. ) . convert ( ) )
155
- }
156
- }
157
-
158
132
impl < ' a > From < & ' a Gradient > for BrushRef < ' a > {
159
133
fn from ( gradient : & ' a Gradient ) -> Self {
160
134
Self :: Gradient ( gradient)
0 commit comments