Skip to content

Commit be1ebf7

Browse files
committed
Avoid using formatting strings in color serialization
1 parent 26c5e6d commit be1ebf7

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/color.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,26 @@ impl ToCss for RGBA {
100100
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
101101
where W: fmt::Write,
102102
{
103-
// Try first with two decimal places, then with three.
104-
let mut rounded_alpha = (self.alpha_f32() * 100.).round() / 100.;
105-
if clamp_unit_f32(rounded_alpha) != self.alpha {
106-
rounded_alpha = (self.alpha_f32() * 1000.).round() / 1000.;
107-
}
103+
let serialize_alpha = self.alpha != 255;
104+
105+
dest.write_str(if serialize_alpha { "rgba(" } else { "rgb(" })?;
106+
self.red.to_css(dest)?;
107+
dest.write_str(", ")?;
108+
self.green.to_css(dest)?;
109+
dest.write_str(", ")?;
110+
self.blue.to_css(dest)?;
111+
if serialize_alpha {
112+
dest.write_str(", ")?;
113+
114+
// Try first with two decimal places, then with three.
115+
let mut rounded_alpha = (self.alpha_f32() * 100.).round() / 100.;
116+
if clamp_unit_f32(rounded_alpha) != self.alpha {
117+
rounded_alpha = (self.alpha_f32() * 1000.).round() / 1000.;
118+
}
108119

109-
if self.alpha == 255 {
110-
write!(dest, "rgb({}, {}, {})", self.red, self.green, self.blue)
111-
} else {
112-
write!(dest, "rgba({}, {}, {}, {})",
113-
self.red, self.green, self.blue, rounded_alpha)
120+
rounded_alpha.to_css(dest)?;
114121
}
122+
dest.write_char(')')
115123
}
116124
}
117125

0 commit comments

Comments
 (0)