Skip to content

fix(conversion): round exact halves up in hslToRgb and hsvToRgb - #275

Merged
scttcper merged 2 commits into
scttcper:masterfrom
simonkundrik:fix/round-exact-halves
Nov 14, 2026
Merged

scttcper merged 2 commits into
scttcper:masterfrom
simonkundrik:fix/round-exact-halves

Conversation

@simonkundrik

@simonkundrik simonkundrik commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

hsl(60, 100%, 25%) is the CSS color olive, but it comes out as #7f8000, not #808000:

new TinyColor('hsl(60, 100%, 25%)').toHexString(); // "#7f8000", should be "#808000" (olive)
new TinyColor('hsl(180, 100%, 25%)').toHexString(); // "#007f80", should be "#008080" (teal)
new TinyColor('hsl(300, 100%, 25%)').toHexString(); // "#80007f", should be "#800080" (purple)
new TinyColor('hsl(0, 50%, 20%)').toRgb(); // g and b are 25, browsers give 26
new TinyColor('#f00').spin(-90).toHexString(); // "#7f00ff", but spin(90) is "#80ff00"

In these the channel is exactly 127.5 (or 25.5), and the conversion gives 127.49999999999999, so Math.round goes down. For hue 60 in hslToRgb, h + 1/3 is exactly 0.5, so t < 1 / 2 is false and hue2rgb takes the next branch, which gives 0.4999999999999999 instead of 0.5. hsvToRgb has the same noise (hsv(0, 80%, 50%) gave 25 for green and blue, 26 is right).

hslToRgb and hsvToRgb round the three channels to 9 decimals before they return them (removeNoise in util.ts), so a half stays a half.

Checks:

  • new test/exact-halves.spec.ts: the three named colors, some hand picked rows, and every hsl() and hsv() 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 master
  • I ran every integer hue of hsl() (3,672,360 inputs) in Chrome 156 and compared with getComputedStyle. The old hslToRgb differed 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 for hsv() against its integer conversion
  • pinned values that were rounded down halves are updated: #7f00ff is #8000ff for tetrad(), polyad(4), spin(-90) and the hue 270 rows of the percentage hue tests (and in the README), DESATURATIONS[20] is e61a1a and LIGHTENS[45] is ffe6e6. The toRgbRatio test used hsl(210, 50%, 40%), whose red only had a fraction because of the noise, so it uses hsl(210, 50%, 41%) now
  • eslint and vitest run pass (222 tests), and so does tsc -p tsconfig.build.json --noEmit

Note

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 removeNoise in util.ts and applies it to RGB channel outputs in hslToRgb, hsvToRgb, and cmykToRgb. Named colors like hsl(60, 100%, 25%) now match olive (#808000), and derived APIs (spin, tetrad, polyad, hue 270 cases) shift from #7f00ff to #8000ff where appropriate.

Adds test/exact-halves.spec.ts with 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.

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.
@vercel

vercel Bot commented Nov 13, 2026

Copy link
Copy Markdown

@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]>
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.

2 participants