Skip to content

Round non-integer rgb() channels to nearest, like browsers - #2641

Open
Arj052 wants to merge 1 commit into
Automattic:masterfrom
Arj052:fix-rgb-channel-rounding
Open

Arj052 wants to merge 1 commit into
Automattic:masterfrom
Arj052:fix-rgb-channel-rounding

Conversation

@Arj052

@Arj052 Arj052 commented Nov 25, 2026 •

Copy link
Copy Markdown

parse_rgb_channel uses ceil(), so non-integer channels always round up: rgb(255, 200.09, 90) gives 201 and rgb(99% 0% 0%) gives 253. Chrome gives 200 and 252. CSS Values 4 says to round to nearest, with halfway values going up.

This PR rounds to nearest, and scales percentages from the parsed value directly so 90% is exactly 229.5 → 230. Three existing assertions that expected the old round-up values are updated. New cases cover 127.4, 127.5, 0.2, 99%, and the repro from #2339 (decimal parsing itself was fixed in #2398, so #2339 can probably be closed).

  • A coding agent was used to make some of these changes

parse_rgb_channel used ceil(), so rgb(255, 200.09, 90) became 201 and
rgb(99% 0% 0%) became 253, while Chrome gives 200 and 252. Round to the
nearest integer with halfway values going up, per CSS Values 4. Scale
percentages from the parsed value directly so 90% is exactly 229.5.

Refs Automattic#2339
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.

1 participant