{"record":{"id":"ca774c091d7ef6cd","repo":"d2lang/d2","slug":"expected-font-color-to-be-a-valid-named-color","errorCode":null,"errorMessage":"expected \"font-color\" to be a valid named color (\"orange\"), a hex code (\"#f0ff3a\"), or a gradient (\"linear-gradient(red, blue)\")","messagePattern":"expected \"font-color\" to be a valid named color \\(\"orange\"\\), a hex code \\(\"#f0ff3a\"\\), or a gradient \\(\"linear-gradient\\(red, blue\\)\"\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"d2graph/d2graph.go","lineNumber":426,"sourceCode":"\t\tif _, ok := d2fonts.D2_FONT_TO_FAMILY[strings.ToLower(value)]; !ok {\n\t\t\treturn fmt.Errorf(`\"%v\" is not a valid font in our system`, value)\n\t\t}\n\t\ts.Font.Value = strings.ToLower(value)\n\tcase \"font-size\":\n\t\tif s.FontSize == nil {\n\t\t\tbreak\n\t\t}\n\t\tf, err := strconv.Atoi(value)\n\t\tif err != nil || (f < 8 || f > 100) {\n\t\t\treturn errors.New(`expected \"font-size\" to be a number between 8 and 100`)\n\t\t}\n\t\ts.FontSize.Value = value\n\tcase \"font-color\":\n\t\tif s.FontColor == nil {\n\t\t\tbreak\n\t\t}\n\t\tif !color.ValidColor(value) {\n\t\t\treturn errors.New(`expected \"font-color\" to be a valid named color (\"orange\"), a hex code (\"#f0ff3a\"), or a gradient (\"linear-gradient(red, blue)\")`)\n\t\t}\n\t\ts.FontColor.Value = value\n\tcase \"animated\":\n\t\tif s.Animated == nil {\n\t\t\tbreak\n\t\t}\n\t\t_, err := strconv.ParseBool(value)\n\t\tif err != nil {\n\t\t\treturn errors.New(`expected \"animated\" to be true or false`)\n\t\t}\n\t\ts.Animated.Value = value\n\tcase \"bold\":\n\t\tif s.Bold == nil {\n\t\t\tbreak\n\t\t}\n\t\t_, err := strconv.ParseBool(value)\n\t\tif err != nil {\n\t\t\treturn errors.New(`expected \"bold\" to be true or false`)","sourceCodeStart":408,"sourceCodeEnd":444,"githubUrl":"https://github.com/d2lang/d2/blob/0d69dca6f532ceaeacd615d35d1eaa41a238ffdb/d2graph/d2graph.go#L408-L444","documentation":"D2 validates the \"font-color\" style with color.ValidColor, which accepts named colors (e.g. \"orange\"), hex codes (\"#f0ff3a\"), or gradient expressions (\"linear-gradient(red, blue)\"). Any other string returns this error.","triggerScenarios":"Setting style key \"font-color\" to an invalid color string, e.g. `x.font-color: lightish-blue`, `font-color: #12345` (bad hex), or a malformed gradient.","commonSituations":"Users invent color names not in the accepted palette, use CSS rgb()/rgba() functions or 3-digit hex shorthand that the validator rejects, or typo hex codes.","solutions":["Use a valid hex code like \"#f0ff3a\" (e.g. `x.font-color: #ff0000`).","Use one of D2's supported named colors (e.g. \"orange\", \"red\").","Replace rgb()/rgba() with the equivalent hex value.","Fix gradient syntax to \"linear-gradient(color1, color2)\".","Call the library's color.ValidColor (or equivalent) on user input before applying the style."],"exampleFix":"// before (D2)\nx: my shape\nx.font-color: rgb(255, 0, 0)\n// after\nx: my shape\nx.font-color: #ff0000","handlingStrategy":"validation","validationCode":"func validFontColor(v string) bool {\n\treturn color.ValidColor(v) // named color, #hex, or linear-gradient(...)\n}\n// before applying: if !validFontColor(val) { val = \"#000000\" }","typeGuard":"func isHexColor(s string) bool {\n\tif !strings.HasPrefix(s, \"#\") {\n\t\treturn false\n\t}\n\t_, err := strconv.ParseUint(strings.TrimPrefix(s, \"#\"), 16, 64)\n\treturn err == nil && (len(s) == 7 || len(s) == 9)\n}","tryCatchPattern":"if err := obj.SetStyle(\"font-color\", val); err != nil {\n\tif strings.Contains(err.Error(), `\"font-color\" to be a valid named color`) {\n\t\t// fallback to a valid default\n\t\tval = \"#000000\"\n\t}\n}","preventionTips":["Prefer hex codes (#rrggbb) or known named colors for font-color.","Convert rgb()/rgba() to hex before applying.","Run color.ValidColor on user-supplied colors first.","Check gradient syntax: linear-gradient(c1, c2), well-formed with parentheses."],"tags":["d2","style-validation","color","font-color"],"backgroundTag":"invalid-color-value","analyzedSha":"0d69dca6f532ceaeacd615d35d1eaa41a238ffdb","analyzedAt":"2026-08-31T12:19:21.182Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}