Skip to content

Core: Allow fractional channel values in rgb() and rgba() - #195

Open
RaphaelFakhri wants to merge 1 commit into
jquery:mainfrom
RaphaelFakhri:fix-rgb-fractional-channels
Open

RaphaelFakhri wants to merge 1 commit into
jquery:mainfrom
RaphaelFakhri:fix-rgb-fractional-channels

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

CSS Color Module Level 4 allows numbers, not only integers, for the red, green, and blue arguments of rgb() and rgba(), and browsers may return values such as rgb(2.5, 3.4, 4.6) from getComputedStyle() (see the web-platform-tests case css/css-color/parsing/color-computed-rgb.html). The integer rgb() parser accepted only whole numbers, so these strings fell back to the default color, and animations started from or ended at white.

This change accepts an optional fractional part in each channel. The value is then floored like the percentage form, so rgb(127.5, 0, 0) parses the same as rgb(50%, 0%, 0%).

Before:

jQuery.Color( "rgb(127.5, 0, 0)" ).toRgbaString();
//> "rgb(255, 255, 255)"

After:

jQuery.Color( "rgb(127.5, 0, 0)" ).toRgbaString();
//> "rgb(127, 0, 0)"

The two new parse tests fail without the source change:

parse: jQuery.Color( "rgb(127.5, 127.25, 127.9)" )
expected: 127
actual: 255

npm run test:chrome passes for all jQuery versions in jtr.yml.

Fixes #194

CSS Color Module Level 4 allows numbers, not only integers, for the
red, green and blue arguments of rgb() and rgba(), and browsers may
return values such as "rgb(2.5, 3.4, 4.6)" from getComputedStyle().
The rgb parser only accepted integers, so these strings fell back to
the default color (white), and animations started from or ended at
white.

Accept an optional fractional part in each channel. The value is then
floored like the percentage form, so "rgb(127.5, 0, 0)" parses the
same as "rgb(50%, 0%, 0%)".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

rgb() and rgba() with fractional channel values parse as white

1 participant