parallax/jsPDF · error · Error
Invalid code/color length, must be power of 2 and 2 .. 256.
Error message
Invalid code/color length, must be power of 2 and 2 .. 256.
What it means
Thrown by GifWriter's check_palette_and_num_colors when a palette has fewer than 2 entries, more than 256 entries, or a count that is not a power of two. GIF color tables must be a power-of-two size between 2 and 256 because the size is encoded as a log2 field in the descriptor.
Source
Thrown at src/libs/omggif.js:47
"use strict";
import { console } from "./console.js";
function GifWriter(buf, width, height, gopts) {
var p = 0;
var gopts = gopts === undefined ? {} : gopts;
var loop_count = gopts.loop === undefined ? null : gopts.loop;
var global_palette = gopts.palette === undefined ? null : gopts.palette;
if (width <= 0 || height <= 0 || width > 65535 || height > 65535)
throw new Error("Width/Height invalid.");
function check_palette_and_num_colors(palette) {
var num_colors = palette.length;
if (num_colors < 2 || num_colors > 256 || num_colors & (num_colors - 1)) {
throw new Error(
"Invalid code/color length, must be power of 2 and 2 .. 256."
);
}
return num_colors;
}
// - Header.
buf[p++] = 0x47;
buf[p++] = 0x49;
buf[p++] = 0x46; // GIF
buf[p++] = 0x38;
buf[p++] = 0x39;
buf[p++] = 0x61; // 89a
// Handling of Global Color Table (palette) and background index.
var gp_num_colors_pow2 = 0;
var background = 0;
if (global_palette !== null) {View on GitHub (pinned to a3930ce03a)
Solutions
- Pad the palette to the next power of two between 2 and 256 (e.g. duplicate the last color).
- Use a quantizer (median-cut, neuquant) that outputs exactly 2^n colors.
- Validate palette.length is a power of two in range before calling GifWriter.
Example fix
// before
var gw = new GifWriter(buf, w, h, { palette: uniqueColors }); // length 7
// after
function padPalette(p) {
var n = 2; while (n < p.length) n *= 2; if (n > 256) n = 256;
while (p.length < n) p.push(p[p.length - 1] || 0);
return p;
}
var gw = new GifWriter(buf, w, h, { palette: padPalette(uniqueColors.slice()) }); Defensive patterns
Strategy: validation
Validate before calling
function validPalette(p){ var n = p.length; return n >= 2 && n <= 256 && (n & (n-1)) === 0; }
if (validPalette(palette)) new GifWriter(buf, w, h, { palette: palette }); Type guard
function isPowerOfTwoPalette(p){ return Array.isArray(p) && p.length >= 2 && p.length <= 256 && (p.length & (p.length-1)) === 0; } Try / catch
try { new GifWriter(buf, w, h, { palette: pal }); } catch (e) { if (/Invalid code\/color length/.test(e.message)) { /* pad palette */ } else throw e; } Prevention
- Use a quantizer that outputs 2^n colors
- Pad palettes to the next power of two
When it happens
Trigger: Passing gopts.palette with length 1, 3, 5, 100, 257, etc. Passing an empty array. Passing a palette with duplicate-trimmed or dynamically-built entries that broke the power-of-two invariant.
Common situations: Building a palette by unique-color extraction that yields an arbitrary count; truncating a 256-color palette to an arbitrary subset; using a 3-color logo palette directly.
Related errors
- Background index out of range.
- Background index explicitly passed as 0.
- Must supply either a local or global palette.
- Transparent color index.
- Width/Height invalid.
AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13).
Data as JSON: /api/errors/34571dc035aac503.
Report an issue: GitHub.