{"record":{"id":"87faf7ae56da31a9","repo":"parallax/jsPDF","slug":"invalid-color-ch1-passed-to-jspdf-encodecolors","errorCode":null,"errorMessage":"Invalid color \"{ch1}\" passed to jsPDF.encodeColorString.","messagePattern":"Invalid color \"(.+?)\" passed to jsPDF\\.encodeColorString\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/jspdf.js","lineNumber":1668,"sourceCode":"\n    if (typeof options === \"string\") {\n      options = {\n        ch1: options\n      };\n    }\n    var ch1 = options.ch1;\n    var ch2 = options.ch2;\n    var ch3 = options.ch3;\n    var ch4 = options.ch4;\n    var letterArray =\n      options.pdfColorType === \"draw\" ? [\"G\", \"RG\", \"K\"] : [\"g\", \"rg\", \"k\"];\n\n    if (typeof ch1 === \"string\" && ch1.charAt(0) !== \"#\") {\n      var rgbColor = new RGBColor(ch1);\n      if (rgbColor.ok) {\n        ch1 = rgbColor.toHex();\n      } else if (!/^\\d*\\.?\\d*$/.test(ch1)) {\n        throw new Error(\n          'Invalid color \"' + ch1 + '\" passed to jsPDF.encodeColorString.'\n        );\n      }\n    }\n    //convert short rgb to long form\n    if (typeof ch1 === \"string\" && /^#[0-9A-Fa-f]{3}$/.test(ch1)) {\n      ch1 = \"#\" + ch1[1] + ch1[1] + ch1[2] + ch1[2] + ch1[3] + ch1[3];\n    }\n\n    if (typeof ch1 === \"string\" && /^#[0-9A-Fa-f]{6}$/.test(ch1)) {\n      var hex = parseInt(ch1.substr(1), 16);\n      ch1 = (hex >> 16) & 255;\n      ch2 = (hex >> 8) & 255;\n      ch3 = hex & 255;\n    }\n\n    if (\n      typeof ch2 === \"undefined\" ||","sourceCodeStart":1650,"sourceCodeEnd":1686,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/jspdf.js#L1650-L1686","documentation":"`encodeColorString` (src/jspdf.js:1646) is the color encoder behind setDrawColor/setFillColor/setTextColor. When `ch1` is a string that doesn't start with `#`, it tries the RGBColor parser (src/jspdf.js:1664); if that fails and the string also isn't a bare numeric value (matching `/^\\d*\\.?\\d*$/`), it throws at src/jspdf.js:1668. So a color string must be a CSS color name RGBColor can parse, a hex string, or a plain decimal number string — anything else is rejected.","triggerScenarios":"`doc.setFillColor('redish')` (typo of a color name); `doc.setTextColor('rgb(300,0,0)')` (invalid RGB values RGBColor rejects); `doc.setDrawColor('hsl(0,100%,50%)')` if RGBColor can't parse it; passing a unit-bearing string like `'255px'`.","commonSituations":"User-typed color strings; values from a color picker that emit unsupported formats; concatenating strings that produce malformed color tokens; passing DOM style values with units.","solutions":["Use a hex string (`'#ff0000'`) or a recognized CSS color name (`'red'`) that RGBColor understands.","Pass numeric channels instead of a string: `doc.setFillColor(255, 0, 0)`.","Validate/normalize color input with a library (e.g. tinycolor2) and convert to hex before passing.","Strip units like 'px' before sending numeric strings."],"exampleFix":"// before\n doc.setFillColor('255px');   // throws: not a valid color\n\n// after\n doc.setFillColor('#ff0000');  // hex\n // or numeric channels:\n doc.setFillColor(255, 0, 0);","handlingStrategy":"validation","validationCode":"function toPdfColor(c) {\n  if (typeof c === 'number') return c;\n  if (typeof c === 'string') {\n    if (/^#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$/.test(c)) return c; // hex\n    if (/^\\d+(\\.\\d+)?$/.test(c)) return c;                      // numeric string\n    // try CSS name; fall back to black\n    return /^#[0-9a-fA-F]{6}$/.test(c) ? c : '#000000';\n  }\n  return '#000000';\n}\ndoc.setFillColor(toPdfColor(userColor));","typeGuard":"function isValidColorString(c) {\n  if (typeof c !== 'string') return false;\n  if (/^#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$/.test(c)) return true;\n  if (/^\\d*\\.?\\d*$/.test(c)) return true;\n  return new RGBColor(c).ok; // requires rgbcolor util\n}","tryCatchPattern":"try {\n  doc.setFillColor(userColor);\n} catch (e) {\n  if (/Invalid color/.test(e.message)) {\n    doc.setFillColor('#000000');  // safe fallback\n  } else throw e;\n}","preventionTips":["Prefer hex strings ('#rrggbb') or numeric channels for colors.","Normalize user/picker color input with a library (tinycolor2) to hex.","Strip units like 'px' from numeric color strings."],"tags":["color","setfillcolor","settextcolor","setdrawcolor","validation"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}