From ca04a3be42917c8c7b4bd7b130f25683bd18a571 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Fri, 19 Sep 2014 13:13:56 -0400 Subject: [PATCH 1/4] Implement basic, incorrect color value serialization. --- src/color.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/color.rs b/src/color.rs index 4c7dde3b..f7ce49ed 100644 --- a/src/color.rs +++ b/src/color.rs @@ -30,6 +30,15 @@ pub enum Color { RGBAColor(RGBA), } +impl fmt::Show for Color { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + &CurrentColor => write!(f, "inherit"), + &RGBAColor(c) => write!(f, "rgba({}, {}, {}, {})", c.red, c.green, c.blue, c.alpha), + }; + Ok(()) + } +} /// Return `Err(())` on invalid or unsupported value (not a color). impl Color { From 5b8e47cb6c6c2af4527a8eaf3aad76f95c1e7599 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Mon, 22 Sep 2014 11:21:00 -0400 Subject: [PATCH 2/4] Improve color serialization to reflect rgb/rgba rules. --- src/color.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/color.rs b/src/color.rs index f7ce49ed..1af77cea 100644 --- a/src/color.rs +++ b/src/color.rs @@ -20,7 +20,11 @@ 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, self.green, self.blue) + } else { + write!(f, "rgba({} {} {} {})", self.red, self.green, self.blue, self.alpha) + } } } @@ -33,10 +37,9 @@ pub enum Color { impl fmt::Show for Color { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - &CurrentColor => write!(f, "inherit"), - &RGBAColor(c) => write!(f, "rgba({}, {}, {}, {})", c.red, c.green, c.blue, c.alpha), - }; - Ok(()) + &CurrentColor => write!(f, "currentColor"), + &RGBAColor(c) => write!(f, "{}", c), + } } } From 3716158d93f096a4aa03528b23869908d43ab79c Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Wed, 1 Oct 2014 00:45:27 -0400 Subject: [PATCH 3/4] Fix color serialization. --- src/color.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/color.rs b/src/color.rs index 1af77cea..98628a48 100644 --- a/src/color.rs +++ b/src/color.rs @@ -21,9 +21,11 @@ pub struct RGBA { impl fmt::Show for RGBA { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if self.alpha == 1f32 { - write!(f, "rgb({}, {}, {}", self.red, self.green, self.blue) + write!(f, "rgb({}, {}, {})", (self.red * 255.).round(), (self.green * 255.).round(), + (self.blue * 255.).round()) } else { - write!(f, "rgba({} {} {} {})", self.red, self.green, self.blue, self.alpha) + write!(f, "rgba({}, {}, {}, {})", (self.red * 255.).round(), (self.green * 255.).round(), + (self.blue * 255.).round(), self.alpha) } } } From 03231db5def269cd634d541a9f7a53af1fa97855 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Wed, 1 Oct 2014 14:33:21 -0400 Subject: [PATCH 4/4] Add color serialization unit tests. --- src/tests.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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 {