diff --git a/src/color.rs b/src/color.rs index 4c7dde3b..98628a48 100644 --- a/src/color.rs +++ b/src/color.rs @@ -20,7 +20,13 @@ pub struct RGBA { impl fmt::Show for RGBA { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "(r={} g={} b={} a={})", self.red, self.green, self.blue, self.alpha) + if self.alpha == 1f32 { + write!(f, "rgb({}, {}, {})", (self.red * 255.).round(), (self.green * 255.).round(), + (self.blue * 255.).round()) + } else { + write!(f, "rgba({}, {}, {}, {})", (self.red * 255.).round(), (self.green * 255.).round(), + (self.blue * 255.).round(), self.alpha) + } } } @@ -30,6 +36,14 @@ pub enum Color { RGBAColor(RGBA), } +impl fmt::Show for Color { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + &CurrentColor => write!(f, "currentColor"), + &RGBAColor(c) => write!(f, "{}", c), + } + } +} /// Return `Err(())` on invalid or unsupported value (not a color). impl Color { diff --git a/src/tests.rs b/src/tests.rs index bf1ad0a4..ae0c5852 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -275,6 +275,27 @@ fn serializer() { } +#[test] +fn serialize_current_color() { + let c = CurrentColor; + assert!(format!("{}", c).as_slice() == "currentColor"); +} + + +#[test] +fn serialize_rgb_full_alpha() { + let c = RGBAColor(RGBA { red: 1.0, green: 0.9, blue: 0.8, alpha: 1.0 }); + assert!(format!("{}", c).as_slice() == "rgb(255, 230, 204)"); +} + + +#[test] +fn serialize_rgba() { + let c = RGBAColor(RGBA { red: 0.1, green: 0.2, blue: 0.3, alpha: 0.5 }); + assert!(format!("{}", c).as_slice() == "rgba(26, 51, 77, 0.5)"); +} + + impl ToJson for Result { fn to_json(&self) -> json::Json { match *self {