Skip to content

Commit 1911a16

Browse files
committed
Ensure serialization takes precision into account.
1 parent 91338f6 commit 1911a16

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/color.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const OPAQUE: f32 = 1.0;
1616
#[inline]
1717
fn serialize_alpha(dest: &mut impl fmt::Write, alpha: f32, legacy_syntax: bool) -> fmt::Result {
1818
// If the alpha component is full opaque, don't emit the alpha value in CSS.
19-
if alpha == OPAQUE {
19+
if clamp_unit_f32(alpha) == 255 {
2020
return Ok(());
2121
}
2222

@@ -159,7 +159,7 @@ impl ToCss for RGBA {
159159
where
160160
W: fmt::Write,
161161
{
162-
let has_alpha = self.alpha != OPAQUE;
162+
let has_alpha = clamp_unit_f32(self.alpha) != 255;
163163

164164
dest.write_str(if has_alpha { "rgba(" } else { "rgb(" })?;
165165
self.red.to_css(dest)?;

src/tests.rs

+7
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,13 @@ fn serialize_rgb_full_alpha() {
572572
fn serialize_rgba() {
573573
let c = rgba(26, 51, 77, 0.125);
574574
assert_eq!(c.to_css_string(), "rgba(26, 51, 77, 0.125)");
575+
576+
let c = rgba(26, 51, 77, 0.99);
577+
assert_eq!(c.to_css_string(), "rgba(26, 51, 77, 0.99)");
578+
let c = rgba(26, 51, 77, 0.999);
579+
assert_eq!(c.to_css_string(), "rgb(26, 51, 77)");
580+
let c = rgba(26, 51, 77, 0.9999);
581+
assert_eq!(c.to_css_string(), "rgb(26, 51, 77)");
575582
}
576583

577584
#[test]

0 commit comments

Comments
 (0)