Skip to content

fix(hcg): return chroma/grayness on [0..1] so rgb2hcg round-trips (#250) - #393

Open
chatman-media wants to merge 2 commits into
gka:mainfrom
chatman-media:fix/rgb2hcg-range
Open

fix(hcg): return chroma/grayness on [0..1] so rgb2hcg round-trips (#250)#393
chatman-media wants to merge 2 commits into
gka:mainfrom
chatman-media:fix/rgb2hcg-range

Conversation

@chatman-media

Copy link
Copy Markdown

Closes #250.

rgb2hcg returned chroma and grayness on a [0..100] scale (and computed grayness with a mismatched 255 - delta denominator), while hcg2rgb and the existing hcg2rgb tests treat both channels as [0..1]. So rgb -> hcg -> rgb did not round-trip — e.g. rgb2hcg([255,0,0]) returned [0, 100, NaN] instead of [0, 1, 0], and the breakage was most visible when all three channels were non-zero.

Fix: normalize RGB to [0..1], return delta as chroma and min / (1 - delta) as grayness (0 when delta === 1, where min is necessarily 0, avoiding a 0/0 NaN). The new cases are the exact inverse of the existing hcg2rgb test cases, so the two functions now agree.

  • new test/io/rgb2hcg.test.js: per-color conversions + arbitrary-RGB round-trip; fails without the fix (25/28), passes with it
  • full suite: 2547 passing; prettier + eslint clean

rgb2hcg scaled chroma and grayness to [0..100] and computed grayness with
a mismatched denominator, while hcg2rgb (and the existing tests) treat both
channels as [0..1]. As a result rgb -> hcg -> rgb did not round-trip,
notably when all three channels were non-zero (gka#250).

Normalize RGB to [0..1], return delta as chroma and min/(1-delta) as
grayness (0 when delta is 1, where min is necessarily 0).
@chatman-media
chatman-media requested a review from gka as a code owner June 26, 2026 12:33
@changeset-bot

changeset-bot Bot commented Jun 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4bb76cb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
chroma-js Patch

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

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.

rgb2hcg produces out of range values

1 participant