d2lang/d2 · error
expected "opacity" to be a number between 0.0 and 1.0
Error message
expected "opacity" to be a number between 0.0 and 1.0
What it means
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.
Source
Thrown at d2graph/d2graph.go:323
TextTransform *Scalar `json:"textTransform,omitempty"`
}
// NoneTextTransform will return a boolean if the text should not have any
// transformation applied. This should overwrite theme specific transformations
// like `CapsLock` from the `terminal` theme.
func (s Style) NoneTextTransform() bool {
return s.TextTransform != nil && s.TextTransform.Value == "none"
}
func (s *Style) Apply(key, value string) error {
switch key {
case "opacity":
if s.Opacity == nil {
break
}
f, err := strconv.ParseFloat(value, 64)
if err != nil || math.IsNaN(f) || math.IsInf(f, 0) || f < 0 || f > 1 {
return errors.New(`expected "opacity" to be a number between 0.0 and 1.0`)
}
s.Opacity.Value = value
case "stroke":
if s.Stroke == nil {
break
}
if !color.ValidColor(value) {
return errors.New(`expected "stroke" to be a valid named color ("orange"), a hex code ("#f0ff3a"), or a gradient ("linear-gradient(red, blue)")`)
}
s.Stroke.Value = value
case "fill":
if s.Fill == nil {
break
}
if !color.ValidColor(value) {
return errors.New(`expected "fill" to be a valid named color ("orange"), a hex code ("#f0ff3a"), or a gradient ("linear-gradient(red, blue)")`)
}
s.Fill.Value = valueView on GitHub (pinned to 0d69dca6f5)
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).
Example fix
// before x.style.opacity: 100 // after x.style.opacity: 1.0
Defensive patterns
Strategy: validation
Validate before calling
f, err := strconv.ParseFloat(v, 64)
if err != nil || math.IsNaN(f) || math.IsInf(f, 0) || f < 0 || f > 1 {
return fmt.Errorf("opacity %q must be between 0.0 and 1.0", v)
} Type guard
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 } Try / catch
if err := applyStyle(key, value); err != nil {
if strings.Contains(err.Error(), "opacity") {
// fall back to a safe default
value = "1.0"
}
} Prevention
- 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.
When it happens
Trigger: 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".
Common situations: 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.
Related errors
- expected "stroke" to be a valid named color ("orange"), a he
- expected "fill" to be a valid named color ("orange"), a hex
- expected "fill-pattern" to be one of: %s
- expected "stroke-width" to be a number between 0 and 15
- expected "stroke-dash" to be a number between 0 and 10
AI-assisted analysis of d2lang/d2@0d69dca6f5 (2026-08-31).
Data as JSON: /api/errors/8d9976ac8aad204b.
Report an issue: GitHub.