@@ -205,22 +205,17 @@ impl ToCss for RGBA {
205
205
#[ derive( Clone , Copy , PartialEq , Debug ) ]
206
206
pub struct Hsl {
207
207
/// The hue component.
208
- pub hue : Option < f32 > ,
208
+ pub hue : f32 ,
209
209
/// The saturation component.
210
- pub saturation : Option < f32 > ,
210
+ pub saturation : f32 ,
211
211
/// The lightness component.
212
- pub lightness : Option < f32 > ,
212
+ pub lightness : f32 ,
213
213
/// The alpha component.
214
- pub alpha : Option < f32 > ,
214
+ pub alpha : f32 ,
215
215
}
216
216
217
217
impl Hsl {
218
- pub fn new (
219
- hue : Option < f32 > ,
220
- saturation : Option < f32 > ,
221
- lightness : Option < f32 > ,
222
- alpha : Option < f32 > ,
223
- ) -> Self {
218
+ pub fn new ( hue : f32 , saturation : f32 , lightness : f32 , alpha : f32 ) -> Self {
224
219
Self {
225
220
hue,
226
221
saturation,
@@ -236,13 +231,9 @@ impl ToCss for Hsl {
236
231
W : fmt:: Write ,
237
232
{
238
233
// HSL serializes to RGB, so we have to convert it.
239
- let ( red, green, blue) = hsl_to_rgb (
240
- self . hue . unwrap_or ( 0.0 ) / 360.0 ,
241
- self . saturation . unwrap_or ( 0.0 ) ,
242
- self . lightness . unwrap_or ( 0.0 ) ,
243
- ) ;
234
+ let ( red, green, blue) = hsl_to_rgb ( self . hue / 360.0 , self . saturation , self . lightness ) ;
244
235
245
- RGBA :: from_floats ( red, green, blue, self . alpha . unwrap_or ( 0.0 ) ) . to_css ( dest)
236
+ RGBA :: from_floats ( red, green, blue, self . alpha ) . to_css ( dest)
246
237
}
247
238
}
248
239
@@ -839,12 +830,7 @@ pub trait FromParsedColor {
839
830
fn from_rgba ( red : u8 , green : u8 , blue : u8 , alpha : f32 ) -> Self ;
840
831
841
832
/// Construct a new color from hue, saturation, lightness and alpha components.
842
- fn from_hsl (
843
- hue : Option < f32 > ,
844
- saturation : Option < f32 > ,
845
- lightness : Option < f32 > ,
846
- alpha : Option < f32 > ,
847
- ) -> Self ;
833
+ fn from_hsl ( hue : f32 , saturation : f32 , lightness : f32 , alpha : f32 ) -> Self ;
848
834
849
835
/// Construct a new color from hue, blackness, whiteness and alpha components.
850
836
fn from_hwb (
@@ -929,12 +915,7 @@ impl FromParsedColor for Color {
929
915
Color :: Rgba ( RGBA :: new ( red, green, blue, alpha) )
930
916
}
931
917
932
- fn from_hsl (
933
- hue : Option < f32 > ,
934
- saturation : Option < f32 > ,
935
- lightness : Option < f32 > ,
936
- alpha : Option < f32 > ,
937
- ) -> Self {
918
+ fn from_hsl ( hue : f32 , saturation : f32 , lightness : f32 , alpha : f32 ) -> Self {
938
919
Color :: Hsl ( Hsl :: new ( hue, saturation, lightness, alpha) )
939
920
}
940
921
@@ -1371,29 +1352,33 @@ where
1371
1352
// the legacy syntax.
1372
1353
let is_legacy_syntax = maybe_hue. is_some ( ) && arguments. try_parse ( |p| p. expect_comma ( ) ) . is_ok ( ) ;
1373
1354
1374
- let saturation: Option < f32 > ;
1375
- let lightness: Option < f32 > ;
1355
+ let saturation: f32 ;
1356
+ let lightness: f32 ;
1376
1357
1377
1358
let alpha = if is_legacy_syntax {
1378
- saturation = Some ( color_parser. parse_percentage ( arguments) ?) ;
1359
+ saturation = color_parser. parse_percentage ( arguments) ?;
1379
1360
arguments. expect_comma ( ) ?;
1380
- lightness = Some ( color_parser. parse_percentage ( arguments) ?) ;
1381
- Some ( parse_legacy_alpha ( color_parser, arguments) ?)
1361
+ lightness = color_parser. parse_percentage ( arguments) ?;
1362
+ parse_legacy_alpha ( color_parser, arguments) ?
1382
1363
} else {
1383
- saturation = parse_none_or ( arguments, |p| color_parser. parse_percentage ( p) ) ?;
1384
- lightness = parse_none_or ( arguments, |p| color_parser. parse_percentage ( p) ) ?;
1364
+ saturation = parse_none_or ( arguments, |p| color_parser. parse_percentage ( p) ) ?. unwrap_or ( 0.0 ) ;
1365
+ lightness = parse_none_or ( arguments, |p| color_parser. parse_percentage ( p) ) ?. unwrap_or ( 0.0 ) ;
1385
1366
1386
1367
if !arguments. is_exhausted ( ) {
1387
1368
arguments. expect_delim ( '/' ) ?;
1388
- parse_none_or ( arguments, |p| parse_alpha_component ( color_parser, p) ) ?
1369
+ parse_none_or ( arguments, |p| parse_alpha_component ( color_parser, p) ) ?. unwrap_or ( 0.0 )
1389
1370
} else {
1390
- Some ( OPAQUE )
1371
+ OPAQUE
1391
1372
}
1392
1373
} ;
1393
1374
1394
- let hue = maybe_hue. map ( |h| normalize_hue ( h. degrees ( ) ) ) ;
1395
- let saturation = saturation. map ( |s| s. clamp ( 0.0 , 1.0 ) ) ;
1396
- let lightness = lightness. map ( |s| s. clamp ( 0.0 , 1.0 ) ) ;
1375
+ let hue = if let Some ( h) = maybe_hue {
1376
+ normalize_hue ( h. degrees ( ) )
1377
+ } else {
1378
+ 0.0
1379
+ } ;
1380
+ let saturation = saturation. clamp ( 0.0 , 1.0 ) ;
1381
+ let lightness = lightness. clamp ( 0.0 , 1.0 ) ;
1397
1382
1398
1383
Ok ( P :: Output :: from_hsl ( hue, saturation, lightness, alpha) )
1399
1384
}
0 commit comments