Repository navigation
fix(color): stop rgb2hsv_approximate wrapping orange into green (#436) - #3803
Merged
Merged
Conversation
The Orange->Yellow branch computed:
h += scale8( qsub8((g - 85) + (171 - r), 4), FIXFRAC8(32,85));
Reaching that branch requires g >= r/2, and r is the largest channel, so
r > 171 is the ordinary case there -- which makes (171 - r) negative. The
negative sum was then truncated to u8 by qsub8() and wrapped to a large
value, pushing the hue far past yellow and into the greens.
rgb(255,153,0) returned hue 122. Sweeping green up at full red showed the
window plainly:
g=120 -> 46 g=135 -> 115 g=180 -> 35
g=150 -> 121
g=165 -> 126
(255 - r) measures the same "how far has r fallen" quantity but cannot go
negative, and over this branch's own entry condition the sum
(g - 85) + (255 - r) has a minimum near 42 -- so the wrap becomes
structurally impossible rather than merely unlikely. The span widens from
~85 to ~170, hence FIXFRAC8(32,170) to keep the top of the range on
HUE_YELLOW.
That sweep now reads 46, 41, 44, 47, 50, ... 64.
Note that the correct answer here is ~45, not the ~26 reported in the
thread. rgb2hsv_approximate inverts hsv2rgb_rainbow, not a textbook HSV
hexagon, so the standard-HSV hue that paint.net and 5chmidti's
rgb2hsv_precise report is a different mapping. Round-tripping confirms
45 is right: rgb(255,153,0) -> hue 45 -> rgb(171,119,0), preserving the
r>g>b ordering and the 0.60 g/r ratio.
Whole-cube round-trip deviation improves from 78.05 to 77.48 average
(median 62.01 -> 61.93); the aggregate moves little because the broken
window is a narrow slice of the sampled cube, but colors inside it go
from unusable to correct.
Adds regression tests to tests/fl/hsv2rgb_accuracy.cpp covering all four
colors reported on the issue, the sweep, and grey handling.
KNOWN LIMITATION, documented in the test: a ~5 unit backwards step
remains at g=128 where the Red-Orange and Orange-Yellow branches meet.
The boundary sits at g = r/2, which is not HUE_ORANGE, so the branches
are calibrated against different endpoints. Closing that needs both
branches recalibrated together and is left for follow-up.
Refs #436
Co-Authored-By: Claude <[email protected]>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthrough
ChangesHSV hue correction
Estimated code review effort: 2 (Simple) | ~10 minutes ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Closes #436. Open 8 years, reported independently by 6 people.
Root cause
The Orange→Yellow branch of
rgb2hsv_approximate():Reaching this branch requires
g >= r/2, andris the largest channel — sor > 171is the ordinary case here, making(171 - r)negative. The negative sum is then truncated tou8byqsub8()and wraps to a large value, pushing hue past yellow and into the greens.For
rgb(255,153,0):68 + (−84) = −16→ wraps to236→ hue 122.Sweeping green up at full red shows the window exactly:
Fix
(255 - r)measures the same "how far has r fallen" quantity but cannot go negative. Over this branch's own entry condition,(g - 85) + (255 - r)has a minimum near 42 — the wrap becomes structurally impossible rather than merely unlikely. The span widens from ~85 to ~170, henceFIXFRAC8(32,170)to keep the top of the range onHUE_YELLOW.Sweep now reads
46, 41, 44, 47, 50, 53, 56, 59, 61, 64.The expected value in the thread is wrong (worth reading)
Everyone compared against ~26, from paint.net reporting
hsv(36°,100,100). Butrgb2hsv_approximateinvertshsv2rgb_rainbow, not a textbook HSV hexagon — FastLED's rainbow hue is a perceptual mapping whereHUE_ORANGE = 32andHUE_YELLOW = 64. The standard-HSV hue that paint.net and @5chmidti'srgb2hsv_precisereport is simply a different scale.So correctness is round-trip fidelity, which confirms 45 is right:
r > g > bpreserved, g/r ratio 0.60 in and 0.60 out. That's orange. (The absolute value drop is rainbow mode's own r-channel ceiling in this band, not part of this bug.)Whole-cube round-trip deviation: 78.05 → 77.48 average, 62.01 → 61.93 median. The aggregate barely moves because the broken window is a narrow slice of the 4096-sample cube — but colors inside it go from unusable to correct.
Tests
Added to
tests/fl/hsv2rgb_accuracy.cpp(which already exercised this function): all four colors reported in the thread, the red→yellow sweep, and grey handling. Verified failing before the fix and passing after.Known limitation, documented in the test
A ~5 unit backwards step remains at g=128, where the Red-Orange and Orange-Yellow branches meet. The boundary sits at
g = r/2, which does not correspond toHUE_ORANGE, so the two branches are calibrated against different endpoints. Closing that seam means recalibrating both branches together rather than fixing one expression — deliberately left for follow-up. A 5-unit seam is not visible where a 70-unit jump into green very much was.The
// Examples that need work:list at the bottom ofhsv2rgb.cpp.hppis also still unaddressed.Verification
bash test --cpp --clean— 357/357 on a full clean rebuildbash lint— cleanCredit
@srwi for the original report with a reproducer; @PhotonicVelocity for the palette table that showed it was a band rather than a one-off; @5chmidti for the independent analysis and benchmarks; @cad435, @DrJaymz, @fabianoriccardi, @mike2003, @Modifikator for confirmations over the years.
🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests