Skip to content

fix(rgbw): address CodeRabbit findings from #2552 - #2554

Merged
zackees merged 1 commit into
masterfrom
fix/rgbw-colorimetric-coderabbit
May 30, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/rgbw-colorimetric-coderabbit

Conversation

@zackees

@zackees zackees commented May 30, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #2552 addressing three actionable CodeRabbit findings (correctness + robustness; no API surface change).

1. Check invert3x3 return values in build_profile_cache (🔴 critical)

A degenerate DiodeProfile (colinear primaries, near-zero luminance, etc.) used to silently produce garbage matrices and corrupt every subsequent color conversion. Now we capture the four invert3x3 returns and emit FL_WARN_ONCE if any sub-gamut matrix inversion fails.

2. Persist effective W chromaticity in ProfileCache (🟠 major)

When cct_override was applied, cache->P_W was rebuilt from the shifted white point — but solve_strict_subgamut / solve_strict_subgamut_xy still did their barycentric routing against cache.profile->xy_w. CCT-shifted whites would fall in the wrong sub-gamut while the matrix solve used the right one. ProfileCache now carries an xy_w[2] field; build_profile_cache populates it with the effective (post-shift) chromaticity and the solvers route against it.

3. Harden build_lut / lookup_lut against invalid inputs (🟠 major)

  • build_lut(grid_n) used to divide by N-1 unconditionally and computed allocation size in signed int (overflow risk for negative N). Now bails to an empty LutTable when grid_n < 2 and uses size_t arithmetic.
  • lookup_lut now zero-fills out_rgbw and early-returns when the LUT is empty/unbuilt or has a zero xy span.

Not addressed in this PR

