@@ -467,10 +467,12 @@ impl ToCss for ColorFunction {
467467 }
468468}
469469
470- /// An absolutely specified color.
471- /// https://w3c.github.io/csswg-drafts/ css-color-4/#typedef-absolute- color-base
470+ /// A < color> value .
471+ /// https://drafts.csswg.org/ css-color-4/#color-type
472472#[ derive( Clone , Copy , PartialEq , Debug ) ]
473- pub enum AbsoluteColor {
473+ pub enum Color {
474+ /// The 'currentcolor' keyword.
475+ CurrentColor ,
474476 /// Specify sRGB colors directly by their red/green/blue/alpha chanels.
475477 Rgba ( RGBA ) ,
476478 /// Specifies a CIELAB color by CIE Lightness and its a- and b-axis hue
@@ -491,64 +493,19 @@ pub enum AbsoluteColor {
491493 ColorFunction ( ColorFunction ) ,
492494}
493495
494- impl AbsoluteColor {
495- /// Return the alpha component of any of the schemes within.
496- pub fn alpha ( & self ) -> f32 {
497- match self {
498- Self :: Rgba ( c) => c. alpha ,
499- Self :: Lab ( c) => c. alpha ,
500- Self :: Lch ( c) => c. alpha ,
501- Self :: Oklab ( c) => c. alpha ,
502- Self :: Oklch ( c) => c. alpha ,
503- Self :: ColorFunction ( c) => c. alpha ,
504- }
505- }
506- }
507-
508- impl ToCss for AbsoluteColor {
509- fn to_css < W > ( & self , dest : & mut W ) -> fmt:: Result
510- where
511- W : fmt:: Write ,
512- {
513- match self {
514- Self :: Rgba ( rgba) => rgba. to_css ( dest) ,
515- Self :: Lab ( lab) => lab. to_css ( dest) ,
516- Self :: Lch ( lch) => lch. to_css ( dest) ,
517- Self :: Oklab ( lab) => lab. to_css ( dest) ,
518- Self :: Oklch ( lch) => lch. to_css ( dest) ,
519- Self :: ColorFunction ( color_function) => color_function. to_css ( dest) ,
520- }
521- }
522- }
523-
524- #[ inline]
525- pub ( crate ) const fn rgb ( red : u8 , green : u8 , blue : u8 ) -> Color {
526- rgba ( red, green, blue, OPAQUE )
527- }
528-
529- #[ inline]
530- pub ( crate ) const fn rgba ( red : u8 , green : u8 , blue : u8 , alpha : f32 ) -> Color {
531- Color :: Absolute ( AbsoluteColor :: Rgba ( RGBA :: new ( red, green, blue, alpha) ) )
532- }
533-
534- /// A <color> value.
535- /// https://w3c.github.io/csswg-drafts/css-color-4/#color-type
536- #[ derive( Clone , Copy , PartialEq , Debug ) ]
537- pub enum Color {
538- /// The 'currentcolor' keyword.
539- CurrentColor ,
540- /// An absolutely specified color.
541- Absolute ( AbsoluteColor ) ,
542- }
543-
544496impl ToCss for Color {
545497 fn to_css < W > ( & self , dest : & mut W ) -> fmt:: Result
546498 where
547499 W : fmt:: Write ,
548500 {
549501 match * self {
550502 Color :: CurrentColor => dest. write_str ( "currentcolor" ) ,
551- Color :: Absolute ( absolute) => absolute. to_css ( dest) ,
503+ Color :: Rgba ( rgba) => rgba. to_css ( dest) ,
504+ Color :: Lab ( lab) => lab. to_css ( dest) ,
505+ Color :: Lch ( lch) => lch. to_css ( dest) ,
506+ Color :: Oklab ( lab) => lab. to_css ( dest) ,
507+ Color :: Oklch ( lch) => lch. to_css ( dest) ,
508+ Color :: ColorFunction ( color_function) => color_function. to_css ( dest) ,
552509 }
553510 }
554511}
@@ -686,8 +643,9 @@ impl Color {
686643 let location = input. current_source_location ( ) ;
687644 let token = input. next ( ) ?;
688645 match * token {
689- Token :: Hash ( ref value) | Token :: IDHash ( ref value) => RGBA :: parse_hash ( value. as_bytes ( ) )
690- . map ( |rgba| Color :: Absolute ( AbsoluteColor :: Rgba ( rgba) ) ) ,
646+ Token :: Hash ( ref value) | Token :: IDHash ( ref value) => {
647+ RGBA :: parse_hash ( value. as_bytes ( ) ) . map ( |rgba| Color :: Rgba ( rgba) )
648+ }
691649 Token :: Ident ( ref value) => parse_color_keyword ( & * value) ,
692650 Token :: Function ( ref name) => {
693651 let name = name. clone ( ) ;
@@ -714,6 +672,16 @@ impl Color {
714672/// (For example, the value of an `Ident` token is fine.)
715673#[ inline]
716674pub fn parse_color_keyword ( ident : & str ) -> Result < Color , ( ) > {
675+ #[ inline]
676+ pub ( crate ) const fn rgb ( red : u8 , green : u8 , blue : u8 ) -> Color {
677+ rgba ( red, green, blue, OPAQUE )
678+ }
679+
680+ #[ inline]
681+ pub ( crate ) const fn rgba ( red : u8 , green : u8 , blue : u8 , alpha : f32 ) -> Color {
682+ Color :: Rgba ( RGBA :: new ( red, green, blue, alpha) )
683+ }
684+
717685 ascii_case_insensitive_phf_map ! {
718686 keyword -> Color = {
719687 "black" => rgb( 0 , 0 , 0 ) ,
@@ -927,25 +895,25 @@ where
927895 // for L: 0% = 0.0, 100% = 100.0
928896 // for a and b: -100% = -125, 100% = 125
929897 "lab" => parse_lab_like( component_parser, arguments, 100.0 , 125.0 , |l, a, b, alpha| {
930- Color :: Absolute ( AbsoluteColor :: Lab ( Lab :: new( l. max( 0. ) , a , b , alpha) ) )
898+ Color :: Lab ( Lab :: new( l. max( 0. ) , a , b , alpha) )
931899 } ) ,
932900
933901 // for L: 0% = 0.0, 100% = 100.0
934902 // for C: 0% = 0, 100% = 150
935903 "lch" => parse_lch_like( component_parser, arguments, 100.0 , 150.0 , |l, c, h, alpha| {
936- Color :: Absolute ( AbsoluteColor :: Lch ( Lch :: new( l. max( 0. ) , c. max( 0. ) , h, alpha) ) )
904+ Color :: Lch ( Lch :: new( l. max( 0. ) , c. max( 0. ) , h, alpha) )
937905 } ) ,
938906
939907 // for L: 0% = 0.0, 100% = 1.0
940908 // for a and b: -100% = -0.4, 100% = 0.4
941909 "oklab" => parse_lab_like( component_parser, arguments, 1.0 , 0.4 , |l, a, b, alpha| {
942- Color :: Absolute ( AbsoluteColor :: Oklab ( Oklab :: new( l. max( 0. ) , a, b, alpha) ) )
910+ Color :: Oklab ( Oklab :: new( l. max( 0. ) , a, b, alpha) )
943911 } ) ,
944912
945913 // for L: 0% = 0.0, 100% = 1.0
946914 // for C: 0% = 0.0 100% = 0.4
947915 "oklch" => parse_lch_like( component_parser, arguments, 1.0 , 0.4 , |l, c, h, alpha| {
948- Color :: Absolute ( AbsoluteColor :: Oklch ( Oklch :: new( l. max( 0. ) , c. max( 0. ) , h, alpha) ) )
916+ Color :: Oklch ( Oklch :: new( l. max( 0. ) , c. max( 0. ) , h, alpha) )
949917 } ) ,
950918
951919 "color" => parse_color_function( component_parser, arguments) ,
@@ -1029,7 +997,7 @@ where
1029997 ComponentParser : ColorComponentParser < ' i > ,
1030998{
1031999 let ( red, green, blue, alpha) = parse_rgb_components_rgb ( component_parser, arguments) ?;
1032- Ok ( rgba ( red, green, blue, alpha) )
1000+ Ok ( Color :: Rgba ( RGBA :: new ( red, green, blue, alpha) ) )
10331001}
10341002
10351003/// Parses hsl and hbw syntax, which happens to be identical.
@@ -1076,7 +1044,7 @@ where
10761044
10771045 let alpha = parse_alpha ( component_parser, arguments, uses_commas) ?;
10781046
1079- Ok ( rgba ( red, green, blue, alpha) )
1047+ Ok ( Color :: Rgba ( RGBA :: new ( red, green, blue, alpha) ) )
10801048}
10811049
10821050/// https://drafts.csswg.org/css-color-4/#hwb-to-rgb
@@ -1223,13 +1191,11 @@ where
12231191
12241192 let alpha = parse_alpha ( component_parser, arguments, false ) ?;
12251193
1226- Ok ( Color :: Absolute ( AbsoluteColor :: ColorFunction (
1227- ColorFunction {
1228- color_space,
1229- c1,
1230- c2,
1231- c3,
1232- alpha,
1233- } ,
1234- ) ) )
1194+ Ok ( Color :: ColorFunction ( ColorFunction {
1195+ color_space,
1196+ c1,
1197+ c2,
1198+ c3,
1199+ alpha,
1200+ } ) )
12351201}
0 commit comments