Skip to content

Conversation

@tomcur
Copy link
Member

@tomcur tomcur commented Jan 3, 2025

The cast to u8 is saturating, so some bounds checking can be dropped.

The division and multiplication in value / 100.0 * 255.0 are not collapsed, so it has the same rounding behavior as when using parse_number_or_percent.

The cast to `u8` is saturating, so some bounds checking can be dropped.

The division and multiplication in `value / 100.0 * 255.0` are not
collapsed, so it has the same rounding behavior as when using
`parse_number_or_percent`.
Copy link
Member

@DJMcNab DJMcNab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the analysis.

There are additional is_finite assertions in the old version, but hopefully the parse would have failed if it hit NaN? I think the one case which could be problematic is if we parse a floating point number which is infinity (e.g. 10e50000 or w/e). Maybe it's worth a test to make sure this has the same behaviour in that case?

Edit: But of course, we want that to clamp, which will be the case with this new behaviour.

@waywardmonkeys
Copy link
Collaborator

This code is all going away in the. next breaking release (due to the color crate replacing it).

@waywardmonkeys
Copy link
Collaborator

(That's more of an FYI / reminder. Not saying I disapprove of the changes.)

@DJMcNab DJMcNab mentioned this pull request Jan 6, 2025
Copy link
Collaborator

@waywardmonkeys waywardmonkeys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 2 more instances of this code pattern with the alpha here and hsla parsing which could be adjusted similarly.

src/color.rs Outdated
color.red = from_percent(value / 100.0);
color.green = from_percent(self.parse_list_number_or_percent()?);
color.blue = from_percent(self.parse_list_number_or_percent()?);
color.red = (value / 100.0 * 255.0).round() as u8;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sort of feel like extra parens here would be nice for my braindead days.

@tomcur tomcur changed the title Simplify RGB rounding in parser Simplify color component rounding in parser Jan 10, 2025
@tomcur tomcur added this pull request to the merge queue Jan 10, 2025
@tomcur tomcur removed this pull request from the merge queue due to a manual request Jan 10, 2025
@tomcur tomcur added this pull request to the merge queue Jan 10, 2025
Merged via the queue into main with commit 6ab61a7 Jan 10, 2025
15 checks passed
@tomcur tomcur deleted the simplify-bounds branch January 10, 2025 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants