@@ -6,8 +6,11 @@ use std::ascii::AsciiExt;
6
6
use std:: fmt;
7
7
use std:: num:: { Float , FloatMath } ;
8
8
9
+ use text_writer:: { mod, TextWriter } ;
10
+
9
11
use ast:: { ComponentValue , SkipWhitespaceIterable } ;
10
12
use ast:: ComponentValue :: { Number , Percentage , Function , Ident , Hash , IDHash , Comma } ;
13
+ use serializer:: ToCss ;
11
14
12
15
13
16
#[ deriving( Clone , Copy , PartialEq ) ]
@@ -20,14 +23,19 @@ pub struct RGBA {
20
23
pub alpha : f32 ,
21
24
}
22
25
23
- impl fmt :: Show for RGBA {
24
- fn fmt ( & self , f : & mut fmt :: Formatter ) -> fmt :: Result {
26
+ impl ToCss for RGBA {
27
+ fn to_css < W > ( & self , dest : & mut W ) -> text_writer :: Result where W : TextWriter {
25
28
if self . alpha == 1f32 {
26
- write ! ( f, "rgb({}, {}, {})" , ( self . red * 255. ) . round( ) , ( self . green * 255. ) . round( ) ,
29
+ write ! ( dest, "rgb({}, {}, {})" ,
30
+ ( self . red * 255. ) . round( ) ,
31
+ ( self . green * 255. ) . round( ) ,
27
32
( self . blue * 255. ) . round( ) )
28
33
} else {
29
- write ! ( f, "rgba({}, {}, {}, {})" , ( self . red * 255. ) . round( ) , ( self . green * 255. ) . round( ) ,
30
- ( self . blue * 255. ) . round( ) , self . alpha)
34
+ write ! ( dest, "rgba({}, {}, {}, {})" ,
35
+ ( self . red * 255. ) . round( ) ,
36
+ ( self . green * 255. ) . round( ) ,
37
+ ( self . blue * 255. ) . round( ) ,
38
+ self . alpha)
31
39
}
32
40
}
33
41
}
@@ -38,15 +46,23 @@ pub enum Color {
38
46
RGBA ( RGBA ) ,
39
47
}
40
48
41
- impl fmt :: Show for Color {
42
- fn fmt ( & self , f : & mut fmt :: Formatter ) -> fmt :: Result {
49
+ impl ToCss for Color {
50
+ fn to_css < W > ( & self , dest : & mut W ) -> text_writer :: Result where W : TextWriter {
43
51
match self {
44
- & Color :: CurrentColor => write ! ( f , "currentColor" ) ,
45
- & Color :: RGBA ( c ) => write ! ( f , "{}" , c ) ,
52
+ & Color :: CurrentColor => dest . write_str ( "currentColor" ) ,
53
+ & Color :: RGBA ( rgba ) => rgba . to_css ( dest ) ,
46
54
}
47
55
}
48
56
}
49
57
58
+ impl fmt:: Show for RGBA {
59
+ #[ inline] fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result { self . fmt_to_css ( f) }
60
+ }
61
+
62
+ impl fmt:: Show for Color {
63
+ #[ inline] fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result { self . fmt_to_css ( f) }
64
+ }
65
+
50
66
/// Return `Err(())` on invalid or unsupported value (not a color).
51
67
impl Color {
52
68
pub fn parse ( component_value : & ComponentValue ) -> Result < Color , ( ) > {
0 commit comments