gradient_linear.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 *LinearGradientBrush) AddColorStop(offset float64, c RGBA) *LinearGradientBrush {
	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 *LinearGradientBrush) ColorAt(x, y float64) RGBA {
	// Handle zero-length gradient (start == end)
	dx := g.End.X - g.Start.X
	dy := g.End.Y - g.Start.Y
	lengthSq := dx*dx + dy*dy

	if lengthSq == 0 {
		return firstStopColor(g.ensureSorted())
	}

	// Project point onto the gradient line
	// t = dot(P - Start, End - Start) / |End - Start|^2
	px := x - g.Start.X
	py := y - g.Start.Y
	t := (px*dx + py*dy) / lengthSq

	return colorAtOffset(g.ensureSorted(), t, g.Extend)
}

func NewLinearGradientBrush

NewLinearGradientBrush creates a new linear gradient from (x0, y0) to (x1, y1).

func NewLinearGradientBrush(x0, y0, x1, y1 float64) *LinearGradientBrush {
	return &LinearGradientBrush{
		Start:	Point{X: x0, Y: y0},
		End:	Point{X: x1, Y: y1},
		Stops:	nil,
		Extend:	ExtendPad,
	}
}

func SetExtend

SetExtend sets the extend mode for the gradient.

Returns the gradient for method chaining.

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

Structs

type LinearGradientBrush struct

LinearGradientBrush represents a linear color transition between two points.

It implements the Brush interface and supports multiple color stops,

proper sRGB color interpolation, and configurable extend modes.

 

LinearGradientBrush follows the vello/peniko gradient model, providing

professional-quality gradients for 2D graphics.

 

Example:

 

gradient := gg.NewLinearGradientBrush(0, 0, 100, 0).

AddColorStop(0, gg.Red).

AddColorStop(0.5, gg.Yellow).

AddColorStop(1, gg.Blue)

ctx.SetFillBrush(gradient)

type LinearGradientBrush struct {
	Start	Point		// Start point of the gradient
	End	Point		// End point of the gradient
	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
}