fix(hcg): wrap negative hue into [0, 360) for rgb -> hcg#68
Open
spokodev wants to merge 1 commit into
Open
Conversation
When red is the maximum channel and blue > green, the red sextant branch `((g - b) / chroma % 6)` is negative, and the final `hue % 1` keeps the sign (JavaScript modulo is sign-preserving). The result is a negative hue that falls outside hcg's declared [0, 360) range and breaks the rgb -> hcg -> rgb round-trip (e.g. rgb [255, 0, 55] returned hue -12.94 and round-tripped to a blue channel of 310). Add the same negative-hue wrap already used by rgb -> hsv and rgb -> hsl.
Member
|
Thanks @spokodev — this is a real bug and a clean fix. v3 already lands the identical fix: |
Open
dy
added a commit
that referenced
this pull request
Jul 2, 2026
… not -13) Ports the targeted assertion from #68 (spokodev). v3's hcg.js already wraps negative hues; this pins it so it can't regress.
Author
|
Thanks, glad it helped, and thanks for porting the test into v3 with credit. Fine to close this once v3 lands. |
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.
Problem
rgb.hcgcan return a hue outside its declared[0, 360)range. When red is the maximum channel and blue > green, the red sextant branch computes a negative value:hcgdeclaresmin: [0, 0, 0],max: [360, 100, 100], so a negative hue violates the space's own range, and it breaks thergb -> hcg -> rgbround-trip:The sibling spaces agree the hue should be ~347:
rgb.hsi([255, 0, 55])is348.17andrgb.hsp([255, 0, 55])is347. Onlyhcgreturns a negative.This affects the whole red-dominant, blue > green region of the RGB cube.
Fix
Add the same negative-hue wrap that
rgb.hsvandrgb.hslalready use (if (h < 0) { h += ... }), applied here in the fractional domain before scaling to degrees:Tests
Extended the existing
hcg: rgb -> hcgtest with the red-dominant/blue > green case and a round-trip assertion. Both fail onmasterand pass with the fix. Full suite:1353 pass,0 fail.