@@ -467,10 +467,12 @@ impl ToCss for ColorFunction {
467
467
}
468
468
}
469
469
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
472
472
#[ derive( Clone , Copy , PartialEq , Debug ) ]
473
- pub enum AbsoluteColor {
473
+ pub enum Color {
474
+ /// The 'currentcolor' keyword.
475
+ CurrentColor ,
474
476
/// Specify sRGB colors directly by their red/green/blue/alpha chanels.
475
477
Rgba ( RGBA ) ,
476
478
/// Specifies a CIELAB color by CIE Lightness and its a- and b-axis hue
@@ -491,64 +493,19 @@ pub enum AbsoluteColor {
491
493
ColorFunction ( ColorFunction ) ,
492
494
}
493
495
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
-
544
496
impl ToCss for Color {
545
497
fn to_css < W > ( & self , dest : & mut W ) -> fmt:: Result
546
498
where
547
499
W : fmt:: Write ,
548
500
{
549
501
match * self {
550
502
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) ,
552
509
}
553
510
}
554
511
}
@@ -686,8 +643,9 @@ impl Color {
686
643
let location = input. current_source_location ( ) ;
687
644
let token = input. next ( ) ?;
688
645
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
+ }
691
649
Token :: Ident ( ref value) => parse_color_keyword ( & * value) ,
692
650
Token :: Function ( ref name) => {
693
651
let name = name. clone ( ) ;
@@ -714,6 +672,16 @@ impl Color {
714
672
/// (For example, the value of an `Ident` token is fine.)
715
673
#[ inline]
716
674
pub 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
+
717
685
ascii_case_insensitive_phf_map ! {
718
686
keyword -> Color = {
719
687
"black" => rgb( 0 , 0 , 0 ) ,
@@ -927,25 +895,25 @@ where
927
895
// for L: 0% = 0.0, 100% = 100.0
928
896
// for a and b: -100% = -125, 100% = 125
929
897
"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) )
931
899
} ) ,
932
900
933
901
// for L: 0% = 0.0, 100% = 100.0
934
902
// for C: 0% = 0, 100% = 150
935
903
"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) )
937
905
} ) ,
938
906
939
907
// for L: 0% = 0.0, 100% = 1.0
940
908
// for a and b: -100% = -0.4, 100% = 0.4
941
909
"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) )
943
911
} ) ,
944
912
945
913
// for L: 0% = 0.0, 100% = 1.0
946
914
// for C: 0% = 0.0 100% = 0.4
947
915
"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) )
949
917
} ) ,
950
918
951
919
"color" => parse_color_function( component_parser, arguments) ,
@@ -1029,7 +997,7 @@ where
1029
997
ComponentParser : ColorComponentParser < ' i > ,
1030
998
{
1031
999
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) ) )
1033
1001
}
1034
1002
1035
1003
/// Parses hsl and hbw syntax, which happens to be identical.
@@ -1076,7 +1044,7 @@ where
1076
1044
1077
1045
let alpha = parse_alpha ( component_parser, arguments, uses_commas) ?;
1078
1046
1079
- Ok ( rgba ( red, green, blue, alpha) )
1047
+ Ok ( Color :: Rgba ( RGBA :: new ( red, green, blue, alpha) ) )
1080
1048
}
1081
1049
1082
1050
/// https://drafts.csswg.org/css-color-4/#hwb-to-rgb
@@ -1223,13 +1191,11 @@ where
1223
1191
1224
1192
let alpha = parse_alpha ( component_parser, arguments, false ) ?;
1225
1193
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
+ } ) )
1235
1201
}
0 commit comments