{"record":{"id":"8d9976ac8aad204b","repo":"d2lang/d2","slug":"expected-opacity-to-be-a-number-between-0-0-and","errorCode":null,"errorMessage":"expected \"opacity\" to be a number between 0.0 and 1.0","messagePattern":"expected \"opacity\" to be a number between 0\\.0 and 1\\.0","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"d2graph/d2graph.go","lineNumber":323,"sourceCode":"\tTextTransform *Scalar `json:\"textTransform,omitempty\"`\n}\n\n// NoneTextTransform will return a boolean if the text should not have any\n// transformation applied. This should overwrite theme specific transformations\n// like `CapsLock` from the `terminal` theme.\nfunc (s Style) NoneTextTransform() bool {\n\treturn s.TextTransform != nil && s.TextTransform.Value == \"none\"\n}\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","sourceCodeStart":305,"sourceCodeEnd":341,"githubUrl":"https://github.com/d2lang/d2/blob/0d69dca6f532ceaeacd615d35d1eaa41a238ffdb/d2graph/d2graph.go#L305-L341","documentation":"The D2 style validator requires the 'opacity' style key to be a float between 0.0 and 1.0. This error is returned when the value fails ParseFloat, is NaN or Inf, or is outside [0,1]. Thrown from the style-key switch in d2graph.ApplyKey-like style application.","triggerScenarios":"Setting a shape's style 'opacity' (via d2graph style application with s.Opacity != nil) to a value like \"1.5\", \"abc\", \"NaN\", \"Inf\", or \"-0.2\".","commonSituations":"D2 diagram authors writing `shape.style.opacity: 100` (expecting percentage) or a typo like \"0,5\" in a .d2 file; programmatic graph manipulation assigning invalid opacity strings.","solutions":["Set opacity to a decimal fraction between 0.0 and 1.0, e.g. 0.5 for 50%.","Convert percentage values by dividing by 100 (100 -> 1.0).","Fix numeric typos (commas as decimal separators, stray units)."],"exampleFix":"// before\nx.style.opacity: 100\n// after\nx.style.opacity: 1.0","handlingStrategy":"validation","validationCode":"f, err := strconv.ParseFloat(v, 64)\nif err != nil || math.IsNaN(f) || math.IsInf(f, 0) || f < 0 || f > 1 {\n    return fmt.Errorf(\"opacity %q must be between 0.0 and 1.0\", v)\n}","typeGuard":"func validOpacity(s string) bool { f, err := strconv.ParseFloat(s, 64); return err == nil && !math.IsNaN(f) && !math.IsInf(f, 0) && f >= 0 && f <= 1 }","tryCatchPattern":"if err := applyStyle(key, value); err != nil {\n    if strings.Contains(err.Error(), \"opacity\") {\n        // fall back to a safe default\n        value = \"1.0\"\n    }\n}","preventionTips":["Express opacity as a fraction (0.0–1.0), never a percentage.","Pre-validate with ParseFloat plus NaN/Inf/range checks mirroring d2graph.","Clamp computed opacities to [0,1] before writing them into the graph."],"tags":["validation","d2graph","style"],"backgroundTag":"invalid-style-value","analyzedSha":"0d69dca6f532ceaeacd615d35d1eaa41a238ffdb","analyzedAt":"2026-08-31T12:19:21.182Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}