{"record":{"id":"13956a62215d9a32","repo":"d2lang/d2","slug":"expected-stroke-to-be-a-valid-named-color-oran","errorCode":null,"errorMessage":"expected \"stroke\" to be a valid named color (\"orange\"), a hex code (\"#f0ff3a\"), or a gradient (\"linear-gradient(red, blue)\")","messagePattern":"expected \"stroke\" to be a valid named color \\(\"orange\"\\), a hex code \\(\"#f0ff3a\"\\), or a gradient \\(\"linear-gradient\\(red, blue\\)\"\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"d2graph/d2graph.go","lineNumber":331,"sourceCode":"}\n\nfunc (s *Style) Apply(key, value string) error {\n\tswitch key {\n\tcase \"opacity\":\n\t\tif s.Opacity == nil {\n\t\t\tbreak\n\t\t}\n\t\tf, err := strconv.ParseFloat(value, 64)\n\t\tif err != nil || math.IsNaN(f) || math.IsInf(f, 0) || f < 0 || f > 1 {\n\t\t\treturn errors.New(`expected \"opacity\" to be a number between 0.0 and 1.0`)\n\t\t}\n\t\ts.Opacity.Value = value\n\tcase \"stroke\":\n\t\tif s.Stroke == nil {\n\t\t\tbreak\n\t\t}\n\t\tif !color.ValidColor(value) {\n\t\t\treturn errors.New(`expected \"stroke\" to be a valid named color (\"orange\"), a hex code (\"#f0ff3a\"), or a gradient (\"linear-gradient(red, blue)\")`)\n\t\t}\n\t\ts.Stroke.Value = value\n\tcase \"fill\":\n\t\tif s.Fill == nil {\n\t\t\tbreak\n\t\t}\n\t\tif !color.ValidColor(value) {\n\t\t\treturn errors.New(`expected \"fill\" to be a valid named color (\"orange\"), a hex code (\"#f0ff3a\"), or a gradient (\"linear-gradient(red, blue)\")`)\n\t\t}\n\t\ts.Fill.Value = value\n\tcase \"fill-pattern\":\n\t\tif s.FillPattern == nil {\n\t\t\tbreak\n\t\t}\n\t\tif !go2.Contains(d2ast.FillPatterns, strings.ToLower(value)) {\n\t\t\treturn fmt.Errorf(`expected \"fill-pattern\" to be one of: %s`, strings.Join(d2ast.FillPatterns, \", \"))\n\t\t}\n\t\ts.FillPattern.Value = value","sourceCodeStart":313,"sourceCodeEnd":349,"githubUrl":"https://github.com/d2lang/d2/blob/0d69dca6f532ceaeacd615d35d1eaa41a238ffdb/d2graph/d2graph.go#L313-L349","documentation":"The 'stroke' style key must be a valid color as recognized by color.ValidColor: a named color, a hex code, or a gradient expression. This error is returned when the stroke value fails that check. Note the check is skipped when s.Stroke is nil (key not present).","triggerScenarios":"Setting style 'stroke' to a value that is not a named color, hex code (#rrggbb), or linear-gradient(...) — e.g. `stroke: rgb(255,0,0)` if unsupported, `stroke: red-ish`, or empty string.","commonSituations":"D2 authors using CSS color functions or formats the D2 color parser doesn't accept, misspelling a color name, or omitting the '#' in hex codes.","solutions":["Use a supported format: named color (\"orange\"), hex (\"#f0ff3a\"), or gradient (\"linear-gradient(red, blue)\").","Add the '#' prefix to hex codes and verify 3/6 hex digits.","Check the value against color.ValidColor before applying."],"exampleFix":"// before\nx.style.stroke: rgb(255, 0, 0)\n// after\nx.style.stroke: #ff0000","handlingStrategy":"validation","validationCode":"if !color.ValidColor(v) {\n    return fmt.Errorf(\"stroke %q is not a named color, hex code, or gradient\", v)\n}","typeGuard":"func validStroke(v string) bool { return color.ValidColor(v) }","tryCatchPattern":"if err := applyStyle(\"stroke\", value); err != nil {\n    if strings.Contains(err.Error(), \"stroke\") {\n        value = \"#000000\" // fallback\n    }\n}","preventionTips":["Use only named colors, #hex codes, or linear-gradient(...) in D2 styles.","Check values with color.ValidColor before assigning.","Avoid CSS-only formats like rgb()/hsl() unless D2 accepts them."],"tags":["validation","d2graph","color","style"],"backgroundTag":"invalid-color-value","analyzedSha":"0d69dca6f532ceaeacd615d35d1eaa41a238ffdb","analyzedAt":"2026-08-31T12:19:21.182Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}