gradient_radial.go

Functions Structs

Functions

func AddColorStop

AddColorStop adds a color stop at the specified offset.

Offset should be in the range [0, 1].

Returns the gradient for method chaining.

func (g *RadialGradientBrush) AddColorStop(offset float64, c RGBA) *RadialGradientBrush {
	g.Stops = append(g.Stops, ColorStop{Offset: offset, Color: c})
	g.sorted = false	// invalidate cached sort
	return g
}

func ColorAt

ColorAt returns the color at the given point.

Implements the Pattern and Brush interfaces.

func (g *RadialGradientBrush) ColorAt(x, y float64) RGBA {
	// Handle degenerate gradient (zero radius difference)
	radiusDiff := g.EndRadius - g.StartRadius
	if radiusDiff == 0 {
		return firstStopColor(g.ensureSorted())
	}

	t := g.computeT(x, y)
	return colorAtOffset(g.ensureSorted(), t, g.Extend)
}

func NewRadialGradientBrush

NewRadialGradientBrush creates a new radial gradient.

The gradient transitions from startRadius to endRadius around (cx, cy).

Focus defaults to center.

func NewRadialGradientBrush(cx, cy, startRadius, endRadius float64) *RadialGradientBrush {
	center := Point{X: cx, Y: cy}
	return &RadialGradientBrush{
		Center:		center,
		Focus:		center,	// Default focus at center
		StartRadius:	startRadius,
		EndRadius:	endRadius,
		Stops:		nil,
		Extend:		ExtendPad,
	}
}

func SetExtend

SetExtend sets the extend mode for the gradient.

Returns the gradient for method chaining.

func (g *RadialGradientBrush) SetExtend(mode ExtendMode) *RadialGradientBrush {
	g.Extend = mode
	return g
}

func SetFocus

SetFocus sets the focal point of the gradient.

A focal point different from center creates an asymmetric gradient.

Returns the gradient for method chaining.

func (g *RadialGradientBrush) SetFocus(fx, fy float64) *RadialGradientBrush {
	g.Focus = Point{X: fx, Y: fy}
	return g
}

Structs

type RadialGradientBrush struct

RadialGradientBrush represents a radial color transition.

Colors radiate from a focal point within a circle defined by center and end radius.

It implements the Brush interface and supports multiple color stops,

proper sRGB color interpolation, and configurable extend modes.

 

RadialGradientBrush follows the vello/peniko gradient model, supporting

both simple radial gradients (focus at center) and focal gradients

(focus offset from center for spotlight effects).

 

Example:

 

// Simple radial gradient

gradient := gg.NewRadialGradientBrush(50, 50, 0, 50).

AddColorStop(0, gg.White).

AddColorStop(1, gg.Black)

 

// Focal gradient (spotlight effect)

spotlight := gg.NewRadialGradientBrush(50, 50, 0, 50).

SetFocus(30, 30).

AddColorStop(0, gg.White).

AddColorStop(1, gg.Black)

type RadialGradientBrush struct {
	Center		Point		// Center of the gradient circle
	Focus		Point		// Focal point (can differ from center)
	StartRadius	float64		// Inner radius where gradient begins (t=0)
	EndRadius	float64		// Outer radius where gradient ends (t=1)
	Stops		[]ColorStop	// Color stops defining the gradient
	Extend		ExtendMode	// How gradient extends beyond bounds

	// sortedStops caches stops sorted by offset. Populated lazily on first
	// ColorAt call. Invalidated by AddColorStop (sorted set to false).
	sortedStops	[]ColorStop
	sorted		bool
}