Skip to content

feat(colorimetric): per-strip variant<RGBW_MODE, ColorimetricMode> config + drop FASTLED_RGBW_COLORIMETRIC gate + extract shared response layer #3255

Closed

Description

@zackees

Summary

Implementation contract for the colorimetric refactor planned in #3242. After a design discussion on that issue (see investigation report and follow-ups), the open questions were resolved with the maintainer. This issue captures the chosen shape so the PR work has a single contract to land against.

Headline decisions:

  1. No compile gates. Drop FASTLED_RGBW_COLORIMETRIC everywhere. Both RGB and RGBW colorimetric paths compile unconditionally; --gc-sections drops them in sketches that don't install a profile.
  2. Per-strip profiles, not a global singleton. Each strip carries its own shared_ptr<const profile>.
  3. fl::variant + fl::shared_ptr at the public type surface — model "mode XOR colorimetric+profile" as a variant; model profile ownership as shared_ptr<const T>.
  4. Clean break. No legacy aliases — the colorimetric code was added since the last release, so there is no backward-compat burden.
  5. Tier-gate per-instance state so TINY-tier boards do not grow CLEDController / Rgbw for a feature they can't run.
  6. Extract a shared colorimetric_response module for math primitives reused between the RGB and RGBW solvers.

Background

The current RGBW colorimetric code (src/fl/gfx/rgbw_colorimetric.{h,cpp.hpp}, ~1,500 lines) ships behind a FASTLED_RGBW_COLORIMETRIC compile gate, references a single global DiodeProfile, and exposes its modes through the RGBW_MODE enum (kRGBWColorimetric, kRGBWColorimetricBoosted). #3242 asked to:

  • extract the reusable colorimetric primitives out of the RGBW-specific solver,
  • add an opt-in RGB-only entrypoint that runs after correction/temperature/brightness/gamma and before chipset emit,
  • ensure no post-solve mutation can invalidate the solved chromaticity.

That issue framed three open design questions. They were discussed and resolved as listed under "Decisions" below.

Decisions

1. No compile gates — tree-shake via install-on-setter function pointers

The existing precedent for this pattern is enable_rgbw_colorimetric_lut (see src/fl/gfx/rgbw.h:128-133: "gc-sections drops the LUT path for sketches that never call this"). Same idea, applied at the solver level:

// runtime.cpp (always compiled, a couple of pointer-sized globals)
extern void (*g_rgb_colorimetric_apply)(const EmitterProfile*, u8*, u8*, u8*);
extern void (*g_rgbw_colorimetric_apply)(const DiodeProfile*, /* ... */);

Both pointers default to nullptr. Setters install the real solver implementations. Sketches that never construct a profile or call a setter → setters unreferenced → solver impls unreferenced → gc-sections drops them along with all the float math libcalls they pull in.

2. Per-strip profiles via the existing per-strip mode mechanism

The Rgbw config struct already carries rgbw_mode per-strip; the profile follows the same path. No new parallel setRgbwColorimetricProfile(...) setter family needed — the config carrier itself is the per-strip handle.

3. fl::variant + fl::shared_ptr at the public surface

using DiodeProfilePtr   = fl::shared_ptr<const DiodeProfile>;     // RGBW (R, G, B, W chromaticity)
using EmitterProfilePtr = fl::shared_ptr<const EmitterProfile>;   // RGB (R, G, B chromaticity only)

struct ColorimetricMode { DiodeProfilePtr profile; bool boosted = false; };
struct ColorimetricRgb  { EmitterProfilePtr profile; };

shared_ptr<const> removes the lifetime footgun the previous raw const T* API exposed.
fl::variant lifts the "if you're colorimetric, you have a profile" invariant into the type system.

4. Clean break — no legacy aliases

RGBW_MODE::kRGBWColorimetric and kRGBWColorimetricBoosted are removed from the enum. The colorimetric path is reached only via the variant's ColorimetricMode alternative. The FASTLED_RGBW_COLORIMETRIC macro is removed; the warn-once fallback stubs at src/fl/gfx/rgbww.cpp.hpp:374 and :388 are removed; the stale -DFASTLED_RGBW_COLORIMETRIC=1 comment at examples/RGBWW/RGBWW.ino:14 is removed. Global setters (set_rgbw_colorimetric_profile, FastLED.setRgbwColorimetricProfile) are removed in favor of the per-strip carrier.

5. Memory-tier gate on per-instance state

Per src/fl/system/sketch_macros.h, use FL_PLATFORM_HAS_TINY_MEMORY to skip the colorimetric machinery entirely on <=1KB-SRAM parts:

struct Rgbw {
    u16 white_color_temp;
    EOrderW w_placement;
#if !FL_PLATFORM_HAS_TINY_MEMORY
    fl::variant<RGBW_MODE, ColorimetricMode> algorithm;
#else
    RGBW_MODE rgbw_mode;
#endif
};

