Skip to content

Commit

Permalink
rgb() and rgba() support floating point numbers.
Browse files Browse the repository at this point in the history
  • Loading branch information
yisibl authored Jul 14, 2023
1 parent f77b1b4 commit 8b44f98
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ impl<'a> Stream<'a> {
color.green = from_percent(self.parse_list_number_or_percent()?);
color.blue = from_percent(self.parse_list_number_or_percent()?);
} else {
color.red = bound(0, value as i32, 255) as u8;
color.green = bound(0, self.parse_list_integer()?, 255) as u8;
color.blue = bound(0, self.parse_list_integer()?, 255) as u8;
color.red = f64_bound(0.0, (value.round() as i32).into(), 255.0) as u8;
color.green = f64_bound(0.0, self.parse_list_number()?.round(), 255.0) as u8;
color.blue = f64_bound(0.0, self.parse_list_number()?.round(), 255.0) as u8;
}

self.skip_spaces();
Expand Down Expand Up @@ -407,6 +407,42 @@ mod tests {
Color::new_rgb(254, 203, 231)
);

test!(
rgb_numeric_red_float,
"rgb(3.141592653, 110, 201)",
Color::new_rgb(3, 110, 201)
);

test!(
rgb_numeric_green_float,
"rgb(254, 150.829521289232389, 210)",
Color::new_rgb(254, 151, 210)
);

test!(
rgb_numeric_blue_float,
"rgb(96, 255, 0.2)",
Color::new_rgb(96, 255, 0)
);

test!(
rgb_numeric_all_float,
"rgb(0.0, 129.82, 231.092)",
Color::new_rgb(0, 130, 231)
);

test!(
rgb_numeric_all_float_with_alpha,
"rgb(0.0, 129.82, 231.092, 0.5)",
Color::new_rgba(0, 130, 231, 127)
);

test!(
rgb_numeric_all_float_overflow,
"rgb(290.2, 255.9, 300.0)",
Color::new_rgb(255, 255, 255)
);

test!(
name_red,
"red",
Expand Down Expand Up @@ -449,6 +485,18 @@ mod tests {
Color::new_rgba(10, 20, 30, 127)
);

test!(
rgba_numeric_red_float,
"rgba(3.141592653, 110, 201, 1.0)",
Color::new_rgba(3, 110, 201, 255)
);

test!(
rgba_numeric_all_float,
"rgba(0.0, 129.82, 231.092, 1.5)",
Color::new_rgba(0, 130, 231, 255)
);

test!(
rgba_negative,
"rgba(10, 20, 30, -2)",
Expand Down

0 comments on commit 8b44f98

Please sign in to comment.