Skip to content

Commit

Permalink
Correct rounding of alpha. (#35)
Browse files Browse the repository at this point in the history
This makes the behavior match the other values in how they're rounded as
well as the WPT.
  • Loading branch information
waywardmonkeys authored Dec 27, 2024
1 parent 62d0847 commit 7c79573
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ impl Stream<'_> {

self.skip_spaces();
if !self.starts_with(b")") {
color.alpha = (f64_bound(0.0, self.parse_list_number()?, 1.0) * 255.0) as u8;
color.alpha =
(f64_bound(0.0, self.parse_list_number()?, 1.0) * 255.0).round() as u8;
}

self.skip_spaces();
Expand All @@ -215,7 +216,8 @@ impl Stream<'_> {

self.skip_spaces();
if !self.starts_with(b")") {
color.alpha = (f64_bound(0.0, self.parse_list_number()?, 1.0) * 255.0) as u8;
color.alpha =
(f64_bound(0.0, self.parse_list_number()?, 1.0) * 255.0).round() as u8;
}

self.skip_spaces();
Expand Down Expand Up @@ -437,7 +439,7 @@ mod tests {
test!(
rgb_numeric_all_float_with_alpha,
"rgb(0.0, 129.82, 231.092, 0.5)",
Color::new_rgba(0, 130, 231, 127)
Color::new_rgba(0, 130, 231, 128)
);

test!(
Expand Down Expand Up @@ -485,7 +487,7 @@ mod tests {
test!(
rgba_half,
"rgba(10, 20, 30, 0.5)",
Color::new_rgba(10, 20, 30, 127)
Color::new_rgba(10, 20, 30, 128)
);

test!(
Expand Down Expand Up @@ -515,7 +517,7 @@ mod tests {
test!(
rgb_with_alpha,
"rgb(10, 20, 30, 0.5)",
Color::new_rgba(10, 20, 30, 127)
Color::new_rgba(10, 20, 30, 128)
);

test!(
Expand Down Expand Up @@ -545,13 +547,13 @@ mod tests {
test!(
hsla_green,
"hsla(120, 100%, 75%, 0.5)",
Color::new_rgba(128, 255, 128, 127)
Color::new_rgba(128, 255, 128, 128)
);

test!(
hsl_with_alpha,
"hsl(120, 100%, 75%, 0.5)",
Color::new_rgba(128, 255, 128, 127)
Color::new_rgba(128, 255, 128, 128)
);

test!(
Expand All @@ -569,7 +571,7 @@ mod tests {
test!(
hsla_with_hue_float,
"hsla(120.152, 100%, 75%, 0.5)",
Color::new_rgba(128, 255, 128, 127)
Color::new_rgba(128, 255, 128, 128)
);

macro_rules! test_err {
Expand Down

0 comments on commit 7c79573

Please sign in to comment.