Documentation ¶
Overview ¶
Package gradient provides linear, radial, and conic color gradients.
Index ¶
- Variables
- func Apply(img image.Image, f ApplyFunc) image.Image
- func ApplyOpacity(img image.Image, opacity float32) image.Image
- func CopyFrom(g Gradient, cp Gradient)
- func FromAny(val any, ctx ...colors.Context) (image.Image, error)
- func FromString(str string, ctx ...colors.Context) (image.Image, error)
- func ReadXML(g *Gradient, reader io.Reader) error
- func UnmarshalXML(g *Gradient, decoder *xml.Decoder, se xml.StartElement) error
- func XMLAttr(name string, attrs []xml.Attr) string
- type Applier
- type ApplyFunc
- type ApplyFuncs
- type Base
- func (b *Base) AddStop(color color.RGBA, pos float32, opacity ...float32) *Base
- func (b *Base) ApplyOpacityToStops(opacity float32)
- func (b *Base) AsBase() *Base
- func (b *Base) Bounds() image.Rectangle
- func (b *Base) ColorModel() color.Model
- func (b *Base) CopyStopsFrom(cp *Base)
- func (t *Base) SetBlend(v colors.BlendTypes) *Base
- func (t *Base) SetBox(v math32.Box2) *Base
- func (t *Base) SetOpacity(v float32) *Base
- func (t *Base) SetSpread(v Spreads) *Base
- func (t *Base) SetTransform(v math32.Matrix2) *Base
- func (t *Base) SetUnits(v Units) *Base
- type Gradient
- type Linear
- func (l *Linear) AddStop(color color.RGBA, pos float32, opacity ...float32) *Linear
- func (l *Linear) At(x, y int) color.Color
- func (t *Linear) SetEnd(v math32.Vector2) *Linear
- func (t *Linear) SetStart(v math32.Vector2) *Linear
- func (l *Linear) SetString(str string) error
- func (l *Linear) Update(opacity float32, box math32.Box2, objTransform math32.Matrix2)
- type Radial
- func (r *Radial) AddStop(color color.RGBA, pos float32, opacity ...float32) *Radial
- func (r *Radial) At(x, y int) color.Color
- func (t *Radial) SetCenter(v math32.Vector2) *Radial
- func (t *Radial) SetFocal(v math32.Vector2) *Radial
- func (t *Radial) SetRadius(v math32.Vector2) *Radial
- func (r *Radial) SetString(str string) error
- func (r *Radial) Update(opacity float32, box math32.Box2, objTransform math32.Matrix2)
- type Spreads
- func (i Spreads) Desc() string
- func (i Spreads) Int64() int64
- func (i Spreads) MarshalText() ([]byte, error)
- func (i *Spreads) SetInt64(in int64)
- func (i *Spreads) SetString(s string) error
- func (i Spreads) String() string
- func (i *Spreads) UnmarshalText(text []byte) error
- func (i Spreads) Values() []enums.Enum
- type Stop
- type Units
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Cache map[string]image.Image
Cache is a cache of the image.Image results of FromString calls for each string passed to FromString.
Functions ¶
func Apply ¶
Apply returns a copy of the given image with the given color function applied to each pixel of the image, handling special cases: image.Uniform is optimized and must be preserved as such: color is directly updated. gradient.Gradient must have Update called prior to rendering, with the current bounding box.
func ApplyOpacity ¶
ApplyOpacity applies the given opacity (0-1) to the given image, handling the following special cases, and using an Applier for the general case. image.Uniform is optimized and must be preserved as such: color is directly updated. gradient.Gradient must have Update called prior to rendering, with the current bounding box. Multiplies the opacity of the stops.
func CopyFrom ¶
CopyFrom copies from the given gradient (cp) onto this gradient (g), making new copies of the stops instead of re-using pointers. It assumes the gradients are of the same type.
func FromAny ¶
FromAny returns the color image specified by the given value of any type in the given Context. It handles values of types color.Color, image.Image, and string. If no Context is provided, it uses [BaseContext] with [Transparent].
func FromString ¶
FromString parses the given CSS image/gradient/color string and returns the resulting image. FromString is based on https://www.w3schools.com/css/css3_gradients.asp. See UnmarshalXML for an XML-based version. If no Context is provied, FromString uses [BaseContext] with [Transparent].
func ReadXML ¶
ReadXML reads an XML-formatted gradient color from the given io.Reader and sets the properties of the given gradient accordingly.
func UnmarshalXML ¶
UnmarshalXML parses the given XML gradient color data and sets the properties of the given gradient accordingly.
Types ¶
type Applier ¶ added in v0.0.3
Applier is an image.Image wrapper that applies a color transformation to the output of a source image, using the given ApplyFunc
func NewApplier ¶ added in v0.0.3
NewApplier returns a new applier for given image and apply function
type ApplyFunc ¶ added in v0.0.3
ApplyFunc is a function that transforms input color to an output color.
type ApplyFuncs ¶ added in v0.0.3
type ApplyFuncs []ApplyFunc
ApplyFuncs is a slice of ApplyFunc color functions applied in order
func (*ApplyFuncs) Add ¶ added in v0.0.3
func (af *ApplyFuncs) Add(fun ApplyFunc)
Add adds a new function
func (ApplyFuncs) Apply ¶ added in v0.0.3
func (af ApplyFuncs) Apply(c color.Color) color.Color
Apply applies all functions in order to given input color
func (ApplyFuncs) Clone ¶ added in v0.0.3
func (af ApplyFuncs) Clone() ApplyFuncs
type Base ¶
type Base struct { // the stops for the gradient; use AddStop to add stops Stops []Stop `set:"-"` // the spread method used for the gradient if it stops before the end Spread Spreads // the colorspace algorithm to use for blending colors Blend colors.BlendTypes // the units to use for the gradient Units Units // the bounding box of the object with the gradient; this is used when rendering // gradients with [Units] of [ObjectBoundingBox]. Box math32.Box2 // Transform is the gradient's own transformation matrix applied to the gradient's points. // This is a property of the Gradient itself. Transform math32.Matrix2 // Opacity is the overall object opacity multiplier, applied in conjunction with the // stop-level opacity blending. Opacity float32 // ApplyFuncs contains functions that are applied to the color after gradient color is generated. // This allows for efficient StateLayer and other post-processing effects // to be applied. The Applier handles other cases, but gradients always // must have the Update function called at render time, so they must // remain Gradient types. ApplyFuncs ApplyFuncs `set:"-"` // contains filtered or unexported fields }
Base contains the data and logic common to all gradient types.
func NewBase ¶
func NewBase() Base
NewBase returns a new Base with default values. It should only be used in the New functions of gradient types.
func (*Base) AddStop ¶
AddStop adds a new stop with the given color, position, and optional opacity to the gradient.
func (*Base) ApplyOpacityToStops ¶ added in v0.0.3
ApplyOpacityToStops multiplies all stop opacities by the given opacity.
func (*Base) ColorModel ¶
ColorModel returns the color model used by the gradient image, which is color.RGBAModel
func (*Base) CopyStopsFrom ¶
CopyStopsFrom copies the base gradient stops from the given base gradient
func (*Base) SetBlend ¶
func (t *Base) SetBlend(v colors.BlendTypes) *Base
SetBlend sets the [Base.Blend]: the colorspace algorithm to use for blending colors
func (*Base) SetBox ¶
SetBox sets the [Base.Box]: the bounding box of the object with the gradient; this is used when rendering gradients with Units of ObjectBoundingBox.
func (*Base) SetOpacity ¶ added in v0.0.5
SetOpacity sets the [Base.Opacity]: Opacity is the overall object opacity multiplier, applied in conjunction with the stop-level opacity blending.
func (*Base) SetSpread ¶
SetSpread sets the [Base.Spread]: the spread method used for the gradient if it stops before the end
func (*Base) SetTransform ¶
SetTransform sets the [Base.Transform]: Transform is the gradient's own transformation matrix applied to the gradient's points. This is a property of the Gradient itself.
type Gradient ¶
type Gradient interface { image.Image // AsBase returns the [Base] of the gradient AsBase() *Base // Update updates the computed fields of the gradient, using // the given object opacity, current bounding box, and additional // object-level transform (i.e., the current painting transform), // which is applied in addition to the gradient's own Transform. // This must be called before rendering the gradient, and it should only be called then. Update(opacity float32, box math32.Box2, objTransform math32.Matrix2) }
Gradient is the interface that all gradient types satisfy.
type Linear ¶
type Linear struct { Base // the starting point of the gradient (x1 and y1 in SVG) Start math32.Vector2 // the ending point of the gradient (x2 and y2 in SVG) End math32.Vector2 // contains filtered or unexported fields }
Linear represents a linear gradient. It implements the image.Image interface.
Example ¶
NewLinear().AddStop(colors.White, 0).AddStop(colors.Black, 1)
Output:
func (*Linear) AddStop ¶
AddStop adds a new stop with the given color, position, and optional opacity to the gradient.
func (*Linear) SetEnd ¶
SetEnd sets the [Linear.End]: the ending point of the gradient (x2 and y2 in SVG)
func (*Linear) SetStart ¶
SetStart sets the [Linear.Start]: the starting point of the gradient (x1 and y1 in SVG)
func (*Linear) SetString ¶
SetString sets the linear gradient from the given CSS linear gradient string (only the part inside of "linear-gradient(...)") (see https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/linear-gradient)
func (*Linear) Update ¶
Update updates the computed fields of the gradient, using the given current bounding box, and additional object-level transform (i.e., the current painting transform), which is applied in addition to the gradient's own Transform. This must be called before rendering the gradient, and it should only be called then.
type Radial ¶
type Radial struct { Base // the center point of the gradient (cx and cy in SVG) Center math32.Vector2 // the focal point of the gradient (fx and fy in SVG) Focal math32.Vector2 // the radius of the gradient (rx and ry in SVG) Radius math32.Vector2 // contains filtered or unexported fields }
Radial represents a radial gradient. It implements the image.Image interface.
Example ¶
NewRadial().AddStop(colors.Green, 0).AddStop(colors.Yellow, 0.5).AddStop(colors.Red, 1)
Output:
func (*Radial) AddStop ¶
AddStop adds a new stop with the given color, position, and optional opacity to the gradient.
func (*Radial) SetCenter ¶
SetCenter sets the [Radial.Center]: the center point of the gradient (cx and cy in SVG)
func (*Radial) SetFocal ¶
SetFocal sets the [Radial.Focal]: the focal point of the gradient (fx and fy in SVG)
func (*Radial) SetRadius ¶
SetRadius sets the [Radial.Radius]: the radius of the gradient (rx and ry in SVG)
func (*Radial) SetString ¶
SetString sets the radial gradient from the given CSS radial gradient string (only the part inside of "radial-gradient(...)") (see https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/radial-gradient)
func (*Radial) Update ¶
Update updates the computed fields of the gradient, using the given current bounding box, and additional object-level transform (i.e., the current painting transform), which is applied in addition to the gradient's own Transform. This must be called before rendering the gradient, and it should only be called then.
type Spreads ¶
type Spreads int32 //enums:enum -transform lower
Spreads are the spread methods used when a gradient reaches its end but the object isn't yet fully filled.
const ( // Pad indicates to have the final color of the gradient fill // the object beyond the end of the gradient. Pad Spreads = iota // Reflect indicates to have a gradient repeat in reverse order // (offset 1 to 0) to fully fill an object beyond the end of the gradient. Reflect // Repeat indicates to have a gradient continue in its original order // (offset 0 to 1) by jumping back to the start to fully fill an object beyond // the end of the gradient. Repeat )
const SpreadsN Spreads = 3
SpreadsN is the highest valid value for type Spreads, plus one.
func SpreadsValues ¶
func SpreadsValues() []Spreads
SpreadsValues returns all possible values for the type Spreads.
func (Spreads) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (*Spreads) SetString ¶
SetString sets the Spreads value from its string representation, and returns an error if the string is invalid.
func (*Spreads) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.
type Stop ¶
type Stop struct { // the color of the stop. these should be fully opaque, // with opacity specified separately, for best results, as is done in SVG etc. Color color.Color // the position of the stop between 0 and 1 Pos float32 // Opacity is the 0-1 level of opacity for this stop Opacity float32 }
Stop represents a single stop in a gradient
func (*Stop) OpacityColor ¶ added in v0.0.3
func (st *Stop) OpacityColor(opacity float32, apply ApplyFuncs) color.Color
OpacityColor returns the stop color with its opacity applied, along with a global opacity multiplier
type Units ¶
type Units int32 //enums:enum -transform lower-camel
Units are the types of units used for gradient coordinate values
const ( // ObjectBoundingBox indicates that coordinate values are scaled // relative to the size of the object and are specified in the // normalized range of 0 to 1. ObjectBoundingBox Units = iota // UserSpaceOnUse indicates that coordinate values are specified // in the current user coordinate system when the gradient is used // (ie: actual SVG/core coordinates). UserSpaceOnUse )
const UnitsN Units = 2
UnitsN is the highest valid value for type Units, plus one.
func UnitsValues ¶
func UnitsValues() []Units
UnitsValues returns all possible values for the type Units.
func (Units) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (*Units) SetString ¶
SetString sets the Units value from its string representation, and returns an error if the string is invalid.
func (*Units) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.