fix(hex2rgb): preserve full alpha precision when parsing hex colors - #392
Open
spokodev wants to merge 2 commits into
Open
fix(hex2rgb): preserve full alpha precision when parsing hex colors#392spokodev wants to merge 2 commits into
spokodev wants to merge 2 commits into
Conversation
hex2rgb rounded the parsed alpha of #rrggbbaa / #rgba colors to two
decimals, so alpha bytes did not round-trip and the (exactly parsed)
RGB channels disagreed with alpha:
chroma('#ff000088').alpha() // 0.53, exact is 136/255 = 0.5333…
chroma('#ff000088').hex() // '#ff000087', not '#ff000088'
Parse alpha as the exact byte / 255, matching the RGB channels. This also
brings the APCA contrast of colors with alpha in line with the APCA demo
values the test already noted (36.7, -68.6), so that todo is resolved.
Updated the hex2rgb / alpha / contrast fixtures that encoded the rounded
values; full suite green (2519).
🦋 Changeset detectedLatest commit: eb4d107 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When parsing
#rrggbbaa/#rgbacolors,hex2rgbrounds the alpha channel to two decimals:The RGB channels are parsed exactly (
& 0xff), so only alpha is degraded. As a result alpha bytes do not round-trip and the channels disagree:136 of 256 alpha bytes fail to round-trip. CSS Color 4 §5.2 defines the hex alpha as
aa / 255exactly.The fix parses alpha as the exact
(u & 0xff) / 0xff, matching how the RGB channels are handled. As a side effect it also aligns the APCA contrast of colors with alpha with the values the contrast test already noted in comments (// 36.7,// -68.6), so thattodois removed. Thehex2rgb,alpha, andcontrastfixtures that encoded the rounded values are updated. Full suite passes (2519).