Repository navigation
Description
Summary
Palette lookup currently has partial high-precision support through ColorFromPaletteExtended(..., u16 index, ...), but the palette pipeline still loses precision in two important places:
- palette lookup returns
CRGB, so channel precision is truncated to 8-bit after interpolation; - several noise/palette helper paths downsample 16-bit source data into
u8buffers before color mapping.
With fl::CRGB16 now available, we should define a coherent high-precision palette path that preserves low-end brightness resolution and supports smooth slow fades, noise maps, palette cycling, and HD chipset output.
Background / related history
- ColorFromPalette in 16 bits? #1014 requested a 16-bit-index
ColorFromPaletteAPI for smoother palette transitions. - Add 8-bit palette interpolation function and comparative example. #202 proposed the original extended palette interpolation idea: use a 16-bit index, with the upper palette bits selecting entries and the next 8 bits interpolating between entries.
- Add ColorFromPaletteExtended function for higher precision #1687 merged
ColorFromPaletteExtended, which addresses index precision forCRGBPalette16,CRGBPalette32,CRGBPalette256, and progmem palette variants. - Very slow fade #409 is an older user-facing report of stepped very slow palette/noise fades.
- generic color palettes #1036 and Rebase generic palette refactor onto current master #2363 are relevant palette refactor work that may make new palette families easier to add.
Current state
Code locations from current master:
src/fl/gfx/crgb16.h:CRGB16exists as 8.8 fixed-point RGB channels.src/fl/gfx/colorutils.h: palette types are stillCRGBPalette16,CRGBPalette32, andCRGBPalette256, all storing 8-bitCRGBentries.src/fl/gfx/colorutils.cpp.hpp:ColorFromPaletteExtendedacceptsu16 index, but returnsCRGB.src/noise.cpp.hpp:fill_noise16()andfill_2dnoise16()still route throughu8intermediate buffers in common display paths.src/fl/chipsets/encoders/pixel_iterator.h: the RGB16 output adapter maps already-8-bitCRGBvalues to 16-bit withmap8_to_16(), so it cannot recover precision lost before output.
Problem
ColorFromPaletteExtended solves palette address precision, but not channel precision. A high-precision source signal can still be quantized to 8-bit before or immediately after palette lookup.
That means users working with high-resolution noise, very slow fades, HD chipsets, antialiased effects, or low-brightness gradients can still see stepping/banding even though we now have both u16 indexes and CRGB16 available elsewhere in the library.
Proposed implementation plan
- Add a high-precision palette lookup API, likely named one of:
ColorFromPaletteHD(...)ColorFromPalette16(...)ColorFromPaletteExtended16(...)
- Initial overloads should accept existing palette types:
const CRGBPalette16&const CRGBPalette32&const CRGBPalette256&- possibly
TProgmemRGBPalette16andTProgmemRGBPalette32
- Return
CRGB16instead ofCRGB. - Use 16-bit or 8.8 fixed-point channel interpolation internally instead of
scale8math that truncates tou8. - Support high-precision brightness scaling, probably via
u8x8 brightnessor a separate overload. - Preserve existing
ColorFromPaletteandColorFromPaletteExtendedbehavior for source compatibility. - Add tests that demonstrate sub-8-bit channel preservation across palette interpolation and brightness scaling.
- Audit noise/palette helper paths and add high-precision variants that avoid
fill_raw_*noise16into8when the caller wants HD output. - Decide whether to add true high-precision palette storage types, e.g.
CRGB16Palette16/32/256, after the lookup API lands. - Update docs/examples to show when to use standard palette lookup, extended
u16index lookup, and fullCRGB16lookup.
API design questions
- Should the first implementation promote existing 8-bit palette entries to
CRGB16during lookup, or should it introduceCRGB16Palette*storage immediately? - Should brightness use
u8,u16,u8x8, or overloads for compatibility? - Should
CRGB16results be convertible back toCRGBexplicitly, or should palette helpers provide both HD and legacy return paths? - Should
GradientsupportcolorAt16(u16)and/orcolorAtHD(u16)? - Should
fill_palette,fill_palette_circular, andmap_data_into_colors_through_paletteget HD variants, or should we keep the first PR limited to lookup primitives?
Acceptance criteria for first PR
- A caller can map a
u16palette index toCRGB16without truncating interpolated channel values to 8-bit. - Existing palette APIs remain source-compatible.
- Tests cover at least:
CRGBPalette16HD lookup between adjacent entries;- low-brightness scaling that preserves fractional channel values;
- behavior at wrap/no-wrap boundaries;
- comparison against current
ColorFromPaletteExtendedafter explicit downconversion toCRGB.
Notes
This is not just about making palettes larger. A practical first step is preserving precision through lookup and brightness scaling. True CRGB16Palette* storage can follow once the API shape is validated.
Activity