{"id":"232a2dd3f45f04bc","repo":"gohugoio/hugo","slug":"invalid-color-code-q","errorCode":null,"errorMessage":"invalid color code: %q","messagePattern":"invalid color code: %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"resources/images/color.go","lineNumber":169,"sourceCode":"\t\treturn vv.ColorGo(), true, nil\n\tdefault:\n\t\ts, ok := hstrings.ToString(v)\n\t\tif !ok {\n\t\t\treturn nil, false, nil\n\t\t}\n\t\tc, err := hexStringToColorGo(s)\n\t\tif err != nil {\n\t\t\treturn nil, false, err\n\t\t}\n\t\treturn c, true, nil\n\t}\n}\n\nfunc hexStringToColorGo(s string) (color.Color, error) {\n\ts = strings.TrimPrefix(s, \"#\")\n\n\tif len(s) != 3 && len(s) != 4 && len(s) != 6 && len(s) != 8 {\n\t\treturn nil, fmt.Errorf(\"invalid color code: %q\", s)\n\t}\n\n\ts = strings.ToLower(s)\n\n\tif len(s) == 3 || len(s) == 4 {\n\t\tvar v strings.Builder\n\t\tfor _, r := range s {\n\t\t\tv.WriteString(string(r) + string(r))\n\t\t}\n\t\ts = v.String()\n\t}\n\n\t// Standard colors.\n\tif s == \"ffffff\" {\n\t\treturn color.White, nil\n\t}\n\n\tif s == \"000000\" {","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/resources/images/color.go#L151-L187","documentation":"hexStringToColorGo parses hex color strings used across Hugo's image processing (bgColor in imaging config, images.Filter colors, overlays, etc.). After stripping an optional leading `#`, the string must be exactly 3, 4, 6, or 8 hex digits (RGB, RGBA, RRGGBB, RRGGBBAA); any other length is rejected immediately with this error before digit validation.","triggerScenarios":"Passing a malformed hex color like \"#ff00\"→ok(4) but \"#ff0000f\" (7 digits), \"white\", \"rgb(255,0,0)\", or an empty string to imaging `bgColor`, `images.Text`/`images.Padding` color options, or image processing spec strings like `#bgcolor`.","commonSituations":"Using CSS named colors or rgb() notation instead of hex; truncated/extra digits from copy-paste; template variables that render empty; double `##` prefixes (only one `#` is trimmed).","solutions":["Use a valid hex code of 3, 4, 6, or 8 digits, e.g. `#fff`, `#ffffff`, `#ffffffff`.","Replace named/functional CSS colors (`white`, `rgb(...)`) with their hex equivalents.","If the value comes from a template variable, guard against empty or unrendered values."],"exampleFix":"# before (hugo.toml)\n[imaging]\nbgColor = \"white\"\n\n# after\n[imaging]\nbgColor = \"#ffffff\"","handlingStrategy":"validation","validationCode":"re := regexp.MustCompile(`^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$`)\nif !re.MatchString(color) { /* fix hex code */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use 3, 4, 6, or 8-digit hex codes (e.g. #fff, #ffffff, #ffffffff) for image filter/background colors","Don't pass CSS color names or rgb() syntax — only hex is accepted","Validate user-supplied colors in templates before passing to images.Filter/Fill"],"tags":["hugo","images","color","config"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}