Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernization to the current CSS rgb/rgba color parser #172

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

themojache
Copy link

@themojache themojache commented Apr 13, 2024

https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb#values
https://developer.mozilla.org/en-US/docs/Web/CSS/alpha-value

Both rgb and rgba contain the optional parameter of alpha despite naming convention. R, G, B are required; A is optional for both.

Test cases: ["rgb(30% 20% 50%)","rgb(153,255,153,0.5)","rgb(253, 255, 153)","rgb(255 255 255 / 50.75%)","rgba(255, 255, 255 / 50%)","rgb( 255, 255, 255, none )","rgba(none, 255, 255 / none)"]

Remains unsupported: Relative value syntax (rgb(from R G B[ / A]))

It seems like Math.round is used where you use Math.floor in browser implementations of handling percent byte rgb values (see MDN's test case of the first test case).

Thanks for the work in maintaining this library. Hope a few touch-ups are appreciated. Reach out/add commits if you notice any impactful bugs. I focused on making the parser more flexible and reducing code (for individual cases). ✌️

Alternative: #171

Made rgb/rgba more spec compliant:
https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb#values
https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgba#values
https://developer.mozilla.org/en-US/docs/Web/CSS/alpha-value

Both rgb and rgba contain the optional parameter of alpha despite naming convention. R, G, B are required; A is optional for both.

Test cases: ["rgb(153,255,153,0.5)","rgb(253, 255, 153)","rgb(255 255 255 / 50.75%)","rgba(255, 255, 255 / 50%)","rgb(255, 255, 255, none)","rgba(none, 255, 255 / none)"]

Changes (Line references use my changed code):
- Reduce processing, one regex for both cases. [Line 32]
- Augment to allow flexibility with parts.length being either 3 or 4. [Line 84, 306-323]
- Simplify logic into a longer ternary operator rather than 3 cases. [Line 83-86]
- Augment split to take several white space, comma, or / delimiters as per spec. [Line 82, 306]
- Alpha can now be a %.
- Added support for "none" keyword for a rgba value.
- Simplified handling of RGB into a single map operation.
- Refactored so rgba prefix would depend on the array length.
- Moved rgbdelimiters to a variable near colorRegEx2.

Still missing: Relative value syntax (rgb(from <color> R G B[ / A]))
This reverts commit 5904e91.
…s far as I can see implementation wise in browsers
Moved / from rgba to simply the alpha case. More spec compliant for a minor change in the regex.
Expanded the colorRegEx1 to capture the hex color string and have case insensitive matching.
Updated the hex parsing to use the regex match group. Logic is mostly the same, I added a check to see if full alpha before adding the alpha value to the list.
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.

None yet

1 participant