Skip to content

allocateEmitterDrivesQ16 sizes its feasibility slack in drive space, and pays it in colour #4303

Closed

Description

@zackees

Found while closing the last open question on P7 (#4041); the measurement is in #4304.

The defect

allocateEmitterDrivesQ16 decides whether a target is reachable, and allows each RGB drive to fall kWhiteSlack = 64 raw s16.16 units outside [0, 1] before refusing:

const i32 drive = at_zero[i] - scaleWhiteQ16(allocation.per_white[i], level);
if (drive < -kWhiteSlack || drive > kWhiteFullDrive + kWhiteSlack) {
    return false;
}
rgb[i] = drive < 0 ? 0 : (drive > kWhiteFullDrive ? kWhiteFullDrive : drive);

A drive inside the slack is accepted and clamped. The function returns true — the target is reported reachable — and hands back an allocation that reproduces a different colour.

The constant's own comment sizes it:

64 raw units is a quarter of one code at 8-bit output.

That is true in drive space. The cost is paid in colour space, and the exchange rate is the primary's XYZ column, which is not near unity for a saturated primary.

Measured

Test device, blue primary at xy = (0.15, 0.06) normalised to Y = 1, so xyY_to_XYZ gives it a Z column of 13.17: one drive unit of blue is thirteen units of Z.

Target (0.55, 0.41) at Y = 0.5 — inside the four-emitter hull. solveRgbDrivesQ16 returns blue = -62 raw, inside the slack:

reproduced from XYZ error OKLab hue divergence
the solve's drives (blue = -62) 5.3e-05 0
what the allocator returns (blue = 0) 0.0158 0.026

0.0125 of that is Z alone, against a target Z of 0.0488 — a 26% error in the smallest component, and about 1.5° of OKLab hue.

Extent

Over the 291 in-gamut targets on a coarse grid of the xy plane at Y = 0.5:

worst hue divergence 0.0197 at (0.56, 0.40)
median 6.9e-05
above 0.005 6 of 291
above 0.010 2 of 291

So the allocator is accurate nearly everywhere and wrong in a small orange-yellow region — the region where blue solves just below zero. It is not pervasive, and it is not a coincidence either.

Why the slack cannot simply be removed

It is there for a real reason, stated at its definition: a colour the device reproduces exactly does not solve to exactly 0 or 1. The cited case is corpus target (4, 1, 19) solving to (0, -1, 1) — a one-unit rounding artefact. The constant is 64 times that.

So "delete the slack" would start rejecting reachable targets, and "shrink it to 2" is a guess at where rounding stops and real infeasibility starts.

Directions

  1. Size it in colour space. Accept the clamp only when the XYZ error it induces is below a threshold. Principled and directly checkable, but WhiteAllocationQ16 carries only the inverse matrix (rgb_solve), so it would need the forward columns too — a struct change that reaches P2's types, plus per-pixel cost.
  2. Per-channel slack scaled by the inverse of that channel's column norm, so the colour cost of the allowance is uniform across channels. Cheaper: three constants computed at build time, no per-pixel work, no new matrix.
  3. Keep the verdict honest and let the caller decide. Return the unclamped drives alongside the verdict, or report "reachable within tolerance" distinctly from "reachable", so the gamut mapper can choose to compress rather than accept a silently wrong colour.
  4. Shrink the constant to the rounding case it was introduced for and re-measure what starts being rejected. Cheapest, and the one most likely to have side effects worth seeing.

I have not measured which is right. (2) looks like the smallest change that addresses the actual mismatch, but all four change a shipped colour path, so each wants its own before/after over the corpus.

Pinned

#4304 adds two cases to tests/fl/gfx/gamut_map.cpp recording the mechanism and the extent, with vacuity guards on both, so whichever direction is taken has a baseline to move.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9

Activity

  1. zackees commented on Sep 10, 2026

    @zackees
    MemberAuthor

    Direction 2 implemented and measured (#4313)

    I wrote that I had not measured which of the four directions was right. Direction 2 — per-channel slack scaled by the inverse of that channel's column magnitude — measures well enough that I have implemented it.

    Slack, per channel, for the corpus device: green 64, red 33, blue 5. Bind time, three integers per profile, no per-pixel work and no new matrix, exactly as the issue estimated.

    Effect, over 291 in-gamut targets on the xy plane at Y = 0.5:

    before after
    worst OKLab hue divergence 0.0197 0.0034
    targets above 0.005 6 0
    targets above 0.010 2 0
    targets reported reachable 291 283

    The (0.55, 0.41) case that motivated the issue goes from 0.026 of hue to 0.0000 — the target is refused now, and the gamut mapper compresses it into the hull instead of the allocator answering it with the wrong colour.

    The 8 targets are the cost. They are not lost, but they do move from "answered directly" to "compressed", and that count is pinned in the test so it cannot grow unnoticed.

    Directions 1, 3 and 4

    Not implemented, and now less pressing:

    • (1) colour-space threshold needs the forward columns in WhiteAllocationQ16 and a per-pixel comparison. Direction 2 gets the same uniformity from three constants computed once, so the extra machinery buys precision this measurement does not show a need for.
    • (3) return the unclamped drives is still worth considering independently — it would let the mapper distinguish "reachable" from "reachable within tolerance" rather than inferring it.
    • (4) shrink the constant is what direction 2 does per channel, without the collateral of shrinking it on channels that were fine.

    Still open here

    The two-white RGBWW path keeps its uniform slack. The defect was measured on the single-white allocation; RGBWW holds hue under 0.02 on the same five targets that gave RGBW its 0.026, so it is better behaved and may not have the same problem. I did not scale its slack on a guess. Measuring whether it has the same defect is the remaining work on this issue.

  2. zackees commented on Sep 10, 2026

    @zackees
    MemberAuthor

    The remaining question is answered: RGBWW does not share the defect (#4314)

    #4313 fixed the single-white path and left the two-white one alone, because the defect had only been measured on the former. Measured now.

    It does not have it. The defect is accepting a target whose RGB solve wants a drive outside [0, 1], clamping it, and reporting the target reachable. allocateTwoWhiteDrivesQ16 never accepts one.

    The guard that makes that a real statement: on the same grid, 670 targets do have a solve wanting an out-of-range drive, and all 670 are refused. My first probe reported only that no accepted target had one — which would have been true over an empty set too, and I nearly published it that way.

    Worst OKLab hue divergence over the 283 it accepts: 0.00096, against the single-white path's 0.0197 before #4313 and 0.0034 after.

    Why it holds — and a correction

    I assumed the reason was structural: kTwoWhiteBoundSlack is half the final check's tolerance, so the bounds admit less than the check accepts. That is true and it is not the reason — making them equal changes nothing.

    What holds it is margin:

    slack accepted with an out-of-range solve
    64 (shipped) 0
    128 / 256 / 512 0
    1024 3
    2048 5
    4096 11

    64 sits a factor of eight below where the behaviour changes. A comfortable margin rather than a guarantee — worth knowing before anyone widens it.

    Status of this issue

    I think that closes it, but the close is yours.

  3. zackees commented on Sep 19, 2026

    @zackees
    MemberAuthor

    Closing: the defect is fixed on master. It shipped in 6d54da4c12: #4313's commits were squashed into #4314 when that stacked PR was retargeted and merged, which is why #4313 shows as closed rather than merged.

    Verified on current master, not carried over from the PRs:

    • kWhiteSlackAtUnitColumn and the per-channel slack are in src/fl/gfx/white_allocation.cpp.hpp. For the corpus device that gives green 64, red 33 and blue 5.

    • tests/fl/gfx/gamut_map.cpp passes on master and pins the fix:

      case what it asserts
      "a drive the solve puts out of range is refused, not clamped" the original defect case is refused
      "the allocator is accurate across the plane" worst OKLab hue divergence 0.0034; nothing above 0.005 (was 0.0197 with 6 above); 283 accepted, pinned
      "the two-white allocator never clamps…" 670 of 670 out-of-range solves refused
    • Since docs(color): state P7's gamut-mapping tolerances; one hue bound in the tests #4439, the hue bound in those checks is P7's stated tolerance, kHueTolerance = 0.005, rather than a free number.

    Directions 1, 3 and 4 from the issue were alternatives to the fix that shipped. None of them is a remaining defect: with direction 2 in place, no measured case is left for them to repair.

  4. moved this from Triage to Done in FastLED Trackeron Sep 19, 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