Skip to content

Commit 5b8e47c

Browse files
committed
Improve color serialization to reflect rgb/rgba rules.
1 parent ca04a3b commit 5b8e47c

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/color.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ pub struct RGBA {
2020

2121
impl fmt::Show for RGBA {
2222
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23-
write!(f, "(r={} g={} b={} a={})", self.red, self.green, self.blue, self.alpha)
23+
if self.alpha == 1f32 {
24+
write!(f, "rgb({}, {}, {}", self.red, self.green, self.blue)
25+
} else {
26+
write!(f, "rgba({} {} {} {})", self.red, self.green, self.blue, self.alpha)
27+
}
2428
}
2529
}
2630

@@ -33,10 +37,9 @@ pub enum Color {
3337
impl fmt::Show for Color {
3438
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3539
match self {
36-
&CurrentColor => write!(f, "inherit"),
37-
&RGBAColor(c) => write!(f, "rgba({}, {}, {}, {})", c.red, c.green, c.blue, c.alpha),
38-
};
39-
Ok(())
40+
&CurrentColor => write!(f, "currentColor"),
41+
&RGBAColor(c) => write!(f, "{}", c),
42+
}
4043
}
4144
}
4245

0 commit comments

Comments
 (0)