The same gate covers the field on CLEDController, the setter, and the runtime check in loadAndScale*. On TINY: zero added bytes per controller, zero per Rgbw, code that constructs ColorimetricMode / ColorimetricRgb fails to compile. The existing AVR-only fallback at src/pixel_controller.h:593 is rewritten to use the same FL_PLATFORM_HAS_TINY_MEMORY gate so there is one rule, not two.

6. Shared math extraction (refactor only, zero behavior change)

New module src/fl/gfx/colorimetric_response.{h,cpp.hpp} under namespace fl::colorimetric_response. Moves: cct_to_xy, xyY_to_XYZ, invert3x3, matvec3, barycentric_xy, build_source_matrix, nnls3, hermite_basis, quantize_u8, quantize_lut_cell, the kLut* constants, the LutInterp enum, and the new RGB-only EmitterProfile struct.

RGBW-specific code stays in rgbw_colorimetric.{h,cpp.hpp}: ProfileCache, solve_strict_subgamut, solve_wx_lp_legacy, solve_wx_overdrive, solve_rgbcct, LUT machinery.

7. RGB entrypoint integrates at the post-policy / pre-emit boundary

The same structural position the RGBW path uses today via mColorAdjustment.premixed (src/pixel_controller.h:615). Hook lives inside loadAndScaleRGB:

void loadAndScaleRGB(u8* b0, u8* b1, u8* b2) {
    u8 r = loadAndScale0();   // already applied premixed scale (correction × temp × brightness)
    u8 g = loadAndScale1();
    u8 b = loadAndScale2();
#if !FL_PLATFORM_HAS_TINY_MEMORY
    if (mRgbColorimetric && g_rgb_colorimetric_apply) {
        g_rgb_colorimetric_apply(mRgbColorimetric->profile.get(), &r, &g, &b);
    }
#endif
    *b0 = r; *b1 = g; *b2 = b;
}

No driver edits needed — every RGB chipset already funnels through loadAndScaleRGB.

Implementation phases

Independently-mergeable PRs in order. Each preserves the bit-exact output of every path that isn't being touched.

PR 1 — Extract colorimetric_response (zero behavior change)

  • New src/fl/gfx/colorimetric_response.{h,cpp.hpp} under namespace fl::colorimetric_response.
  • Move shared math from rgbw_colorimetric.h per Decision 6.
  • Add EmitterProfile struct (RGB-only).
  • rgbw_colorimetric.h/.cpp.hpp keep RGBW-specific code, include the new header, call into colorimetric_response::* for the moved helpers.
  • New tests at tests/fl/gfx/colorimetric_response.cpp exercising the moved helpers in isolation.
  • Existing RGBW tests at tests/fl/gfx/rgbw_colorimetric.cpp continue to pass byte-identically.

PR 2 — Variant + shared_ptr config refactor; drop FASTLED_RGBW_COLORIMETRIC and globals

  • using DiodeProfilePtr = fl::shared_ptr<const DiodeProfile> in rgbw.h.
  • struct ColorimetricMode { DiodeProfilePtr profile; bool boosted = false; }.
  • Rgbw::algorithm becomes fl::variant<RGBW_MODE, ColorimetricMode> (tier-gated per Decision 5).
  • Remove RGBW_MODE::kRGBWColorimetric and kRGBWColorimetricBoosted from the enum.
  • Remove FASTLED_RGBW_COLORIMETRIC macro and every #if FASTLED_RGBW_COLORIMETRIC guard.
  • Remove warn-once fallback stubs in rgbww.cpp.hpp.
  • Remove global setters set_rgbw_colorimetric_profile / get_rgbw_colorimetric_profile / FastLED.setRgbwColorimetricProfile.
  • Switch the dispatch in loadAndScaleRGBW to read the variant; route the colorimetric alternative through the install-on-setter function pointer.
  • Update examples/RGBWW/RGBWW.ino:14 to remove the -DFASTLED_RGBW_COLORIMETRIC=1 comment.
  • RGBW colorimetric output stays bit-exact for any user-supplied profile that matches today's kRgbwDefaultProfile.

PR 3 — RGB colorimetric solver + per-strip integration

  • New solve_rgb_colorimetric(...) in colorimetric_response.cpp.hpp plus its apply_rgb_colorimetric_impl runtime adapter.
  • using EmitterProfilePtr = fl::shared_ptr<const EmitterProfile>.
  • struct ColorimetricRgb { EmitterProfilePtr profile; }.
  • CLEDController gains tier-gated fl::optional<ColorimetricRgb> mRgbColorimetric and setRgbColorimetricProfile(EmitterProfilePtr).
  • PixelController carries it through (tier-gated).
  • loadAndScaleRGB checks per-strip pointer + function-pointer gate (tier-gated).
  • New tests at tests/fl/gfx/rgb_colorimetric.cpp:
    • Native single-axis identity: (255,0,0) → (255,0,0) for a synthetic profile with input primaries = device primaries.
    • Rec.709 → wider native gamut: out-of-gamut targets clamp non-negative, in-gamut targets match the published sRGB matrix within ±1 LSB.
    • Synthetic chipset asserting byte-for-byte equality with solve_rgb_colorimetric(premixed) when brightness + correction are non-trivial — proves feat(colorimetric): split shared response layer and design RGB solve entrypoint #3242 acceptance criterion 6 ("no post-solve per-channel scaling/correction").

