Skip to content

refactor(colorimetric): extract shared response layer (#3255 PR 1) - #3256

Merged
zackees merged 2 commits into
masterfrom
feat/colorimetric-response-extract
Jun 19, 2026
Merged

zackees merged 2 commits into
masterfrom
feat/colorimetric-response-extract

Conversation

@zackees

@zackees zackees commented Jun 19, 2026 •

Copy link
Copy Markdown
Member

Summary

PR 1 of #3255. Pure refactor — moves the topology-independent colorimetric math helpers out of fl::colorimetric_detail:: (RGBW-specific) into a new fl::colorimetric_response:: namespace so the upcoming RGB-only colorimetric path (PR 3) can reuse them.

Zero behavior change — every RGBW path still calls the same code via using-declarations that re-export the moved names back into colorimetric_detail::. PR 2 will drop those re-exports as part of the variant + shared_ptr config refactor.

What moved

Into src/fl/gfx/colorimetric_response.h under namespace fl::colorimetric_response:

  • cct_to_xy, xyY_to_XYZ, invert3x3, matvec3, barycentric_xy, build_source_matrix
  • quantize_u8, quantize_lut_cell, nnls3, hermite_basis
  • kLutQ, kLutStrideBilinear, kLutStrideHermite, the LutInterp enum
  • New EmitterProfile struct (RGB-only profile shape; declared here so PR 3 can land the solver without a structural reshuffle).

What stayed

src/fl/gfx/rgbw_colorimetric.h keeps every RGBW-specific declaration: ProfileCache, LutTable, RgbcctProfile, is_native_input_gamut, count_active_channels, the build_profile_cache / solve_strict_subgamut* / solve_wx_lp_legacy / solve_wx_overdrive / build_lut / lookup_lut / solve_rgbcct declarations, the kDefaultOverdriveRatio constant.

The header now #includes colorimetric_response.h and starts with a using-declaration block (each marked // ok bare using — controlled re-export inside a namespace block) that re-imports the moved names into colorimetric_detail::. That keeps every external caller working unchanged: src/fl/gfx/rgbw.cpp.hpp (6 references), src/fl/gfx/rgbww.cpp.hpp (11 references), src/fl/gfx/rgbww.h, and tests/fl/gfx/rgbw_colorimetric.cpp (uses using namespace fl::colorimetric_detail;).

New test coverage

tests/fl/gfx/colorimetric_response.cpp exercises the moved primitives in isolation of any RGBW topology — 20 test cases covering xyY/XYZ round-trip, 3x3 invert identity + singular detection, matvec3 hand-computed product, barycentric inside/outside, CCT-to-xy at 2700/4000/6500K, quantize edges, NNLS identity + non-negativity, Hermite basis at nodes + partition-of-unity, LUT stride constants, and EmitterProfile zero-init.

The existing tests/fl/gfx/rgbw_colorimetric.cpp was deliberately not touched and continues to pass byte-identically — it pulls in the moved helpers via the using-declarations.

Build manifest

src/fl/gfx/_build.cpp.hpp picks up colorimetric_response.cpp.hpp in the same unity-build slot as the other gfx modules. The new .cpp.hpp is empty (gated by FASTLED_RGBW_COLORIMETRIC so it matches the existing gate exactly) and reserved for PR 3's solve_rgb_colorimetric definition.

Test plan

  • bash test fl_gfx_colorimetric_response — new module, 20/20 pass
  • bash test fl_gfx_rgbw_colorimetric — existing, byte-identical, pass
  • bash test fl_gfx_rgbww — existing, byte-identical, pass
  • bash test --cpp — 331/331 pass
  • bash lint --cpp — clean

Out of scope

PR 2 of #3255: fl::variant<RGBW_MODE, ColorimetricMode> + DiodeProfilePtr (fl::shared_ptr<const DiodeProfile>), removal of FASTLED_RGBW_COLORIMETRIC macro + kRGBWColorimetric/kRGBWColorimetricBoosted enum entries + global setters + the using-declaration re-exports added here.

PR 3 of #3255: actual RGB solver implementation, per-strip loadAndScaleRGB hook, CLEDController::setRgbColorimetricProfile, FL_PLATFORM_HAS_TINY_MEMORY gating on per-instance state.

Refs #3255.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added a colorimetric response math layer with chromaticity/XYZ conversions, 3×3 matrix helpers, barycentric classification, CCT→xy, quantization, LUT interpolation utilities, and an EmitterProfile structure.
  • Refactor

    • Centralized shared chromaticity/LUT primitives into the new colorimetric response header and re-exported them for backward compatibility from the existing colorimetric detail area.
  • Tests

    • Added unit tests validating conversions, matrix operations, LUT quantization/strides, solver behavior, Hermite basis properties, and EmitterProfile initialization.
  • Chores

    • Updated the unity build compilation to include the new colorimetric response component.

Move the topology-independent math primitives out of
`fl::colorimetric_detail::` and into a new `fl::colorimetric_response::`
namespace so the upcoming RGB-only colorimetric path (PR 3 of #3255)
can reuse them without dragging in RGBW-specific solver internals.

Moved into `src/fl/gfx/colorimetric_response.h`:
  cct_to_xy, xyY_to_XYZ, invert3x3, matvec3, barycentric_xy,
  build_source_matrix, quantize_u8, quantize_lut_cell, nnls3,
  hermite_basis, the kLut* constants, the LutInterp enum, and a new
  EmitterProfile struct (declared now, used by the RGB solver in PR 3).

`rgbw_colorimetric.h` keeps every RGBW-specific declaration
(ProfileCache, LutTable, RgbcctProfile, solve_*) and re-exports the
moved names into `colorimetric_detail::` via using-declarations so
every existing call site (`rgbw.cpp.hpp`, `rgbww.cpp.hpp`, the
`using namespace fl::colorimetric_detail` test) keeps compiling
byte-identically. PR 2 of #3255 will drop the `colorimetric_detail::`
re-exports as part of the variant + shared_ptr refactor.

Verification:
- bash test fl_gfx_colorimetric_response  (new module, 20 cases)
- bash test fl_gfx_rgbw_colorimetric      (existing, byte-identical)
- bash test fl_gfx_rgbww                  (existing, byte-identical)
- bash test --cpp                         (331/331 pass)
- bash lint --cpp                         (clean)

Refs #3255.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 19, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: fef967c9-bfc1-4ac4-8a87-d2a8b2acbaa0

📥 Commits

Reviewing files that changed from the base of the PR and between 0a7ede4 and dd8d330.

📒 Files selected for processing (1)
  • tests/fl/gfx/colorimetric_response.cpp
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/fl/gfx/colorimetric_response.cpp

📝 Walkthrough

Walkthrough

Shared chromaticity math and LUT utilities are extracted from rgbw_colorimetric.h into a new topology-independent header fl/gfx/colorimetric_response.h. The original header is updated to re-export these symbols via using declarations and constexpr aliases into fl::colorimetric_detail for backward compatibility. A unity-build stub and unit tests are also added.

Changes

Colorimetric math extraction and wiring

Layer / File(s) Summary
New colorimetric_response.h: primitives, LUT utils, EmitterProfile
src/fl/gfx/colorimetric_response.h
Introduces the new header in fl::colorimetric_response with inline math helpers (cct_to_xy, xyY_to_XYZ, invert3x3, matvec3, barycentric_xy, quantize_u8, build_source_matrix, nnls3), LUT constants/LutInterp/hermite_basis/quantize_lut_cell, and the EmitterProfile struct.
rgbw_colorimetric.h refactored to re-export
src/fl/gfx/rgbw_colorimetric.h
Removes inline implementations of nnls3, hermite_basis, quantize_lut_cell, LUT constants, and LutInterp; adds include of colorimetric_response.h; introduces using declarations and constexpr aliases in fl::colorimetric_detail for backward compatibility.
Unity build and .cpp.hpp wiring
src/fl/gfx/_build.cpp.hpp, src/fl/gfx/colorimetric_response.cpp.hpp
Registers the new file in the amalgamated build; populates the .cpp.hpp stub with the header include and a FASTLED_RGBW_COLORIMETRIC-gated empty namespace.
Unit tests for new primitives
tests/fl/gfx/colorimetric_response.cpp
Adds tests covering all new math helpers, LUT utilities, and EmitterProfile zero-initialization, including Rec.709/D65 numeric validation and non-negativity enforcement for nnls3.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

  • FastLED/FastLED#3255: This PR implements Phase 1 of the three-phase refactor outlined in that issue, extracting shared colorimetric math primitives into a centralized colorimetric_response module.

Possibly related PRs

  • FastLED/FastLED#2552: Directly overlaps — original rgbw_colorimetric* solver and LUT helper definitions that this PR centralizes were introduced there.
  • FastLED/FastLED#2709: The nnls3 projected-gradient NNLS helper and its RGBW test coverage being relocated in this PR were added/modified there.
  • FastLED/FastLED#2721: The LutInterp (Bilinear vs Hermite) interpolation mode centralized here is made selectable via the RGBW LUT API in that PR.

Poem

🐇 Hopping through the color space,
I pulled the math to one bright place.
nnls3 and hermite_basis too —
Now colorimetric_response holds every clue!
Re-exports keep old callers fed,
While new tests verify what's read. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 35.29% 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 PR title accurately summarizes the main objective: extracting shared colorimetric response math helpers into a new topology-independent layer, enabling code reuse across different colorimetric paths.
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 feat/colorimetric-response-extract

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.

Actionable comments posted: 5

🤖 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.

Inline comments:
In `@src/fl/gfx/colorimetric_response.cpp.hpp`:
- Around line 14-24: Replace the FASTLED_RGBW_COLORIMETRIC preprocessor
directive check at the top of the file with a new FL_* format identifier
following the FL_<COMPONENT>_<NAME> naming convention. Update the `#if`
preprocessor check and its corresponding `#endif` comment to use the new FL_*
macro name instead of the FASTLED_RGBW_COLORIMETRIC macro, ensuring consistency
with the macro policy that forbids new FASTLED_* identifiers in newly introduced
code.

In `@src/fl/gfx/colorimetric_response.h`:
- Around line 41-56: The function signatures cct_to_xy and xyY_to_XYZ use
decayed array parameters (float out[2] and float out[3]) which lose extent
information and don't provide bounds checking. Replace all occurrences of
fixed-size array parameters in the form T out[N] or T in[N] with fl::span<T, N>
to provide extent-safe types. This applies to cct_to_xy, xyY_to_XYZ, and the
other functions mentioned at lines 67-68, 88-89, 94-96, 131-133, and 224-225.
Update each parameter declaration to use the appropriate fl::span specialization
with the correct size.
- Around line 41-238: Move all function definitions (cct_to_xy, xyY_to_XYZ,
invert3x3, matvec3, barycentric_xy, quantize_u8, build_source_matrix, nnls3,
hermite_basis, quantize_lut_cell, and the constexpr constants) out of the
src/fl/gfx/colorimetric_response.h file and into a new
colorimetric_response.cpp.hpp or colorimetric_response.impl.hpp file. The .h
file should contain only forward declarations, type definitions, and enum
declarations, while the function bodies and implementation details move to the
new .cpp.hpp/.impl.hpp file which will be included in the appropriate
implementation files.
- Around line 31-41: The comments in colorimetric_response.h contain non-ASCII
Unicode characters that violate the 7-bit ASCII requirement. Replace all Unicode
glyphs with their ASCII equivalents throughout the file, specifically in the
comments around the cct_to_xy function definition and in the sections at lines
158-165, 202-207, 216-223, and 245-248. Replace the following: ≤ with <=, ≥ with
>=, × with x, · with ., ∂ with d, → with ->, and any other non-ASCII characters
with their ASCII equivalents to ensure the entire file uses only 7-bit ASCII
characters.

In `@tests/fl/gfx/colorimetric_response.cpp`:
- Around line 166-212: Replace all non-ASCII Unicode characters in the comments
throughout this test file with their ASCII equivalents. Specifically, replace
the arrow symbol (→) with a hyphen-greater-than sequence (->), the
approximately-equal symbol (≈) with a tilde-equals sequence (~=), and the middle
dot symbol (·) with an asterisk (*). Apply these replacements to all test cases
including "build_source_matrix: Rec.709 + D65 reproduces published sRGB → XYZ",
"build_source_matrix: singular primaries return false", and "nnls3: identity
system trivial recovery", as well as any other comments in the file marked in
the review.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 610ed167-8bba-4511-8737-2275543eaac2

📥 Commits

Reviewing files that changed from the base of the PR and between 71a2122 and 0a7ede4.

📒 Files selected for processing (5)
  • src/fl/gfx/_build.cpp.hpp
  • src/fl/gfx/colorimetric_response.cpp.hpp
  • src/fl/gfx/colorimetric_response.h
  • src/fl/gfx/rgbw_colorimetric.h
  • tests/fl/gfx/colorimetric_response.cpp

Comment thread src/fl/gfx/colorimetric_response.h Resolved
Comment thread src/fl/gfx/colorimetric_response.h Resolved
Comment on lines +41 to +238
inline void cct_to_xy(int cct, float out[2]) FL_NOEXCEPT {
const float T = static_cast<float>(
(cct < 1500) ? 1500 : ((cct > 15000) ? 15000 : cct));
const float T2 = T * T;
const float u_num = 0.860117757f + 1.54118254e-4f * T + 1.28641212e-7f * T2;
const float u_den = 1.0f + 8.42420235e-4f * T + 7.08145163e-7f * T2;
const float v_num = 0.317398726f + 4.22806245e-5f * T + 4.20481691e-8f * T2;
const float v_den = 1.0f - 2.89741816e-5f * T + 1.61456053e-7f * T2;
const float u = u_num / u_den;
const float v = v_num / v_den;
const float den = 2.0f * u - 8.0f * v + 4.0f;
out[0] = 3.0f * u / den;
out[1] = 2.0f * v / den;
}

inline void xyY_to_XYZ(float x, float y, float Y, float out[3]) FL_NOEXCEPT {
if (y < 1e-12f) {
out[0] = out[1] = out[2] = 0.0f;
return;
}
const float inv_y = 1.0f / y;
out[0] = x * Y * inv_y;
out[1] = Y;
out[2] = (1.0f - x - y) * Y * inv_y;
}

inline bool invert3x3(const float in[3][3], float out[3][3]) FL_NOEXCEPT {
const float a = in[0][0], b = in[0][1], c = in[0][2];
const float d = in[1][0], e = in[1][1], f = in[1][2];
const float g = in[2][0], h = in[2][1], i = in[2][2];
const float det = a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g);
if (fl::fabs(det) < 1e-20f) {
return false;
}
const float inv_det = 1.0f / det;
out[0][0] = (e * i - f * h) * inv_det;
out[0][1] = (c * h - b * i) * inv_det;
out[0][2] = (b * f - c * e) * inv_det;
out[1][0] = (f * g - d * i) * inv_det;
out[1][1] = (a * i - c * g) * inv_det;
out[1][2] = (c * d - a * f) * inv_det;
out[2][0] = (d * h - e * g) * inv_det;
out[2][1] = (b * g - a * h) * inv_det;
out[2][2] = (a * e - b * d) * inv_det;
return true;
}

inline void matvec3(const float M[3][3], const float v[3], float out[3]) FL_NOEXCEPT {
out[0] = M[0][0] * v[0] + M[0][1] * v[1] + M[0][2] * v[2];
out[1] = M[1][0] * v[0] + M[1][1] * v[1] + M[1][2] * v[2];
out[2] = M[2][0] * v[0] + M[2][1] * v[1] + M[2][2] * v[2];
}

inline bool barycentric_xy(const float t[2], const float A[2], const float B[2],
const float C[2], float bary[3]) FL_NOEXCEPT {
const float v0x = B[0] - A[0], v0y = B[1] - A[1];
const float v1x = C[0] - A[0], v1y = C[1] - A[1];
const float v2x = t[0] - A[0], v2y = t[1] - A[1];
const float d00 = v0x * v0x + v0y * v0y;
const float d01 = v0x * v1x + v0y * v1y;
const float d11 = v1x * v1x + v1y * v1y;
const float d20 = v2x * v0x + v2y * v0y;
const float d21 = v2x * v1x + v2y * v1y;
const float den = d00 * d11 - d01 * d01;
if (fl::fabs(den) < 1e-20f) {
return false;
}
const float inv_den = 1.0f / den;
const float u = (d11 * d20 - d01 * d21) * inv_den;
const float v = (d00 * d21 - d01 * d20) * inv_den;
bary[0] = 1.0f - u - v;
bary[1] = u;
bary[2] = v;
return true;
}

inline u8 quantize_u8(float v) FL_NOEXCEPT {
const float scaled = v * 255.0f + 0.5f;
if (scaled <= 0.0f) return 0;
if (scaled >= 255.0f) return 255;
return static_cast<u8>(scaled);
}

// Standard CIE primary-matrix construction (#2705). Given source primary
// chromaticities xy_r/g/b and a source white chromaticity xy_w, build the
// 3x3 matrix M such that M·[1,1,1]^T = xyY_to_XYZ(xy_w, 1.0). Columns are
// per-channel scaled XYZ vectors of the primaries at Y=1, with scaling
// chosen so the (1,1,1) input lands at source white in XYZ. This is the
// classic linear-sRGB -> XYZ derivation generalized to arbitrary primaries.
// Returns false if the primary matrix is singular (collinear chromaticities).
inline bool build_source_matrix(const float xy_r[2], const float xy_g[2],
const float xy_b[2], const float xy_w[2],
float M_out[3][3]) FL_NOEXCEPT {
float xyz_R[3], xyz_G[3], xyz_B[3], xyz_W[3];
xyY_to_XYZ(xy_r[0], xy_r[1], 1.0f, xyz_R);
xyY_to_XYZ(xy_g[0], xy_g[1], 1.0f, xyz_G);
xyY_to_XYZ(xy_b[0], xy_b[1], 1.0f, xyz_B);
xyY_to_XYZ(xy_w[0], xy_w[1], 1.0f, xyz_W);

float P[3][3];
P[0][0] = xyz_R[0]; P[0][1] = xyz_G[0]; P[0][2] = xyz_B[0];
P[1][0] = xyz_R[1]; P[1][1] = xyz_G[1]; P[1][2] = xyz_B[1];
P[2][0] = xyz_R[2]; P[2][1] = xyz_G[2]; P[2][2] = xyz_B[2];

float P_inv[3][3];
if (!invert3x3(P, P_inv)) {
return false;
}
float k[3];
matvec3(P_inv, xyz_W, k);

M_out[0][0] = k[0] * xyz_R[0]; M_out[0][1] = k[1] * xyz_G[0]; M_out[0][2] = k[2] * xyz_B[0];
M_out[1][0] = k[0] * xyz_R[1]; M_out[1][1] = k[1] * xyz_G[1]; M_out[1][2] = k[2] * xyz_B[1];
M_out[2][0] = k[0] * xyz_R[2]; M_out[2][1] = k[1] * xyz_G[2]; M_out[2][2] = k[2] * xyz_B[2];
return true;
}

// Non-negative least squares for the 3×3 sub-system M·t = b with t ≥ 0
// (#2708, gist §3). Projected-gradient form matching the reference
// `_nnls_solve` fallback used when scipy is unavailable: 500 iterations at
// step 0.01. Cheap (~50 µs scalar on a typical MCU at -O2; only invoked for
// out-of-hull source targets, never on the in-hull fast path) and free of
// dynamic allocation, which makes it safe to inline behind the colorimetric
// solvers' rare-branch.
inline void nnls3(const float M[3][3], const float b[3], // ok array parameter
float t_out[3], float* residual_out) FL_NOEXCEPT {
float t[3] = {0.0f, 0.0f, 0.0f};
constexpr float kStep = 0.01f;
constexpr int kIters = 500;
for (int it = 0; it < kIters; ++it) {
float r[3];
r[0] = M[0][0]*t[0] + M[0][1]*t[1] + M[0][2]*t[2] - b[0];
r[1] = M[1][0]*t[0] + M[1][1]*t[1] + M[1][2]*t[2] - b[1];
r[2] = M[2][0]*t[0] + M[2][1]*t[1] + M[2][2]*t[2] - b[2];
// grad = Mᵀ · r
float g[3];
g[0] = M[0][0]*r[0] + M[1][0]*r[1] + M[2][0]*r[2];
g[1] = M[0][1]*r[0] + M[1][1]*r[1] + M[2][1]*r[2];
g[2] = M[0][2]*r[0] + M[1][2]*r[1] + M[2][2]*r[2];
for (int j = 0; j < 3; ++j) {
float v = t[j] - kStep * g[j];
t[j] = v > 0.0f ? v : 0.0f;
}
}
if (residual_out != nullptr) {
float r[3];
r[0] = M[0][0]*t[0] + M[0][1]*t[1] + M[0][2]*t[2] - b[0];
r[1] = M[1][0]*t[0] + M[1][1]*t[1] + M[1][2]*t[2] - b[1];
r[2] = M[2][0]*t[0] + M[2][1]*t[1] + M[2][2]*t[2] - b[2];
*residual_out = fl::sqrt(r[0]*r[0] + r[1]*r[1] + r[2]*r[2]);
}
t_out[0] = t[0]; t_out[1] = t[1]; t_out[2] = t[2];
}

// ===== LUT quantization and interpolation constants =========================
// Used by both the RGBW LUT (today) and any future RGB / RGBCCT LUT paths
// extracted out of the topology-specific solver modules.

// LUT cell quantization scale (value in [-8, +8) maps to i16 via *kLutQ).
constexpr i16 kLutQ = 4096;

// Storage stride per grid point. Bilinear LUTs store 4 (rgbw) values; Hermite
// LUTs additionally store ∂/∂t_x and ∂/∂t_y per channel (in cell-parameter
// units), enabling bicubic Hermite interpolation that reaches comparable
// accuracy at ~half the grid edge length — ~25 % of the memory at ~ the
// same error vs. bilinear. The grid step is uniform so derivatives are
// naturally expressed in cell-parameter units (t ∈ [0, 1] per cell).
constexpr int kLutStrideBilinear = 4;
constexpr int kLutStrideHermite = 12;

enum class LutInterp : u8 {
Bilinear = 0,
Hermite = 1,
};

// Cubic Hermite basis on [0, 1]. Output layout: { h00, h01, h10, h11 } where
// h00(t) = 2t³ - 3t² + 1 value at t=0
// h01(t) = -2t³ + 3t² value at t=1
// h10(t) = t³ - 2t² + t derivative at t=0
// h11(t) = t³ - t² derivative at t=1
// Lifted into a header inline so `lookup_lut` and the test suite consume
// exactly the same evaluator (CodeRabbit #2707: tests that redefine basis
// locally cannot catch regressions in the production code).
inline void hermite_basis(float t, float out[4]) FL_NOEXCEPT {
const float t2 = t * t;
const float t3 = t2 * t;
out[0] = 2.0f * t3 - 3.0f * t2 + 1.0f;
out[1] = -2.0f * t3 + 3.0f * t2;
out[2] = t3 - 2.0f * t2 + t;
out[3] = t3 - t2;
}

inline i16 quantize_lut_cell(float v) FL_NOEXCEPT {
const float scaled = v * static_cast<float>(kLutQ) + 0.5f;
if (scaled <= -32768.0f) return -32768;
if (scaled >= 32767.0f) return 32767;
return static_cast<i16>(scaled);
}

@coderabbitai coderabbitai Bot Jun 19, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | 🏗️ Heavy lift

Move function definitions out of src/**/*.h into .cpp.hpp/.impl.hpp.

This header defines many functions inline under src/fl/.../*.h. Per the repo header policy, function definitions should not live in these .h files.

As per coding guidelines, "src/**/*.h: Header file. Verify it does NOT contain function definitions... If function definitions are needed, they belong in .cpp.hpp or .impl.hpp files."

🤖 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/colorimetric_response.h` around lines 41 - 238, Move all function
definitions (cct_to_xy, xyY_to_XYZ, invert3x3, matvec3, barycentric_xy,
quantize_u8, build_source_matrix, nnls3, hermite_basis, quantize_lut_cell, and
the constexpr constants) out of the src/fl/gfx/colorimetric_response.h file and
into a new colorimetric_response.cpp.hpp or colorimetric_response.impl.hpp file.
The .h file should contain only forward declarations, type definitions, and enum
declarations, while the function bodies and implementation details move to the
new .cpp.hpp/.impl.hpp file which will be included in the appropriate
implementation files.

Source: Coding guidelines

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Deferred to PR 2 of #3255. The inline definitions in src/fl/gfx/colorimetric_response.h are a byte-identical move of code that already lived inline in src/fl/gfx/rgbw_colorimetric.h on master (same pattern, same call sites in rgbw.cpp.hpp / rgbww.cpp.hpp consumed the inline-in-header layout). The header comment at the top of colorimetric_response.h explicitly documents the rationale -- these helpers are in hot inner loops and the optimizer needs to fold them. Splitting the bodies out into colorimetric_response.cpp.hpp now would change inlining behavior versus the master baseline (violating this PR's byte-identical guarantee for the RGBW solver) and would leave the matching inline helpers in rgbw_colorimetric.h half-migrated. PR 2 of #3255 will move both files' bodies into .cpp.hpp together with an explicit benchmark gate to confirm the optimizer still folds them at the call sites.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@zackees, understood — the inline-in-header layout is the established baseline for these helpers and splitting the bodies now would change inlining behavior relative to master. The plan for PR 2 of #3255 (benchmarked, coordinated move of both files together) makes sense.

Would you like me to open a GitHub tracking issue for the migration of colorimetric_response.h and rgbw_colorimetric.h function bodies to .cpp.hpp in PR 2 of #3255, so it doesn't get lost?


✏️ Learnings added
Learnt from: zackees
Repo: FastLED/FastLED PR: 3256
File: src/fl/gfx/colorimetric_response.h:41-238
Timestamp: 2026-06-19T02:26:47.210Z
Learning: In `src/fl/gfx/colorimetric_response.h` (and the peer `src/fl/gfx/rgbw_colorimetric.h`), the colorimetric math helpers (`cct_to_xy`, `xyY_to_XYZ`, `invert3x3`, `matvec3`, `barycentric_xy`, `quantize_u8`, `build_source_matrix`, `nnls3`, `hermite_basis`, `quantize_lut_cell`) are intentionally kept `inline` in the `.h` file rather than moved to `.cpp.hpp`/`.impl.hpp`. These helpers sit in hot inner loops and must be visible to the optimizer for folding at call sites in `rgbw.cpp.hpp` and `rgbww.cpp.hpp`. Moving them to `.cpp.hpp` is planned for PR 2 of issue `#3255`, which will migrate both files together with an explicit benchmark gate to confirm the optimizer still folds them. Do not flag the inline-in-header pattern for these specific files as a violation until that migration PR lands.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Comment thread tests/fl/gfx/colorimetric_response.cpp Resolved
Address CodeRabbit comment 3439669997 on PR #3256: replace non-ASCII
glyphs in comments and test-case names (-> , <=, ~=, etc.) so the new
test file complies with the repo source-encoding rule.

The matching non-ASCII characters in the moved code in
src/fl/gfx/colorimetric_response.h and the inline definitions / decayed
array parameter style are intentionally left untouched in this PR --
that content is a byte-identical move out of rgbw_colorimetric.h and
will be normalized in PR 2 of #3255 alongside the matching cleanup of
the original RGBW header.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@zackees
zackees merged commit e637bc7 into master Jun 19, 2026
13 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jun 19, 2026
@zackees
zackees deleted the feat/colorimetric-response-extract branch August 21, 2026 19:15
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.

1 participant