Skip to content

fix(color): stop rgb2hsv_approximate wrapping orange into green (#436) - #3803

Merged
zackees merged 1 commit into
masterfrom
fix/issue-436-rgb2hsv-orange-underflow
Aug 1, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/issue-436-rgb2hsv-orange-underflow

Conversation

@zackees

@zackees zackees commented Aug 1, 2026 •

Copy link
Copy Markdown
Member

Closes #436. Open 8 years, reported independently by 6 people.

Root cause

The Orange→Yellow branch of rgb2hsv_approximate():

h += scale8( qsub8((g - 85) + (171 - r), 4), FIXFRAC8(32,85));

Reaching this branch requires g >= r/2, and r is the largest channel — so r > 171 is the ordinary case here, making (171 - r) negative. The negative sum is then truncated to u8 by qsub8() and wraps to a large value, pushing hue past yellow and into the greens.

For rgb(255,153,0): 68 + (−84) = −16 → wraps to 236 → hue 122.

Sweeping green up at full red shows the window exactly:

g 105 120 135 150 165 180 195
hue 40 46 115 121 126 35 41

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, hence FIXFRAC8(32,170) to keep the top of the range on HUE_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). But rgb2hsv_approximate inverts hsv2rgb_rainbow, not a textbook HSV hexagon — FastLED's rainbow hue is a perceptual mapping where HUE_ORANGE = 32 and HUE_YELLOW = 64. The standard-HSV hue that paint.net and @5chmidti's rgb2hsv_precise report is simply a different scale.

So correctness is round-trip fidelity, which confirms 45 is right:

rgb(255,153,0) -> hue 45 -> rgb(171,119,0)

r > g > b preserved, 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 to HUE_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 of hsv2rgb.cpp.hpp is also still unaddressed.

Verification

  • bash test --cpp --clean — 357/357 on a full clean rebuild
  • bash lint — clean

Credit

@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

    • Improved color conversion for orange and yellow tones.
    • Prevented certain orange colors from being incorrectly classified as green.
    • Improved hue progression and grayscale handling for more accurate results.
  • Tests

    • Added coverage for orange-to-yellow conversion, hue progression, round-trip accuracy, and grayscale colors.

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]>
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Aug 1, 2026
@zackees
zackees merged commit 3c425eb into master Aug 1, 2026
10 of 12 checks passed
@coderabbitai

coderabbitai Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: ac5b6cef-ca82-462a-8e3f-b83b8411d7ff

📥 Commits

Reviewing files that changed from the base of the PR and between 02d3e4c and 07b3ef7.

📒 Files selected for processing (2)
  • src/hsv2rgb.cpp.hpp
  • tests/fl/hsv2rgb_accuracy.cpp

📝 Walkthrough

Walkthrough

rgb2hsv_approximate now calculates orange-to-yellow hues without unsigned underflow. Tests cover orange regression cases, hue progression, rainbow round-trips, and grayscale saturation.

Changes

HSV hue correction

Layer / File(s) Summary
Correct orange-to-yellow hue calculation
src/hsv2rgb.cpp.hpp
The hue adjustment uses (255 - r) and rescales the adjustment from 32/85 to 32/170.
Validate hue conversion behavior
tests/fl/hsv2rgb_accuracy.cpp
Regression tests verify orange hues, red-to-yellow progression, rainbow channel ordering, and zero saturation for grayscale inputs.

Estimated code review effort: 2 (Simple) | ~10 minutes

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-436-rgb2hsv-orange-underflow

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@zackees
zackees deleted the fix/issue-436-rgb2hsv-orange-underflow branch August 1, 2026 05:14
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Aug 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

rgb2hsv_approximate returns completely wrong color

1 participant