Skip to content

Colors & Gradients

Taco de Wolff edited this page Apr 11, 2023 · 2 revisions

Colors use the color.RGBA type internally and can be created by:

red := canvas.RGBA(255,0,0,0.0)
blue := canvas.Hex("#00ff00")

You can set a color as filling and stroking paint using the following functions on Context:

SetFill(ipaint interface{})
SetFillColor(col color.Color)
SetStroke(ipaint interface{})
SetStrokeColor(col color.Color)

Gradients

SetFillGradient(gradient Gradient)
SetStrokeGradient(gradient Gradient)

The linear and radial gradients are supported. Both define a color gradient between t ∈ [0,1] with various color stops at positions in the interval. Linear gradients define a gradient between a start and end point, while radial gradients define a gradient between two circles (defined by a center point and a radius) with one circle inside the other (but not necessarily concentric).

lin := canvas.NewLinearGradient(canvas.Point{0.0, 0.0}, canvas.Point{10.0,0.0})
lin.Add(0.0, canvas.Hex("#ff0000"))
lin.Add(0.5, canvas.Hex("#00ff00"))
lin.Add(1.0, canvas.Hex("#0000ff"))
ctx.SetFillGradient(lin)

rad := canvas.NewRadialGradient(Point{5.0,5.0}, 0.0, Point{10.0,5.0}, 10.0)
rad.Add(0.0, canvas.Red)
rad.Add(1.0, canvas.Blue)
ctx.SetStrokeGradient(rad)

Color spaces and gamma correction

Predefined colors

Colors

Source code

Clone this wiki locally