@@ -564,8 +564,8 @@ where
564
564
{
565
565
let ( red, green, blue, uses_commas) = match_ignore_ascii_case ! { name,
566
566
"rgb" | "rgba" => parse_rgb_components_rgb( component_parser, arguments) ?,
567
- "hsl" | "hsla" => parse_rgb_components_hsl ( component_parser, arguments) ?,
568
- "hwb" => parse_rgb_components_hwb ( component_parser, arguments) ?,
567
+ "hsl" | "hsla" => parse_hsl_hwb ( component_parser, arguments, hsl_to_rgb ) ?,
568
+ "hwb" => parse_hsl_hwb ( component_parser, arguments, hwb_to_rgb ) ?,
569
569
_ => return Err ( arguments. new_unexpected_token_error( Token :: Ident ( name. to_owned( ) . into( ) ) ) ) ,
570
570
} ;
571
571
@@ -624,10 +624,15 @@ where
624
624
Ok ( ( red, green, blue, uses_commas) )
625
625
}
626
626
627
+ /// Parses hsl and hbw syntax, which happens to be identical.
628
+ ///
629
+ /// https://drafts.csswg.org/css-color/#the-hsl-notation
630
+ /// https://drafts.csswg.org/css-color/#the-hbw-notation
627
631
#[ inline]
628
- fn parse_rgb_components_hsl < ' i , ' t , ComponentParser > (
632
+ fn parse_hsl_hwb < ' i , ' t , ComponentParser > (
629
633
component_parser : & ComponentParser ,
630
634
arguments : & mut Parser < ' i , ' t > ,
635
+ to_rgb : impl FnOnce ( f32 , f32 , f32 ) -> ( f32 , f32 , f32 ) ,
631
636
) -> Result < ( u8 , u8 , u8 , bool ) , ParseError < ' i , ComponentParser :: Error > >
632
637
where
633
638
ComponentParser : ColorComponentParser < ' i > ,
@@ -641,54 +646,17 @@ where
641
646
let hue = hue_normalized_degrees / 360. ;
642
647
643
648
// Saturation and lightness are clamped to 0% ... 100%
644
- // https://drafts.csswg.org/css-color/#the-hsl-notation
645
649
let uses_commas = arguments. try_parse ( |i| i. expect_comma ( ) ) . is_ok ( ) ;
646
650
647
- let saturation = component_parser. parse_percentage ( arguments) ?;
648
- let saturation = saturation. max ( 0. ) . min ( 1. ) ;
651
+ let first_percentage = component_parser. parse_percentage ( arguments) ?. max ( 0. ) . min ( 1. ) ;
649
652
650
653
if uses_commas {
651
654
arguments. expect_comma ( ) ?;
652
655
}
653
656
654
- let lightness = component_parser. parse_percentage ( arguments) ?;
655
- let lightness = lightness. max ( 0. ) . min ( 1. ) ;
656
-
657
- let ( red, green, blue) = hsl_to_rgb ( hue, saturation, lightness) ;
658
- let red = clamp_unit_f32 ( red) ;
659
- let green = clamp_unit_f32 ( green) ;
660
- let blue = clamp_unit_f32 ( blue) ;
661
- Ok ( ( red, green, blue, uses_commas) )
662
- }
663
-
664
- #[ inline]
665
- fn parse_rgb_components_hwb < ' i , ' t , ComponentParser > (
666
- component_parser : & ComponentParser ,
667
- arguments : & mut Parser < ' i , ' t > ,
668
- ) -> Result < ( u8 , u8 , u8 , bool ) , ParseError < ' i , ComponentParser :: Error > >
669
- where
670
- ComponentParser : ColorComponentParser < ' i > ,
671
- {
672
- let hue_degrees = component_parser. parse_angle_or_number ( arguments) ?. degrees ( ) ;
673
-
674
- // Subtract an integer before rounding, to avoid some rounding errors:
675
- let hue_normalized_degrees = hue_degrees - 360. * ( hue_degrees / 360. ) . floor ( ) ;
676
- let hue = hue_normalized_degrees / 360. ;
677
-
678
- let uses_commas = arguments. try_parse ( |i| i. expect_comma ( ) ) . is_ok ( ) ;
679
-
680
- let whiteness = component_parser. parse_percentage ( arguments) ?;
681
- let whiteness = whiteness. max ( 0. ) . min ( 1. ) ;
682
-
683
- if uses_commas {
684
- arguments. expect_comma ( ) ?;
685
- }
686
-
687
- let blackness = component_parser. parse_percentage ( arguments) ?;
688
- let blackness = blackness. max ( 0. ) . min ( 1. ) ;
689
-
690
- let ( red, green, blue) = hwb_to_rgb ( hue, whiteness, blackness) ;
657
+ let second_percentage = component_parser. parse_percentage ( arguments) ?. max ( 0. ) . min ( 1. ) ;
691
658
659
+ let ( red, green, blue) = to_rgb ( hue, first_percentage, second_percentage) ;
692
660
let red = clamp_unit_f32 ( red) ;
693
661
let green = clamp_unit_f32 ( green) ;
694
662
let blue = clamp_unit_f32 ( blue) ;
@@ -711,10 +679,10 @@ fn hwb_to_rgb(h: f32, w: f32, b: f32) -> (f32, f32, f32) {
711
679
( red, green, blue)
712
680
}
713
681
682
+ /// https://drafts.csswg.org/css-color/#hsl-color
683
+ /// except with h pre-multiplied by 3, to avoid some rounding errors.
714
684
#[ inline]
715
685
fn hsl_to_rgb ( hue : f32 , saturation : f32 , lightness : f32 ) -> ( f32 , f32 , f32 ) {
716
- // https://drafts.csswg.org/css-color/#hsl-color
717
- // except with h pre-multiplied by 3, to avoid some rounding errors.
718
686
fn hue_to_rgb ( m1 : f32 , m2 : f32 , mut h3 : f32 ) -> f32 {
719
687
if h3 < 0. {
720
688
h3 += 3.
0 commit comments