22 * License, v. 2.0. If a copy of the MPL was not distributed with this
33 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
5- use std:: ascii:: AsciiExt ;
65use std:: fmt;
76
87use super :: { Token , Parser , ToCss } ;
98
9+ #[ cfg( feature = "serde" ) ]
10+ use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
1011
1112/// A color with red, green, blue, and alpha components.
1213#[ derive( Clone , Copy , PartialEq , Debug ) ]
13- #[ cfg_attr( feature = "serde-serialization" , derive( Deserialize , Serialize ) ) ]
14- #[ cfg_attr( feature = "heap_size" , derive( HeapSizeOf ) ) ]
1514pub struct RGBA {
1615 /// The red channel. Nominally in 0.0 ... 1.0.
1716 pub red : f32 ,
@@ -23,6 +22,34 @@ pub struct RGBA {
2322 pub alpha : f32 ,
2423}
2524
25+ #[ cfg( feature = "serde" ) ]
26+ impl Serialize for RGBA {
27+ fn serialize < S > ( & self , serializer : & mut S ) -> Result < ( ) , S :: Error >
28+ where S : Serializer
29+ {
30+ ( self . red , self . green , self . blue , self . alpha ) . serialize ( serializer)
31+ }
32+ }
33+
34+ #[ cfg( feature = "serde" ) ]
35+ impl Deserialize for RGBA {
36+ fn deserialize < D > ( deserializer : & mut D ) -> Result < Self , D :: Error >
37+ where D : Deserializer
38+ {
39+ let ( red, green, blue, alpha) =
40+ try!( Deserialize :: deserialize ( deserializer) ) ;
41+ Ok ( RGBA {
42+ red : red,
43+ green : green,
44+ blue : blue,
45+ alpha : alpha,
46+ } )
47+ }
48+ }
49+
50+ #[ cfg( feature = "heapsize" ) ]
51+ known_heap_size ! ( 0 , RGBA ) ;
52+
2653impl ToCss for RGBA {
2754 fn to_css < W > ( & self , dest : & mut W ) -> fmt:: Result where W : fmt:: Write {
2855 if self . alpha == 1f32 {
@@ -42,14 +69,16 @@ impl ToCss for RGBA {
4269
4370/// A <color> value.
4471#[ derive( Clone , Copy , PartialEq , Debug ) ]
45- #[ cfg_attr( feature = "heap_size" , derive( HeapSizeOf ) ) ]
4672pub enum Color {
4773 /// The 'currentColor' keyword
4874 CurrentColor ,
4975 /// Everything else gets converted to RGBA during parsing
5076 RGBA ( RGBA ) ,
5177}
5278
79+ #[ cfg( feature = "heapsize" ) ]
80+ known_heap_size ! ( 0 , Color ) ;
81+
5382impl ToCss for Color {
5483 fn to_css < W > ( & self , dest : & mut W ) -> fmt:: Result where W : fmt:: Write {
5584 match self {
0 commit comments