{"record":{"id":"34571dc035aac503","repo":"parallax/jsPDF","slug":"invalid-code-color-length-must-be-power-of-2-and","errorCode":null,"errorMessage":"Invalid code/color length, must be power of 2 and 2 .. 256.","messagePattern":"Invalid code/color length, must be power of 2 and 2 \\.\\. 256\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/libs/omggif.js","lineNumber":47,"sourceCode":"\n\"use strict\";\n\nimport { console } from \"./console.js\";\n\nfunction GifWriter(buf, width, height, gopts) {\n  var p = 0;\n\n  var gopts = gopts === undefined ? {} : gopts;\n  var loop_count = gopts.loop === undefined ? null : gopts.loop;\n  var global_palette = gopts.palette === undefined ? null : gopts.palette;\n\n  if (width <= 0 || height <= 0 || width > 65535 || height > 65535)\n    throw new Error(\"Width/Height invalid.\");\n\n  function check_palette_and_num_colors(palette) {\n    var num_colors = palette.length;\n    if (num_colors < 2 || num_colors > 256 || num_colors & (num_colors - 1)) {\n      throw new Error(\n        \"Invalid code/color length, must be power of 2 and 2 .. 256.\"\n      );\n    }\n    return num_colors;\n  }\n\n  // - Header.\n  buf[p++] = 0x47;\n  buf[p++] = 0x49;\n  buf[p++] = 0x46; // GIF\n  buf[p++] = 0x38;\n  buf[p++] = 0x39;\n  buf[p++] = 0x61; // 89a\n\n  // Handling of Global Color Table (palette) and background index.\n  var gp_num_colors_pow2 = 0;\n  var background = 0;\n  if (global_palette !== null) {","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/libs/omggif.js#L29-L65","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar gw = new GifWriter(buf, w, h, { palette: uniqueColors }); // length 7\n// after\nfunction padPalette(p) {\n  var n = 2; while (n < p.length) n *= 2; if (n > 256) n = 256;\n  while (p.length < n) p.push(p[p.length - 1] || 0);\n  return p;\n}\nvar gw = new GifWriter(buf, w, h, { palette: padPalette(uniqueColors.slice()) });","handlingStrategy":"validation","validationCode":"function validPalette(p){ var n = p.length; return n >= 2 && n <= 256 && (n & (n-1)) === 0; }\nif (validPalette(palette)) new GifWriter(buf, w, h, { palette: palette });","typeGuard":"function isPowerOfTwoPalette(p){ return Array.isArray(p) && p.length >= 2 && p.length <= 256 && (p.length & (p.length-1)) === 0; }","tryCatchPattern":"try { new GifWriter(buf, w, h, { palette: pal }); } catch (e) { if (/Invalid code\\/color length/.test(e.message)) { /* pad palette */ } else throw e; }","preventionTips":["Use a quantizer that outputs 2^n colors","Pad palettes to the next power of two"],"tags":["gif","omggif","image","palette","validation"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}