@@ -22,27 +22,45 @@ use std::convert;
2222use std:: hash:: { Hash , Hasher } ;
2323use std:: mem;
2424
25+ /// A gradient, either linear or radial.
2526#[ derive( Clone , PartialEq , Debug ) ]
2627pub struct Gradient {
28+ /// Information specific to the type of gradient (linear or radial).
2729 pub geometry : GradientGeometry ,
2830 stops : Vec < ColorStop > ,
31+ /// What should be rendered upon reaching the end of the color stops.
2932 pub wrap : GradientWrap ,
3033}
3134
35+ /// A color in a gradient. Points in a gradient between two stops interpolate linearly between the
36+ /// stops.
3237#[ derive( Clone , Copy , PartialEq , Debug ) ]
3338pub struct ColorStop {
39+ /// The offset of the color stop, between 0.0 and 1.0 inclusive. The value 0.0 represents the
40+ /// start of the gradient, and 1.0 represents the end.
3441 pub offset : f32 ,
42+ /// The color of the gradient stop.
3543 pub color : ColorU ,
3644}
3745
46+ /// The type of gradient: linear or radial.
3847#[ derive( Clone , PartialEq , Debug ) ]
3948pub enum GradientGeometry {
49+ /// A linear gradient that follows a line.
50+ ///
51+ /// The line is in scene coordinates, not relative to the bounding box of the path.
4052 Linear ( LineSegment2F ) ,
53+ /// A radial gradient that radiates outward from a line connecting two circles (or from one
54+ /// circle).
4155 Radial {
42- /// The line that connects the two circles. It may have zero length for simple radial
43- /// gradients.
56+ /// The line that connects the centers of the two circles. For single-circle radial
57+ /// gradients (the common case), this line has zero length, with start point and endpoint
58+ /// both at the circle's center point.
59+ ///
60+ /// This is in scene coordinates, not relative to the bounding box of the path.
4461 line : LineSegment2F ,
45- /// The radii of the two circles. The first value may be zero.
62+ /// The radii of the two circles. The first value may be zero to start the gradient at the
63+ /// center of the circle.
4664 radii : F32x2 ,
4765 /// Transform from radial gradient space into screen space.
4866 ///
@@ -52,9 +70,13 @@ pub enum GradientGeometry {
5270 }
5371}
5472
73+ /// What should be rendered outside the color stops.
5574#[ derive( Clone , Copy , PartialEq , Debug ) ]
5675pub enum GradientWrap {
76+ /// The area before the gradient is filled with the color of the first stop, and the area after
77+ /// the gradient is filled with the color of the last stop.
5778 Clamp ,
79+ /// The gradient repeats indefinitely.
5880 Repeat ,
5981}
6082
@@ -97,6 +119,9 @@ impl Hash for ColorStop {
97119}
98120
99121impl Gradient {
122+ /// Creates a new linear gradient with the given line.
123+ ///
124+ /// The line is in scene coordinates, not relative to the bounding box of the current path.
100125 #[ inline]
101126 pub fn linear ( line : LineSegment2F ) -> Gradient {
102127 Gradient {
@@ -106,11 +131,19 @@ impl Gradient {
106131 }
107132 }
108133
134+ /// A convenience method equivalent to `Gradient::linear(LineSegment2F::new(from, to))`.
109135 #[ inline]
110136 pub fn linear_from_points ( from : Vector2F , to : Vector2F ) -> Gradient {
111137 Gradient :: linear ( LineSegment2F :: new ( from, to) )
112138 }
113139
140+ /// Creates a new radial gradient from a line connecting the centers of two circles with the
141+ /// given radii, or a point at the center of one circle.
142+ ///
143+ /// To create a radial gradient with a single circle (the common case), pass a `Vector2F`
144+ /// representing the center of the circle for `line`; otherwise, to create a radial gradient
145+ /// with two circles, pass a `LineSegment2F`. To start the gradient at the center of the
146+ /// circle, pass zero for the first radius.
114147 #[ inline]
115148 pub fn radial < L > ( line : L , radii : F32x2 ) -> Gradient where L : RadialGradientLine {
116149 let transform = Transform2F :: default ( ) ;
@@ -121,6 +154,7 @@ impl Gradient {
121154 }
122155 }
123156
157+ /// Adds a new color stop to the radial gradient.
124158 #[ inline]
125159 pub fn add ( & mut self , stop : ColorStop ) {
126160 let index = self . stops . binary_search_by ( |other| {
@@ -129,22 +163,28 @@ impl Gradient {
129163 self . stops . insert ( index, stop) ;
130164 }
131165
132- /// A convenience method to add a color stop.
166+ /// A convenience method equivalent to
167+ /// `gradient.add_color_stop(ColorStop::new(color, offset))`.
133168 #[ inline]
134169 pub fn add_color_stop ( & mut self , color : ColorU , offset : f32 ) {
135170 self . add ( ColorStop :: new ( color, offset) )
136171 }
137172
173+ /// Returns the list of color stops in this gradient.
138174 #[ inline]
139175 pub fn stops ( & self ) -> & [ ColorStop ] {
140176 & self . stops
141177 }
142178
179+ /// Returns a mutable version of the list of color stops in this gradient.
143180 #[ inline]
144181 pub fn stops_mut ( & mut self ) -> & mut [ ColorStop ] {
145182 & mut self . stops
146183 }
147184
185+ /// Returns the value of the gradient at offset `t`, which will be clamped between 0.0 and 1.0.
186+ ///
187+ /// FIXME(pcwalton): This should probably take `wrap` into account…
148188 pub fn sample ( & self , mut t : f32 ) -> ColorU {
149189 if self . stops . is_empty ( ) {
150190 return ColorU :: transparent_black ( ) ;
@@ -170,16 +210,23 @@ impl Gradient {
170210 lower_stop. color . to_f32 ( ) . lerp ( upper_stop. color . to_f32 ( ) , ratio) . to_u8 ( )
171211 }
172212
213+ /// Returns true if all colors of all stops in this gradient are opaque (alpha is 1.0).
173214 #[ inline]
174215 pub fn is_opaque ( & self ) -> bool {
175216 self . stops . iter ( ) . all ( |stop| stop. color . is_opaque ( ) )
176217 }
177218
219+ /// Returns true if all colors of all stops in this gradient are fully transparent (alpha is
220+ /// 0.0).
178221 #[ inline]
179222 pub fn is_fully_transparent ( & self ) -> bool {
180223 self . stops . iter ( ) . all ( |stop| stop. color . is_fully_transparent ( ) )
181224 }
182225
226+ /// Applies the given affine transform to this gradient.
227+ ///
228+ /// FIXME(pcwalton): This isn't correct for radial gradients, as transforms can transform the
229+ /// circles into ellipses…
183230 pub fn apply_transform ( & mut self , new_transform : Transform2F ) {
184231 if new_transform. is_identity ( ) {
185232 return ;
@@ -195,13 +242,16 @@ impl Gradient {
195242}
196243
197244impl ColorStop {
245+ /// Creates a new color stop from a color and offset between 0.0 and 1.0 inclusive.
198246 #[ inline]
199247 pub fn new ( color : ColorU , offset : f32 ) -> ColorStop {
200248 ColorStop { color, offset }
201249 }
202250}
203251
252+ /// Allows `Gradient::radial` to be called with either a `LineSegment2F` or a `Vector2F`.
204253pub trait RadialGradientLine {
254+ /// Represents this value as a line.
205255 fn to_line ( self ) -> LineSegment2F ;
206256}
207257
0 commit comments