SIGN IN SIGN UP

savers/gif: fix palette generation for color accuracy

Fix GIF palette generation to preserve infrequently used colors and correctly
handle transparent pixels.

_splitPalette() previously divided the palette using fixed pixel counts, which
could cause less frequent colors to share a leaf with unrelated colors and be
lost during averaging. Split boundaries are now determined by color values
using component histograms, ensuring identical colors remain together and
allowing uniform-color subsets to terminate as leaves.

_makePalette() now always calls _pickChangedPixels() so alpha thresholding is
applied consistently, including to the first frame. _pickChangedPixels() also
handles the absence of a previous frame explicitly.

The palette is cleared during gifBegin() to avoid stale values when no visible
pixels remain after alpha filtering.

These changes improve GIF color accuracy, particularly for infrequently used
colors and transparent regions.

Issue: https://github.com/thorvg/thorvg/issues/1758
A
alpakaDurumi committed
ff1b9909d424663168272f0a15b9c7ae6322c308
Parent: 3a6efe9
Committed by GitHub <noreply@github.com> on 8/11/2026, 4:11:53 AM