Test plan

  • fl_gfx_rgbw_colorimetric test suite: 14/14 passing (existing coverage; no new tests required — the fixes harden internal paths that aren't directly callable from tests under the _build-only include rule)
  • Full host C++ test suite: passes
  • Lint passes

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved color processing robustness against edge cases and degenerate inputs.
    • Enhanced error handling and validation for color gamut operations.
    • Added safeguards for color profile processing to prevent errors.
  • Logging

    • Enhanced diagnostic logging for color processing operations.

Review Change Stack

Three correctness/robustness fixes for the colorimetric path, all from
CodeRabbit review of the original PR #2552:

1. Check invert3x3 return values in build_profile_cache (CRITICAL)
   A degenerate DiodeProfile (colinear primaries, near-zero luminance, etc.)
   silently produced garbage matrices and corrupted every subsequent color
   conversion. build_profile_cache now captures the four invert3x3 returns
   and FL_WARN_ONCEs when any sub-gamut matrix inversion fails.

2. Persist effective W chromaticity in ProfileCache (MAJOR)
   When cct_override was applied, cache->P_W was rebuilt from the shifted
   white point, but solve_strict_subgamut / solve_strict_subgamut_xy still
   did their barycentric routing against cache.profile->xy_w. That made
   CCT-shifted whites fall in the wrong sub-gamut while the matrix solve
   used the right one. ProfileCache now carries an xy_w[2] field that the
   solvers route against; build_profile_cache populates it with the
   effective (post-shift) chromaticity.

3. Harden build_lut / lookup_lut against invalid inputs (MAJOR)
   build_lut(grid_n) used to divide by N-1 unconditionally and allocate
   with signed-int product (overflow risk for negative N). It now bails
   to an empty LutTable on grid_n < 2 and computes the allocation size as
   size_t. lookup_lut now zero-fills out_rgbw and early-returns when the
   LUT is empty/unbuilt or has a zero xy span.

Skipped from the review:
  - "Replace raw new/delete with RAII" — already done; the merged PR uses
    fl::unique_ptr throughout.
  - "Use new(std::nothrow)" — same; no bare allocations remain.
  - "Test name doesn't match assertions" — already fixed in the merged PR
    by renaming and removing the misleading equality claim.
  - "Replace raw array parameters with fl::span" — genuine refactor that
    would touch every function signature in the public header. Deferred to
    a dedicated pass.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@coderabbitai

coderabbitai Bot commented May 30, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The PR hardens the RGBW colorimetric solver pipeline by storing the effective W chromaticity (accounting for CCT overrides) in ProfileCache, unifying solver routing logic, and adding degenerate-input guards to LUT generation and lookup paths.

Changes

RGBW Colorimetric Robustness Improvements

Layer / File(s) Summary
ProfileCache schema and initialization
src/fl/gfx/rgbw_colorimetric.h, src/fl/gfx/rgbw_colorimetric.cpp.hpp
ProfileCache now includes xy_w[2] to store effective W chromaticity. build_profile_cache initializes it from the profile, optionally overwrites it from a valid CCT override, and tracks matrix inversion success with FL_WARN_ONCE on failure. Logging include is added to support warnings.
Solver routing consistency
src/fl/gfx/rgbw_colorimetric.cpp.hpp
Both solve_strict_subgamut and solve_strict_subgamut_xy switch their barycentric/simplex containment checks from profile->xy_w to cache.xy_w, ensuring CCT-shifted whites are routed consistently with the matrix solve.
LUT robustness guards
src/fl/gfx/rgbw_colorimetric.cpp.hpp
build_lut returns an empty table when grid_n < 2 to prevent divide-by-zero and unsafe allocation. lookup_lut adds multiple early returns for degenerate states (empty LUT, missing cells, zero/invalid span) to avoid out-of-bounds reads and divide-by-zero errors.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Possibly related PRs

  • FastLED/FastLED#2552: Refines the RGBW colorimetric pipeline introduced in #2552 by adding xy_w-based routing consistency and degenerate/LUT failure guards.

Poem

🐰 The colors converge where white finds its way,
CCT shifts no longer lead astray—
Each solver now sees the world the same,
While LUT guards prevent the degenerate game.
Robustness blooms in every hue! 🌈

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(rgbw): address CodeRabbit findings from #2552' directly and clearly identifies the main purpose of the PR—addressing specific correctness and robustness fixes from a code review, which aligns with the objectives describing three targeted fixes with no API changes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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/rgbw-colorimetric-coderabbit

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 and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/fl/gfx/rgbw_colorimetric.cpp.hpp (1)

43-59: 💤 Low value

Duplicate comment block.

Lines 43-46 and 51-54 convey the same message about singular matrix inversion leaving garbage output. Consider removing one of them.

♻️ Suggested fix
-    // Inverting a singular matrix leaves the destination with whatever
-    // invert3x3 wrote — solvers reading it would silently produce garbage.
-    // Warn once if any inversion fails so the user knows their profile has
-    // degenerate primaries (colinear chromaticities, near-zero luminance, etc).
     const bool ok_rgb = invert3x3(P_RGB, cache->P_RGB_inv);
     const bool ok_rgw = invert3x3(P_RGW, cache->P_RGW_inv);
     const bool ok_rbw = invert3x3(P_RBW, cache->P_RBW_inv);
     const bool ok_bgw = invert3x3(P_BGW, cache->P_BGW_inv);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/fl/gfx/rgbw_colorimetric.cpp.hpp` around lines 43 - 59, The file contains
a duplicated explanatory comment about singular matrix inversion adjacent to the
invert3x3 calls (see invert3x3(P_RGB,...), invert3x3(P_RGW,...),
invert3x3(P_RBW,...), invert3x3(P_BGW,...) and the subsequent FL_WARN_ONCE
call); remove the redundant paragraph so only one clear comment remains
describing that invert3x3 can leave garbage on failure and that FL_WARN_ONCE
reports degenerate primaries, keeping the comment that is closest/most relevant
to the invert3x3 calls and the if (!(ok_rgb && ok_rgw && ok_rbw && ok_bgw))
warning check.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/fl/gfx/rgbw_colorimetric.cpp.hpp`:
- Around line 43-59: The file contains a duplicated explanatory comment about
singular matrix inversion adjacent to the invert3x3 calls (see
invert3x3(P_RGB,...), invert3x3(P_RGW,...), invert3x3(P_RBW,...),
invert3x3(P_BGW,...) and the subsequent FL_WARN_ONCE call); remove the redundant
paragraph so only one clear comment remains describing that invert3x3 can leave
garbage on failure and that FL_WARN_ONCE reports degenerate primaries, keeping
the comment that is closest/most relevant to the invert3x3 calls and the if
(!(ok_rgb && ok_rgw && ok_rbw && ok_bgw)) warning check.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 2e7ce70c-19b3-4d98-9329-cf12faa64d15

📥 Commits

Reviewing files that changed from the base of the PR and between d686427 and d64ea83.

📒 Files selected for processing (2)
  • src/fl/gfx/rgbw_colorimetric.cpp.hpp
  • src/fl/gfx/rgbw_colorimetric.h

@zackees
zackees merged commit a3774db into master May 30, 2026
8 of 9 checks passed
zackees added a commit that referenced this pull request May 30, 2026
…e8 (#2568)

Generalizes the 16-lane BF1 win from #2559 to all PARLIO Wave8 lane
counts. Same algebraic identity (output_bit = M0_p XOR (input_bit AND D_p)),
adapted to each lane count's output packing format:

  16-lane: 8 symbols × 8 pulses × 2 bytes (low lanes, high lanes)  ← #2559
  8-lane:  8 symbols × 8 pulses × 1 byte  (8 lanes per pulse)
  4-lane:  8 symbols × 4 bytes (2 pulses/byte, 4 lanes/nibble)
  2-lane:  8 symbols × 2 bytes (4 pulses/byte, 2 lanes bit-interleaved)

Each kernel:
  1. Pre-computes per-pulse D_p / M0_p masks from chipset constants W0/W1
     (extracted from byte_lut[0x00/0xFF].symbols[0] at the API boundary).
  2. Bit-transposes input lane bytes to per-symbol "column" bytes. For 16-
     and 8-lane, uses the existing `spread_transpose16_symbol` /
     `spread_transpose8_symbol` primitives. For 4-lane, uses spreadA/spreadB
     with shifted-OR across 4 lanes (8 ops). For 2-lane, direct bit
     extraction (16 ops — tiny workload).
  3. Emits the output bytes using `output = M0_p_mask XOR (col AND D_p_mask)`,
     packed per the lane-count's output layout.

### Measured on ESP32-P4 v1.3 (256 LEDs/lane, WS2812B Wave8, 5 iters)

  lanes | total_us | show_us | wait_us | notes
   2    |   9 994  |  2 559  |  7 434  | encode << TX, TX-bound
   4    |  10 244  |  2 917  |  7 326  | encode << TX, TX-bound
   8    |  12 243  |  4 701  |  7 541  | encode < TX, TX-bound
  16    |  12 312  | 10 677  |  1 635  | eager-queue → TX overlaps with show()

All 4 lane counts complete cleanly (completed=1, channels_ok=1, no hangs).
For 2/4/8 lanes the frame is already TX-bound, so BF1's win is primarily
**reduced CPU usage** (encode time drops ~5× per the 16-lane scaling) —
freeing cycles for animation logic. For 16 lanes the win is both reduced
CPU and reduced frame time (because encode was previously > TX).

### Engine dispatch

`ParlioEngine::populateDmaBuffer` 2/4/8-lane Wave8 branches now call
`wave8Transpose_{2,4,8}_bf1` instead of `wave8Transpose_{2,4,8}`. Bit-
identical output for any Wave8 chipset/timing.

### Test plan
- [x] Host bit-exactness: 1 000 random seeds × 5 timings for each of the
  3 new BF1 kernels (= 15 000 inputs × 16/32/64 output bytes each), bit-
  for-bit equality vs the existing byte_lut path. Source in tests/fl/
  channels/wave8.cpp.
- [ ] Host `bash test wave8` skipped — pre-existing rgbw linker error on
  master (undefined `fl::rgb_2_rgbw_colorimetric` from #2545/#2554) blocks
  the shared fastled.dll build. BF1-specific cases compile cleanly.
- [x] Device (ESP32-P4 v1.3): boot-time validation cycles each lane count
  through 5 back-to-back show() calls. All pass cleanly.
- [x] `bash lint --cpp` clean.

Refs #2548, #2559.

Co-authored-by: Claude Opus 4.7 <[email protected]>
zackees added a commit that referenced this pull request May 30, 2026
…lock (#2589)

Two CodeRabbit follow-ups for src/fl/gfx/rgbw_colorimetric.cpp.hpp:

- B1: build_profile_cache warned on invert3x3 failure but still wrote
  garbage matrices into cache->P_*_inv. Downstream matvec3() then
  produced undefined output. Zero-init each failed matrix so output is
  deterministic (zero) on degenerate profiles.

- B7: lines 43-46 and 51-54 were near-identical comment blocks about
  degenerate primaries. Removed the second block.

Addresses CodeRabbit findings B1 (originally #2552) and B7 (#2554) per
meta #2580.
@zackees
zackees deleted the fix/rgbw-colorimetric-coderabbit branch May 30, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant