d2lang/d2 · error

expected "fill" to be a valid named color ("orange"), a hex

Error message

expected "fill" to be a valid named color ("orange"), a hex code ("#f0ff3a"), or a gradient ("linear-gradient(red, blue)")

What it means

The 'fill' style key must pass color.ValidColor — a named color, hex code, or gradient. This error is returned when the fill value is invalid. Same validation pattern as stroke, applied to the shape's background fill.

Source

Thrown at d2graph/d2graph.go:339

		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 = value
	case "fill-pattern":
		if s.FillPattern == nil {
			break
		}
		if !go2.Contains(d2ast.FillPatterns, strings.ToLower(value)) {
			return fmt.Errorf(`expected "fill-pattern" to be one of: %s`, strings.Join(d2ast.FillPatterns, ", "))
		}
		s.FillPattern.Value = value
	case "stroke-width":
		if s.StrokeWidth == nil {
			break
		}
		f, err := strconv.Atoi(value)
		if err != nil || (f < 0 || f > 15) {
			return errors.New(`expected "stroke-width" to be a number between 0 and 15`)
		}

View on GitHub (pinned to 0d69dca6f5)

Solutions

  1. Use a named color, hex code like "#f0ff3a", or "linear-gradient(color1, color2)".
  2. Validate with color.ValidColor before assigning s.Fill.Value.
  3. Correct hex length/characters and color name spelling.

Example fix

// before
x.style.fill: #12345
// after
x.style.fill: #112233
Defensive patterns

Strategy: validation

Validate before calling

if !color.ValidColor(v) {
    return fmt.Errorf("fill %q is not a named color, hex code, or gradient", v)
}

Type guard

func validFill(v string) bool { return color.ValidColor(v) }

Try / catch

if err := applyStyle("fill", value); err != nil {
    if strings.Contains(err.Error(), "fill") {
        value = "transparent" // or a known-good default
    }
}

Prevention

When it happens

Trigger: Setting style 'fill' to a non-color value, e.g. `fill: transparent-red`, `fill: #12345` (bad hex), or an unsupported function form.

Common situations: Copy-pasting CSS colors that D2 doesn't parse, typos in named colors, gradients written with unsupported syntax.

Related errors


AI-assisted analysis of d2lang/d2@0d69dca6f5 (2026-08-31). Data as JSON: /api/errors/4218045ddf95800b. Report an issue: GitHub.