Acceptance criteria

Carried forward from #3242, plus the structural ones we agreed on:

  • Shared colorimetric_response extraction produces no intentional RGBW output changes.
  • Existing RGBW tests pass byte-identically across PR 1 and PR 2.
  • New colorimetric_response tests cover the moved primitives independently of RGBW topology.
  • RGB colorimetric is opt-in per strip via setRgbColorimetricProfile(EmitterProfilePtr); the default loadAndScaleRGB path is byte-identical to today when no profile is installed.
  • The RGB solve runs after loadAndScale{0,1,2} (post-correction/temperature/brightness) and before chipset emit. A synthetic-chipset test asserts byte-for-byte equality with the solver output.
  • On FL_PLATFORM_HAS_TINY_MEMORY boards: zero added bytes per CLEDController, zero per Rgbw. ColorimetricMode and ColorimetricRgb do not exist; code constructing them does not compile.
  • On non-TINY boards where no sketch references the install-on-setter setters: --gc-sections drops every colorimetric solver symbol. Verified with bash bloat <esp32-class-board> before/after.
  • No FASTLED_RGBW_COLORIMETRIC references anywhere in src/, examples/, or tests/ after PR 2 lands.
  • Lifetime safety: profiles passed via shared_ptr<const> survive any strip that points at them, regardless of declaration order.

Validation commands

bash test colorimetric_response          # PR 1 — shared module tests
bash test rgbw_colorimetric              # all PRs — byte-identical RGBW output
bash test rgb_colorimetric               # PR 3 — new RGB solver tests
bash test --cpp                          # full host suite, all PRs
bash compile uno --examples Blink        # TINY-tier compiles; no float math leaks in
bash compile esp32dev --examples Blink   # default RGB path unchanged
bash bloat esp32dev                      # confirm gc-sections drops the solvers when unused

Out of scope (deferred to future issues)

  • Integrating colorimetric solve into the encoding pipeline as a data-transformation step (separate from the JS-canvas / visualization concern surfaced in earlier channels-API investigation).
  • LUT path for RGB colorimetric — the closed-form solve is small enough that the LUT only matters for high-pixel-count perf work; revisit once a benchmark argues for it.
  • RGBCCT / RGBWW colorimetric — the shared colorimetric_response extraction unblocks these, but the per-strip variant work for those modes is its own design discussion.

References

Activity

  1. JChalka commented on Jun 19, 2026

    @JChalka
    Contributor

    @zackees I'm really not sure I can continue to work and verify sessions when you're moving 20 steps ahead of me using AI Agents. Respectfully you should pause any of your work related to the colorimetric solving and wait for my pull request, this is the reason I did not reopen a new issue while I worked on my things initially and each time I look down an AI agent has opened up a new issue on things that I'm actively working on in my fork. I have reiterated multiple times that I'm working on things and I cannot move forward with my own FastLED implementations to PR back in if you're actively changing the exact same moving parts that I've been working on refactoring and fixing, it is extremely frustrating

  2. zackees commented on Jun 19, 2026

    @zackees
    MemberAuthor

    I'll halt on this, final change is going in now.

  3. JChalka commented on Jun 19, 2026

    @JChalka
    Contributor

    I appreciate it, some of the issues you have created did answer some open-ended questions I still had on my roadmap so I'll be sure to drag in those changes/align my fork around them so that when I go to PR that things are a bit more aligned with the direction you've chosen on those points. My forks roadmap has been aligned against these new issues/refactor decisions so they're now active targets that'll be kept in mind

    Most of the remaining changes on my side should be finished in a day or two, the actual new solver modes need real verification with against the colorimeter, RGBCCT solves are pending verification on real RGBCCT strips, RGB only can be tested on RGBW technically. I did order some RGBCCT strips just a moment ago but I also need to setup a new test bench to verify them against so that could take a few days for shipment + getting the bench setup.

  4. zackees commented on Jun 19, 2026

    @zackees
    MemberAuthor

    YCorrectClip now uses source-aligned demand scaling (max(source_i / full_i)) instead of min(input) / min(full)

    Be careful of divides, they are devastating for performance. They are ok for prototyping but i will have to refactor them outs later.

  5. zackees commented on Jun 28, 2026

    @zackees
    MemberAuthor

    Closing after pushing the current RGBW colorimetric codec/profile API and example in draft PR #3435: #3435

  6. moved this from Triage to Done in FastLED Trackeron Jun 28, 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