Skip to content

Gamut mappers clamp lightness to the brightest *neutral*, losing reachable luminance at the hull boundary #4245

Closed

Description

@zackees

Found while completing P7's (#4041) acceptance-criterion coverage for the
white-emitter paths. The three mappers share a fallback shape:

i32 lightness = lab[0];
if (lightness > map.max_neutral_lightness) {
    lightness = map.max_neutral_lightness;   // <-- here
}
// ... then compress chroma at that lightness

max_neutral_lightness is, by construction and by its own docstring, "OKLab
lightness of the brightest D65 neutral this device can reach"
. The fallback
applies it to every out-of-gamut target, including ones whose lightness the
device can reach at their own chroma.

Measured

On the corpus device (rgbDevice() + D65 white at unit luminance), walking a
saturated ramp out of the four-emitter hull:

max_neutral_lightness 87,723
target lightness at the last in-gamut step 98,783

So the device reproduces a colour at lightness 98,783 exactly — the allocation
succeeds — while the cap sits 11,060 (11%) below it. One step later the target
falls outside the hull, the clamp fires, and the output drops:

step 234 (in gamut)     produced Y = 3.225
step 235 (just outside) produced Y = 2.240

A single input step of ~0.02 in Y produces a 0.985 jump in Y and 1.44 in
another component — roughly 30% of the luminance, discarded at the boundary.
An animation walking through that boundary shows a visible seam.

The same ramp also pushes hue divergence to 0.03, against 0.02 elsewhere, which
looks like the same cause: the compression happens at a lightness the target
never asked for.

Why a saturated colour can out-shine the brightest neutral here

Because the cap is a bound on neutrals, not on lightness. A neutral needs
every channel in a fixed ratio, so it saturates on whichever channel runs out
first; a colour that leans on the channels with headroom can reach a higher
OKLab L. The bound is correct for what it is and is being asked a question it
does not answer.

Scope

The clamp is identical in all three builds — mapAndSolveDrivesQ16,
mapAndAllocateRgbwQ16, mapAndAllocateRgbwwQ16 — so all three have it. Only
the RGBW ramp above happens to expose it; the RGBWW path scores 0.025 on the
same continuity test because its cap (98,293) sits above where its ramp crosses.
That is luck, not correctness.

What a fix probably looks like

Do not clamp first. Try compressing chroma at the target's own lightness,
and only lower the lightness if no chroma at that lightness is feasible. That
keeps the halving search's cost the same in the common case, but it changes the
mapping for every out-of-gamut target, so it wants its own before/after
measurement against the P5 reference rather than being folded into a test PR.

Not filed as a failing test

The two tests that expose this — RGBW continuity and RGBW hue preservation —
are deliberately not in the PR that found it, because they would land red.
They are reproduced above in full detail so whoever takes this can re-create
them in one step.

Related: #4041, #4034.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9

