Repository navigation
fix(conversion): round exact halves up in hslToRgb and hsvToRgb - #275
Merged
Merged
Conversation
A channel that is exactly half, like 127.5, came out of the two conversions as 127.49999999999999 and was rounded down. So hsl(60, 100%, 25%), the CSS color olive, was #7f8000, teal was #007f80 and purple was #80007f, and spin(-90) of red was #7f00ff while spin(90) is #80ff00. The three channels are rounded to 9 decimals before they are returned, which removes the noise. The values in the tests and the README that were rounded down halves are updated, and the toRgbRatio test uses a color whose red has a fraction without relying on the noise.
|
@simonkundrik is attempting to deploy a commit to the Scott's projects Team on Vercel. A member of the Team first needs to authorize it. |
cmykToRgb had the same float noise as hsl and hsv, cmyk(0, 0, 0, 90) came out as 25.499999999999993 and rounded down to 25 instead of 26. Runs the channels through removeNoise like the other two and adds cmyk to the exact halves tests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
hsl(60, 100%, 25%)is the CSS colorolive, but it comes out as#7f8000, not#808000:In these the channel is exactly 127.5 (or 25.5), and the conversion gives 127.49999999999999, so
Math.roundgoes down. For hue 60 inhslToRgb,h + 1/3is exactly0.5, sot < 1 / 2is false andhue2rgbtakes the next branch, which gives0.4999999999999999instead of0.5.hsvToRgbhas the same noise (hsv(0, 80%, 50%)gave 25 for green and blue, 26 is right).hslToRgbandhsvToRgbround the three channels to 9 decimals before they return them (removeNoiseinutil.ts), so a half stays a half.Checks:
test/exact-halves.spec.ts: the three named colors, some hand picked rows, and everyhsl()andhsv()of whole percents at the hues 0, 30, ... 330 (122,412 each) against the conversion in integer arithmetic with halves rounded up. 16 of its 17 tests fail on masterhsl()(3,672,360 inputs) in Chrome 156 and compared withgetComputedStyle. The oldhslToRgbdiffered from Chrome on 3,772 of them and the fixed one on 158. Those 158 are where Chrome's own float math lands on the other side of an exact half (hsl(10, 60%, 50%)is exactly 76.5 for green, Chrome gives 76). The fixed code equals the integer conversion on all of them, and the same holds forhsv()against its integer conversion#7f00ffis#8000fffortetrad(),polyad(4),spin(-90)and the hue 270 rows of the percentage hue tests (and in the README),DESATURATIONS[20]ise61a1aandLIGHTENS[45]isffe6e6. ThetoRgbRatiotest usedhsl(210, 50%, 40%), whose red only had a fraction because of the noise, so it useshsl(210, 50%, 41%)noweslintandvitest runpass (222 tests), and so doestsc -p tsconfig.build.json --noEmitNote
Medium Risk
Changes core color conversion outputs for HSL/HSV/CMYK inputs, which may alter hex/RGB results for downstream users but aligns behavior with CSS/browser half-up rounding.
Overview
Fixes floating-point noise in HSL, HSV, and CMYK→RGB conversions where channels that are mathematically exact halves (e.g. 127.5) were computed as
127.499999…and then rounded down instead of up.Adds
removeNoiseinutil.tsand applies it to RGB channel outputs inhslToRgb,hsvToRgb, andcmykToRgb. Named colors likehsl(60, 100%, 25%)now match olive (#808000), and derived APIs (spin,tetrad,polyad, hue 270 cases) shift from#7f00ffto#8000ffwhere appropriate.Adds
test/exact-halves.spec.tswith integer-arithmetic reference conversions across whole-percent HSL/HSV/CMYK grids, and updates README examples plus existing test expectations and modification snapshots for the corrected rounding.Reviewed by Cursor Bugbot for commit 1089253. Bugbot is set up for automated code reviews on this repo. Configure here.