{"record":{"id":"0a4bef46f68e1e93","repo":"framework7io/framework7","slug":"unexpected-hex-hex","errorCode":null,"errorMessage":"unexpected hex ${hex}","messagePattern":"unexpected hex (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/core/shared/material-color-utils.js","lineNumber":2709,"sourceCode":"      sourceColorHct: sourceColorHct,\n      variant: Variant.VIBRANT,\n      contrastLevel: contrastLevel,\n      isDark: isDark,\n      platform: platform,\n      specVersion: specVersion\n    });\n  }\n}\n\nfunction hexFromArgb(argb) {\n  const r = redFromArgb(argb), g = greenFromArgb(argb), b = blueFromArgb(argb), outParts = [ r.toString(16), g.toString(16), b.toString(16) ];\n  for (const [i, part] of outParts.entries()) 1 === part.length && (outParts[i] = \"0\" + part);\n  return \"#\" + outParts.join(\"\");\n}\n\nfunction argbFromHex(hex) {\n  const isThree = 3 === (hex = hex.replace(\"#\", \"\")).length, isSix = 6 === hex.length, isEight = 8 === hex.length;\n  if (!isThree && !isSix && !isEight) throw new Error(\"unexpected hex \" + hex);\n  let r = 0, g = 0, b = 0;\n  return isThree ? (r = parseIntHex(hex.slice(0, 1).repeat(2)), g = parseIntHex(hex.slice(1, 2).repeat(2)), \n  b = parseIntHex(hex.slice(2, 3).repeat(2))) : isSix ? (r = parseIntHex(hex.slice(0, 2)), \n  g = parseIntHex(hex.slice(2, 4)), b = parseIntHex(hex.slice(4, 6))) : isEight && (r = parseIntHex(hex.slice(2, 4)), \n  g = parseIntHex(hex.slice(4, 6)), b = parseIntHex(hex.slice(6, 8))), (255 << 24 | (255 & r) << 16 | (255 & g) << 8 | 255 & b) >>> 0;\n}\n\nfunction parseIntHex(value) {\n  return parseInt(value, 16);\n}\n\nexport { Hct, SchemeMonochrome, SchemeTonalSpot, SchemeVibrant, argbFromHex, hexFromArgb };","sourceCodeStart":2691,"sourceCodeEnd":2721,"githubUrl":"https://github.com/framework7io/framework7/blob/655759126686766530a027b2f8f369c991d63d1c/src/core/shared/material-color-utils.js#L2691-L2721","documentation":"`argbFromHex` converts a CSS-style hex string to an ARGB integer, accepting only 3, 6, or 8 hex digits (after stripping '#'). Any other length or non-hex shape triggers `throw new Error('unexpected hex ' + hex)`. It's an input-format guard so downstream parseIntHex parsing never sees malformed data.","triggerScenarios":"Calling `argbFromHex(hex)` with a string whose length is not 3/6/8 after removing '#': e.g. 4-digit #RGBA, 12-digit values, empty strings, rgb() strings, or hex values containing '0x' prefixes.","commonSituations":"Pasting colors from design tools that emit #RRGGBBAA shortened forms or uppercase '0X' prefixes; user-typed colors in a color picker config; reading colors from APIs that return rgb()/hsl() strings.","solutions":["Normalize the input before calling: trim, strip '#', reject '0x' prefix, and ensure length is 3, 6, or 8.","Convert 4- or 8-digit hex with alpha explicitly (expand #RGBA to #RGB, keep 8-digit as RRGGBBAA).","Parse rgb()/hsl() strings with a dedicated parser instead of feeding them to argbFromHex.","Validate with a regex like /^#?([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i before the call."],"exampleFix":"// before\nargbFromHex(color); // color = '#FFAA' (4-digit) -> throws\n// after\nif (!/^#?([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(color)) color = '#000000';\nconst argb = argbFromHex(color);","handlingStrategy":"validation","validationCode":"const HEX_RE = /^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;\nif (!HEX_RE.test(hex)) throw new TypeError(`argbFromHex expects 3/6/8-digit hex, got: ${hex}`);\nconst argb = argbFromHex(hex);","typeGuard":"function isParseableHex(s) {\n  return typeof s === 'string' && /^#?([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(s.trim());\n}","tryCatchPattern":"let argb;\ntry {\n  argb = argbFromHex(input);\n} catch (e) {\n  if (e.message.startsWith('unexpected hex')) {\n    console.warn('Bad hex color, using fallback', input);\n    argb = argbFromHex('#000000');\n  } else throw e;\n}","preventionTips":["Validate hex strings with a regex before calling argbFromHex.","Strip/normalize inputs: trim, remove '#', reject '0x' prefixes.","Expand 4-digit #RGBA to #RGB before parsing.","Use dedicated parsers for rgb()/hsl() color strings instead of hex conversion."],"tags":["color-parsing","hex","input-validation"],"backgroundTag":"invalid-hex-color","analyzedSha":"655759126686766530a027b2f8f369c991d63d1c","analyzedAt":"2026-09-02T19:48:40.711Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}