Activity

  1. zackees commented on Sep 9, 2026

    @zackees
    MemberAuthor

    The fix I proposed does not work, and here is why

    I suggested: "Do not clamp first. Try compressing chroma at the target's own
    lightness, and only lower the lightness if no chroma at that lightness is
    feasible."
    I implemented that across all three mappers and measured it.

    It changes nothing. Same ramp, same step, byte-identical output:

    step 235   Y 3.225 -> 2.240      target L 98,912   cap 87,723
    

    The reason is that factor == 0 at the target's own lightness, so the retry
    falls straight back to the cap and reproduces the old behaviour exactly.

    Why chroma compression cannot work above the cap

    largestFeasibleChroma brackets [0, full] and keeps the largest feasible
    factor, which assumes feasibility decreases as chroma grows — true below the
    neutral cap, where the zero-chroma end (the neutral at that lightness) is
    reachable and anchors the search.

    Above the cap that anchor is gone. The neutral at L = 98,912 is by definition
    infeasible, because the cap is the brightest reachable neutral. So scaling
    chroma toward zero moves the candidate toward the infeasible neutral axis
    and away from the hull. Feasibility increases with chroma there, the
    bisection's invariant is inverted, and it converges to 0.

    The existing comment says this, and it is correct:

    Lightness first. Reducing chroma cannot bring an over-bright target back into
    the hull — at zero chroma it is still outside — so without this the halvings
    below converge on an infeasible answer.

    So the clamp is not a shortcut someone took; it is what makes the search valid.
    The discontinuity is a property of "clamp lightness to the neutral cap, then
    compress chroma"
    as a strategy, not of a bug within it.

    What a real fix needs

    Something that does not reduce to a 1-D chroma search at fixed lightness:

    • search lightness and chroma together — the feasible set is 2-D and the current
      code walks one axis of it; or
    • line-search from the target toward a point known to be inside (the brightest
      reachable neutral, say) and stop at the hull, which keeps a valid bracket
      because one end is feasible by construction and the other is not.

    The second is the standard formulation and it would be monotonic by
    construction, so the existing halving machinery applies unchanged. It changes
    the mapped result for every out-of-gamut target, so it wants a before/after
    against the P5 reference and the corpus.

    I have reverted my attempt rather than leave a second halving loop that costs
    8 extra feasibility tests per over-bright pixel and produces the same answer.

    Updating the issue so the next attempt does not repeat this.

  2. zackees commented on Sep 10, 2026

    @zackees
    MemberAuthor

    Numbers for the wide hulls, from the #4041 connectivity work

    Measuring this on white-emitter devices while closing a different P7 gap (#4250). It does not fix the issue, but it sizes it, and the size is larger than the three-emitter case suggested.

    The clamp targets the brightest reachable neutral. The hull reaches higher than that off the neutral axis, and here is how much higher:

    device brightest neutral brightest reachable at headroom
    rgbw 1.338544 1.623559 hue 300, C 0.48 +21.3%
    non_d65_white 1.333993 1.633584 hue 300, C 0.50 +22.5%
    rgbww 1.499835 1.748872 hue 300, C 0.46 +16.6%
    rgbww_two_white 1.152403 1.167419 hue 0, C 0.02 +1.3%

    So on a four-emitter device up to 22% of reachable OKLab lightness is discarded before chroma compression even starts.

    The spread across devices is itself informative. rgbww_two_white shows only 1.3% — its primaries carry 0.22/0.60/0.08 rather than unit capacity, so it can barely exceed the white it renders and the hull is nearly flat-topped. The unit-capacity devices are five times brighter than their rendering white, and that is exactly where the headroom lives. A device with a bright white emitter loses the most, which is the opposite of the intuition that a white emitter makes neutrals the easy case.

    This showed up as a test failure, which is worth recording

    Writing an over-bright case for the wide mapper, the natural assertion was that a target above the neutral cap is out of gamut. It is not:

    rgbw  cap = 1.338544
      L = 1.405472, C = 0.20  ->  feasible, mapper returns it unchanged
    

    The mapper's own early-out accepts it. So "above the neutral cap" and "outside the hull" are genuinely different sets, and any test constructing over-bright targets has to screen on feasibility rather than assume. The test in #4250 does.

    Still not a fix, and still for the same reason

    Nothing here disturbs the conclusion I posted above: the clamp is what makes the chroma bisection valid, because above the cap the zero-chroma anchor is infeasible and the search's monotonicity invariant inverts. Removing it without replacing the search is what I measured failing.

    What these numbers do change is the priority. A line-search formulation is worth up to a fifth of the reachable lightness on ordinary RGBW parts, not a rounding error.

    One constraint for whoever takes it: the replacement has to keep the ray-connectivity property that #4250 just measured across 7.5 million evaluations, since that is what licenses a fixed-halving search in the first place.

  3. added a commit that references this issue on Sep 10, 2026
  4. zackees commented on Sep 10, 2026

    @zackees
    MemberAuthor

    Correction to the denominator in my comment above

    Review on #4250 caught that I quoted the headroom with the wrong fraction's wording. Correcting it here rather than leaving it standing on the issue, since these numbers are the argument for prioritising this.

    I wrote "22% of reachable OKLab lightness is discarded". The 22.5% figure is the gain relative to the neutral cap — what the clamp gives up as a fraction of what it keeps. Measured against the reachable maximum, the same interval is smaller:

    device headroom above cap share of reachable range
    rgbw +21.3% 17.6%
    non_d65_white +22.5% 18.3%
    rgbww +16.6% 14.2%
    rgbww_two_white +1.3% 1.3%

    Both are meaningful and neither substitutes for the other, so the report now carries both columns.

    The conclusion is unchanged — roughly a sixth to a fifth of the reachable range is unavailable on ordinary RGBW parts, whichever way you frame it, and that is still far from a rounding error. But "of reachable lightness" specifically meant the second column, and 18.3% is the number that phrase should have carried.

    Everything else in that comment stands, including the observation that a target above the neutral cap can be exactly reachable (rgbw at L = 1.405, C = 0.20), which is what makes this a real defect rather than a conservative bound.

  5. zackees commented on Sep 10, 2026

    @zackees
    MemberAuthor

    Solved: the feasible chroma above the cap is still one interval — it just stops containing zero

    I said above that this "needs a line-search formulation". I tried that, and it fails too. What was actually missing was the shape of the feasible set, and once measured the algorithm follows. #4261.

    The structure

    Sampled along fixed-lightness, fixed-hue rays on the three-emitter device, neutral cap L = 1.118228:

    L hue 30 hue 120 hue 300
    1.0623 (below cap) [0.0000, 0.4215] [0.0000, 0.1913] [0.0000, 0.5670]
    1.1741 (above) [0.0795, 0.3195] (none) [0.1275, 0.6000]
    1.3978 (above) (none) (none) [0.4432, 0.6000]

    Connected throughout — so a search is possible. Not anchored at zero above the cap — so the shipped bracket [0, C] starts with an infeasible low end, its invariant is inverted, and it converges on zero.

    That is exactly the failure my earlier comment described. What I got wrong was concluding the clamp was therefore the only remedy. The bisection is not broken above the cap; only its bracket is.

    The algorithm that follows

    Probe for a feasible chroma, then bisect both edges, and clamp the target's chroma into [Cmin, Cmax] at its own lightness.

    target shipped L, C interval clamp L, C ΔL
    L 1.286, hue 300, C 0.30 1.1182, 0.2988 1.2860, 0.3081 +0.168
    L 1.398, hue 300, C 0.30 1.1182, 0.2988 1.3978, 0.4431 +0.280
    L 1.398, hue 120, C 0.50 1.1182, 0.0000 1.1182, 0.0000 0.000

    Never worse — it falls back to the shipped path when no chroma is feasible at that lightness. Hue preserved exactly. Every result feasible. Cost probes + 2 × halvings, all compile-time constants, so A3/B11 are satisfied.

    The lower edge is not optional: clamping to the upper edge alone returns infeasible output on exactly the targets this fixes.

    Correcting my own headroom framing once more

    The 29.7% figure here is for the three-emitter device and is headroom above the cap — brightest reachable 1.450614 against a cap of 1.118228. Consistent with the wide-hull numbers I posted earlier, and stated with its denominator this time.

    The line search, for the record

    It looked right: the segment from the neutral-at-cap to the target preserves hue exactly, because (a, b) scales linearly so the direction is constant. It fails because the anchor sits on the hull boundary — the segment leaves immediately, the largest feasible step is tiny, and chroma collapses to near zero. Measured worse than the shipped path on most targets. Both refuted shapes now have tests that reproduce their failure, so neither gets tried a third time.

    What is left

    This is host-side. The embedded mapper still clamps, and converting it means a probe loop plus a second bisection in gamut_map.cpp.hpp for rgb/rgbw/rgbww, with the s16.16 accuracy work that implies. The algorithm choice is settled; the implementation is not.

  6. zackees commented on Sep 10, 2026

    @zackees
    MemberAuthor

    The embedded mapper now keeps the lightness too (#4271)

    Your last comment settled the algorithm and left the implementation:

    This is host-side. The embedded mapper still clamps, and converting it means a probe loop plus a second bisection in gamut_map.cpp.hpp for rgb/rgbw/rgbww, with the s16.16 accuracy work that implies.

    Done, for all three, in Q16.

    Re-rendered through the emitters rather than read back off the mapper — what matters is what the device emits, and a mapper that reported a lightness it did not produce would pass a test built on its own arithmetic:

    target shipped now
    L 1.286, hue 300, C 0.30 1.1182, 0.2988 1.2860, 0.3081
    L 1.398, hue 300, C 0.30 1.1182, 0.2988 1.3978, 0.4431
    L 1.398, hue 120, C 0.50 1.1182, 0.0000 1.1182, 0.0000 (no seed)

    Matching the study's numbers to the hundredth, including the fallback row.

    Two things worth recording

    The probe ceiling is relative, not absolute. The study probes to an absolute PROBE_CEILING = 0.8; the embedded version works in factors of the request and reaches 4x it. That is not just a convenience for staying in factor space — a bright near-neutral is infeasible because it is not saturated enough, and an absolute ceiling answers it with a saturated colour. A relative ceiling keeps a near-neutral request near-neutral and falls back to the clamp instead. Both reproduce your table; they differ only on targets your table does not contain, and I would rather that difference be a decision than an accident.

    An over-bright grey still clamps, since it has no chroma to scale, and there is a case pinning that. The existing over-bright-neutral test checks drive ratios, so it would have passed just as happily if the change had started turning every over-bright grey into a colour.

    Cost

    kGamutMapProbes + 2 * kGamutMapHalvings feasibility tests, on the above-cap path only and fixed at build time, so A3/B11 hold. Targets at or below the cap never enter it, so in-gamut and below-cap output is untouched and the existing suite passes unchanged.

  7. moved this from Triage to Done in FastLED Trackeron Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions