@@ -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
)
@@ -427,9 +434,27 @@ fn parse_comma_separated_ignoring_errors() {
427
434
Color :: parse ( input) . map_err ( Into :: < ParseError < ( ) > > :: into)
428
435
} ) ;
429
436
assert_eq ! ( result. len( ) , 3 ) ;
430
- assert_eq ! ( result[ 0 ] . to_css_string( ) , "rgb(255, 0, 0)" ) ;
431
- assert_eq ! ( result[ 1 ] . to_css_string( ) , "rgb(255, 255, 0)" ) ;
432
- assert_eq ! ( result[ 2 ] . to_css_string( ) , "rgb(0, 0, 255)" ) ;
437
+ assert_eq ! (
438
+ result[ 0 ] ,
439
+ Color :: NamedColor ( NamedColor :: new(
440
+ "red" . to_string( ) ,
441
+ SrgbColor :: from_ints( 255 , 0 , 0 , 255 )
442
+ ) )
443
+ ) ;
444
+ assert_eq ! (
445
+ result[ 1 ] ,
446
+ Color :: NamedColor ( NamedColor :: new(
447
+ "yellow" . to_string( ) ,
448
+ SrgbColor :: from_ints( 255 , 255 , 0 , 255 )
449
+ ) )
450
+ ) ;
451
+ assert_eq ! (
452
+ result[ 2 ] ,
453
+ Color :: NamedColor ( NamedColor :: new(
454
+ "blue" . to_string( ) ,
455
+ SrgbColor :: from_ints( 0 , 0 , 255 , 255 )
456
+ ) )
457
+ ) ;
433
458
}
434
459
435
460
#[ test]
@@ -547,25 +572,25 @@ fn serialize_bad_tokens() {
547
572
548
573
#[ test]
549
574
fn serialize_current_color ( ) {
550
- let c = Color :: CurrentColor ;
575
+ let c = CurrentColor ;
551
576
assert ! ( c. to_css_string( ) == "currentcolor" ) ;
552
577
}
553
578
554
579
#[ test]
555
580
fn serialize_rgb_full_alpha ( ) {
556
- let c = rgb ( 255 , 230 , 204 ) ;
581
+ let c = SrgbColor :: from_ints ( 255 , 230 , 204 , 255 ) ;
557
582
assert_eq ! ( c. to_css_string( ) , "rgb(255, 230, 204)" ) ;
558
583
}
559
584
560
585
#[ test]
561
586
fn serialize_rgba ( ) {
562
- let c = rgba ( 26 , 51 , 77 , 0.125 ) ;
563
- assert_eq ! ( c. to_css_string( ) , "rgba(26, 51, 77, 0.125 )" ) ;
587
+ let c = SrgbColor :: from_ints ( 26 , 51 , 77 , 32 ) ;
588
+ assert_eq ! ( c. to_css_string( ) , "rgba(26, 51, 77, 0.12549 )" ) ;
564
589
}
565
590
566
591
#[ test]
567
592
fn serialize_rgba_two_digit_float_if_roundtrips ( ) {
568
- let c = Color :: Absolute ( AbsoluteColor :: Rgba ( RGBA :: from_floats ( 0. , 0. , 0. , 0.5 ) ) ) ;
593
+ let c = SrgbColor :: from_floats ( 0. , 0. , 0. , 0.5 ) ;
569
594
assert_eq ! ( c. to_css_string( ) , "rgba(0, 0, 0, 0.5)" ) ;
570
595
}
571
596
@@ -857,16 +882,43 @@ where
857
882
impl ToJson for Color {
858
883
fn to_json ( & self ) -> Value {
859
884
match * self {
860
- Color :: CurrentColor => "currentcolor" . to_json ( ) ,
861
- Color :: Absolute ( absolute) => match absolute {
862
- AbsoluteColor :: Rgba ( ref rgba) => {
863
- json ! ( [ rgba. red, rgba. green, rgba. blue, rgba. alpha] )
885
+ Color :: SrgbColor ( ref srgb_color)
886
+ | Color :: NamedColor ( NamedColor {
887
+ value : ref srgb_color,
888
+ ..
889
+ } ) => {
890
+ let rgba = srgb_color. to_rgba ( ) ;
891
+ json ! ( [ rgba. red, rgba. green, rgba. blue, srgb_color. to_floats( ) . 3 ] )
892
+ }
893
+ Color :: SystemColor ( ref system_color)
894
+ | Color :: DeprecatedColor ( DeprecatedColor {
895
+ same_as : ref system_color,
896
+ ..
897
+ } ) => system_color. name . to_json ( ) ,
898
+ Color :: CurrentColor ( CurrentColor ) => "currentcolor" . to_json ( ) ,
899
+ Color :: CielabColor ( CielabColor :: CieLab ( ref lab_coords) )
900
+ | Color :: OklabColor ( OklabColor :: OkLab ( ref lab_coords) ) => json ! ( [
901
+ lab_coords. lightness,
902
+ lab_coords. a,
903
+ lab_coords. b,
904
+ match lab_coords. alpha {
905
+ Some ( alpha) => alpha. number,
906
+ None => 0. ,
864
907
}
865
- AbsoluteColor :: Lab ( ref c) => json ! ( [ c. lightness, c. a, c. b, c. alpha] ) ,
866
- AbsoluteColor :: Lch ( ref c) => json ! ( [ c. lightness, c. chroma, c. hue, c. alpha] ) ,
867
- AbsoluteColor :: Oklab ( ref c) => json ! ( [ c. lightness, c. a, c. b, c. alpha] ) ,
868
- AbsoluteColor :: Oklch ( ref c) => json ! ( [ c. lightness, c. chroma, c. hue, c. alpha] ) ,
869
- } ,
908
+ ] ) ,
909
+ Color :: CielabColor ( CielabColor :: CieLch ( ref lch_coords) )
910
+ | Color :: OklabColor ( OklabColor :: OkLch ( ref lch_coords) ) => json ! ( [
911
+ lch_coords. lightness,
912
+ lch_coords. chroma,
913
+ match lch_coords. hue {
914
+ Some ( hue) => hue. degrees,
915
+ None => 0. ,
916
+ } ,
917
+ match lch_coords. alpha {
918
+ Some ( alpha) => alpha. number,
919
+ None => 0. ,
920
+ }
921
+ ] ) ,
870
922
}
871
923
}
872
924
}
0 commit comments