d2lang/d2 · error

no color stops in gradient

Error message

no color stops in gradient

What it means

ParseGradient validates CSS gradient parameter lists. After a direction token (e.g. '45deg' or 'to right' for linear, 'circle'/'ellipse' for radial) is consumed, at least one color stop must remain; if the direction token is the only parameter, the gradient would have nothing to render, so it returns this error.

Source

Thrown at lib/color/gradient.go:54

	params := matches[2]

	gradient := Gradient{
		Type: strings.TrimSuffix(gradientType, "-gradient"),
	}

	paramList := splitParams(params)

	if len(paramList) == 0 {
		return Gradient{}, errors.New("no parameters in gradient")
	}

	firstParam := strings.TrimSpace(paramList[0])

	if gradient.Type == "linear" && (strings.HasSuffix(firstParam, "deg") || strings.HasPrefix(firstParam, "to ")) {
		gradient.Direction = firstParam
		colorStops := paramList[1:]
		if len(colorStops) == 0 {
			return Gradient{}, errors.New("no color stops in gradient")
		}
		gradient.ColorStops = parseColorStops(colorStops)
	} else if gradient.Type == "radial" && (firstParam == "circle" || firstParam == "ellipse") {
		gradient.Direction = firstParam
		colorStops := paramList[1:]
		if len(colorStops) == 0 {
			return Gradient{}, errors.New("no color stops in gradient")
		}
		gradient.ColorStops = parseColorStops(colorStops)
	} else {
		gradient.ColorStops = parseColorStops(paramList)
	}
	gradient.ID = UniqueGradientID(cssGradient)

	return gradient, nil
}

func splitParams(params string) []string {

View on GitHub (pinned to 0d69dca6f5)

Solutions

  1. Add at least one color stop after the direction, e.g. 'linear-gradient(45deg, #fff, #000)'
  2. Remove the trailing direction-only parameter list or the whole gradient value if a plain color was intended
  3. Validate the gradient string with ValidColor before use and surface a clearer message to the user

Example fix

// before
ParseGradient("linear-gradient(45deg)")
// after
ParseGradient("linear-gradient(45deg, blue, red)")
Defensive patterns

Strategy: validation

Validate before calling

params := strings.Split(inner, ",")
first := strings.TrimSpace(params[0])
if (strings.HasSuffix(first, "deg") || strings.HasPrefix(first, "to ")) && len(params) < 2 {
    return errors.New("gradient needs at least one color stop after direction")
}

Try / catch

g, err := ParseGradient(s)
if err != nil {
    // fallback: treat s as a plain color or surface the validation message
}

Prevention

When it happens

Trigger: Calling ParseGradient (indirectly via ValidColor) with a gradient string like 'linear-gradient(45deg)' or 'radial-gradient(circle)' where the parameter list contains only a direction and zero color stops.

Common situations: Hand-written CSS color values with a truncated gradient, templates interpolating an empty color list after a direction, or config files where the stops were accidentally dropped.

Related errors


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