@@ -13,12 +13,12 @@ use serde_json::{self, json, Map, Value};
1313use self :: test:: Bencher ;
1414
1515use super :: {
16- color:: { rgb, rgba} ,
1716 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 ,
2222} ;
2323
2424macro_rules! JArray {
@@ -398,7 +398,14 @@ fn color4_lab_lch_oklab_oklch() {
398398 run_color_tests (
399399 include_str ! ( "css-parsing-tests/color4_lab_lch_oklab_oklch.json" ) ,
400400 |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+ ] ) ,
402409 Err ( _) => Value :: Null ,
403410 } ,
404411 )
@@ -533,25 +540,25 @@ fn serialize_bad_tokens() {
533540
534541#[ test]
535542fn serialize_current_color ( ) {
536- let c = Color :: CurrentColor ;
543+ let c = CurrentColor ;
537544 assert ! ( c. to_css_string( ) == "currentcolor" ) ;
538545}
539546
540547#[ test]
541548fn serialize_rgb_full_alpha ( ) {
542- let c = rgb ( 255 , 230 , 204 ) ;
549+ let c = SrgbColor :: from_ints ( 255 , 230 , 204 , 255 ) ;
543550 assert_eq ! ( c. to_css_string( ) , "rgb(255, 230, 204)" ) ;
544551}
545552
546553#[ test]
547554fn 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 )" ) ;
550557}
551558
552559#[ test]
553560fn 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 ) ;
555562 assert_eq ! ( c. to_css_string( ) , "rgba(0, 0, 0, 0.5)" ) ;
556563}
557564
@@ -843,16 +850,43 @@ where
843850impl ToJson for Color {
844851 fn to_json ( & self ) -> Value {
845852 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. ,
850875 }
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+ ] ) ,
856890 }
857891 }
858892}
0 commit comments