@@ -13,12 +13,12 @@ use serde_json::{self, json, Map, Value};
13
13
use self :: test:: Bencher ;
14
14
15
15
use super :: {
16
- color:: { rgb, rgba} ,
17
16
parse_important, parse_nth, parse_one_declaration, parse_one_rule, stylesheet_encoding,
18
- AbsoluteColor , AtRuleParser , BasicParseError , BasicParseErrorKind , Color , CowRcStr ,
19
- DeclarationListParser , DeclarationParser , Delimiter , EncodingSupport , ParseError ,
20
- ParseErrorKind , Parser , ParserInput , ParserState , QualifiedRuleParser , RuleListParser ,
21
- SourceLocation , ToCss , Token , TokenSerializationType , UnicodeRange , RGBA ,
17
+ AtRuleParser , BasicParseError , BasicParseErrorKind , CielabColor , Color , CowRcStr , CurrentColor ,
18
+ DeclarationListParser , DeclarationParser , Delimiter , DeprecatedColor , EncodingSupport ,
19
+ NamedColor , OklabColor , ParseError , ParseErrorKind , Parser , ParserInput , ParserState ,
20
+ QualifiedRuleParser , RuleListParser , SourceLocation , SrgbColor , ToCss , Token ,
21
+ TokenSerializationType , UnicodeRange ,
22
22
} ;
23
23
24
24
macro_rules! JArray {
@@ -398,7 +398,14 @@ fn color4_lab_lch_oklab_oklch() {
398
398
run_color_tests (
399
399
include_str ! ( "css-parsing-tests/color4_lab_lch_oklab_oklch.json" ) ,
400
400
|c| match c {
401
- Ok ( color) => Value :: Array ( vec ! [ color. to_json( ) , color. to_css_string( ) . to_json( ) ] ) ,
401
+ Ok ( color) => Value :: Array ( vec ! [
402
+ color. to_json( ) ,
403
+ match color {
404
+ Color :: CielabColor ( cielab_color) => cielab_color. to_css_string( ) . to_json( ) ,
405
+ Color :: OklabColor ( oklab_color) => oklab_color. to_css_string( ) . to_json( ) ,
406
+ _ => Value :: Null ,
407
+ } ,
408
+ ] ) ,
402
409
Err ( _) => Value :: Null ,
403
410
} ,
404
411
)
@@ -533,25 +540,25 @@ fn serialize_bad_tokens() {
533
540
534
541
#[ test]
535
542
fn serialize_current_color ( ) {
536
- let c = Color :: CurrentColor ;
543
+ let c = CurrentColor ;
537
544
assert ! ( c. to_css_string( ) == "currentcolor" ) ;
538
545
}
539
546
540
547
#[ test]
541
548
fn serialize_rgb_full_alpha ( ) {
542
- let c = rgb ( 255 , 230 , 204 ) ;
549
+ let c = SrgbColor :: from_ints ( 255 , 230 , 204 , 255 ) ;
543
550
assert_eq ! ( c. to_css_string( ) , "rgb(255, 230, 204)" ) ;
544
551
}
545
552
546
553
#[ test]
547
554
fn serialize_rgba ( ) {
548
- let c = rgba ( 26 , 51 , 77 , 0.125 ) ;
549
- assert_eq ! ( c. to_css_string( ) , "rgba(26, 51, 77, 0.125 )" ) ;
555
+ let c = SrgbColor :: from_ints ( 26 , 51 , 77 , 32 ) ;
556
+ assert_eq ! ( c. to_css_string( ) , "rgba(26, 51, 77, 0.12549 )" ) ;
550
557
}
551
558
552
559
#[ test]
553
560
fn serialize_rgba_two_digit_float_if_roundtrips ( ) {
554
- let c = Color :: Absolute ( AbsoluteColor :: Rgba ( RGBA :: from_floats ( 0. , 0. , 0. , 0.5 ) ) ) ;
561
+ let c = SrgbColor :: from_floats ( 0. , 0. , 0. , 0.5 ) ;
555
562
assert_eq ! ( c. to_css_string( ) , "rgba(0, 0, 0, 0.5)" ) ;
556
563
}
557
564
@@ -843,16 +850,43 @@ where
843
850
impl ToJson for Color {
844
851
fn to_json ( & self ) -> Value {
845
852
match * self {
846
- Color :: CurrentColor => "currentcolor" . to_json ( ) ,
847
- Color :: Absolute ( absolute) => match absolute {
848
- AbsoluteColor :: Rgba ( ref rgba) => {
849
- json ! ( [ rgba. red, rgba. green, rgba. blue, rgba. alpha] )
853
+ Color :: SrgbColor ( ref srgb_color)
854
+ | Color :: NamedColor ( NamedColor {
855
+ value : ref srgb_color,
856
+ ..
857
+ } ) => {
858
+ let rgba = srgb_color. to_rgba ( ) ;
859
+ json ! ( [ rgba. red, rgba. green, rgba. blue, srgb_color. to_floats( ) . 3 ] )
860
+ }
861
+ Color :: SystemColor ( ref system_color)
862
+ | Color :: DeprecatedColor ( DeprecatedColor {
863
+ same_as : ref system_color,
864
+ ..
865
+ } ) => system_color. name . to_json ( ) ,
866
+ Color :: CurrentColor ( CurrentColor ) => "currentcolor" . to_json ( ) ,
867
+ Color :: CielabColor ( CielabColor :: CieLab ( ref lab_coords) )
868
+ | Color :: OklabColor ( OklabColor :: OkLab ( ref lab_coords) ) => json ! ( [
869
+ lab_coords. lightness,
870
+ lab_coords. a,
871
+ lab_coords. b,
872
+ match lab_coords. alpha {
873
+ Some ( alpha) => alpha. number,
874
+ None => 0. ,
850
875
}
851
- AbsoluteColor :: Lab ( ref c) => json ! ( [ c. lightness, c. a, c. b, c. alpha] ) ,
852
- AbsoluteColor :: Lch ( ref c) => json ! ( [ c. lightness, c. chroma, c. hue, c. alpha] ) ,
853
- AbsoluteColor :: Oklab ( ref c) => json ! ( [ c. lightness, c. a, c. b, c. alpha] ) ,
854
- AbsoluteColor :: Oklch ( ref c) => json ! ( [ c. lightness, c. chroma, c. hue, c. alpha] ) ,
855
- } ,
876
+ ] ) ,
877
+ Color :: CielabColor ( CielabColor :: CieLch ( ref lch_coords) )
878
+ | Color :: OklabColor ( OklabColor :: OkLch ( ref lch_coords) ) => json ! ( [
879
+ lch_coords. lightness,
880
+ lch_coords. chroma,
881
+ match lch_coords. hue {
882
+ Some ( hue) => hue. degrees,
883
+ None => 0. ,
884
+ } ,
885
+ match lch_coords. alpha {
886
+ Some ( alpha) => alpha. number,
887
+ None => 0. ,
888
+ }
889
+ ] ) ,
856
890
}
857
891
}
858
892
}
0 commit comments