Documentation ¶
Overview ¶
Package gradient provides linear, radial, and conic color gradients.
Index ¶
- Variables
- func Apply(img image.Image, f func(c color.RGBA) color.RGBA) image.Image
- func ApplyOpacity(img image.Image, opacity float32) image.Image
- func CopyFrom(g Gradient, cp Gradient)
- func FixGradientStops(stops []Stop)
- func FromAny(val any, ctx ...colors.Context) (image.Image, error)
- func FromString(str string, ctx ...colors.Context) (image.Image, error)
- func ParseColorStop(stop *Stop, prev color.RGBA, par string) error
- func RayCircleIntersectionF(s1, s2, c mat32.Vec2, r float32) (pt mat32.Vec2, intersects bool)
- func ReadFraction(v string) (float32, error)
- func ReadGradAttr(g Gradient, attr xml.Attr) 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 Base
- func (b *Base) AddStop(color color.RGBA, pos float32)
- func (b *Base) AsBase() *Base
- func (b *Base) BlendStops(pos float32, s1, s2 Stop, flip bool) color.Color
- func (b *Base) Bounds() image.Rectangle
- func (b *Base) ColorModel() color.Model
- func (b *Base) ComputeObjectMatrix()
- func (b *Base) CopyStopsFrom(cp *Base)
- func (b *Base) GetColor(pos float32) color.Color
- func (t *Base) SetBlend(v colors.BlendTypes) *Base
- func (t *Base) SetBox(v mat32.Box2) *Base
- func (t *Base) SetSpread(v Spreads) *Base
- func (t *Base) SetTransform(v mat32.Mat2) *Base
- func (t *Base) SetUnits(v Units) *Base
- func (b *Base) UpdateBase()
- type Gradient
- type Linear
- func (l *Linear) AddStop(color color.RGBA, pos float32) *Linear
- func (l *Linear) At(x, y int) color.Color
- func (t *Linear) SetBlend(v colors.BlendTypes) *Linear
- func (t *Linear) SetBox(v mat32.Box2) *Linear
- func (t *Linear) SetEnd(v mat32.Vec2) *Linear
- func (t *Linear) SetSpread(v Spreads) *Linear
- func (t *Linear) SetStart(v mat32.Vec2) *Linear
- func (l *Linear) SetString(str string) error
- func (t *Linear) SetTransform(v mat32.Mat2) *Linear
- func (t *Linear) SetUnits(v Units) *Linear
- func (l *Linear) Update()
- type Radial
- func (r *Radial) AddStop(color color.RGBA, pos float32) *Radial
- func (r *Radial) At(x, y int) color.Color
- func (t *Radial) SetBlend(v colors.BlendTypes) *Radial
- func (t *Radial) SetBox(v mat32.Box2) *Radial
- func (t *Radial) SetCenter(v mat32.Vec2) *Radial
- func (t *Radial) SetFocal(v mat32.Vec2) *Radial
- func (t *Radial) SetRadius(v mat32.Vec2) *Radial
- func (t *Radial) SetSpread(v Spreads) *Radial
- func (r *Radial) SetString(str string) error
- func (t *Radial) SetTransform(v mat32.Mat2) *Radial
- func (t *Radial) SetUnits(v Units) *Radial
- func (r *Radial) Update()
- type Spreads
- func (i Spreads) Desc() string
- func (i Spreads) Int64() int64
- func (i Spreads) IsValid() bool
- 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
- func (i Units) Desc() string
- func (i Units) Int64() int64
- func (i Units) IsValid() bool
- func (i Units) MarshalText() ([]byte, error)
- func (i *Units) SetInt64(in int64)
- func (i *Units) SetString(s string) error
- func (i Units) String() string
- func (i *Units) UnmarshalText(text []byte) error
- func (i Units) Values() []enums.Enum
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.
var GradientDegToSides = map[string]string{
"0deg": "top",
"360deg": "top",
"45deg": "top right",
"-315deg": "top right",
"90deg": "right",
"-270deg": "right",
"135deg": "bottom right",
"-225deg": "bottom right",
"180deg": "bottom",
"-180deg": "bottom",
"225deg": "bottom left",
"-135deg": "bottom left",
"270deg": "left",
"-90deg": "left",
"315deg": "top left",
"-45deg": "top left",
}
GradientDegToSides maps gradient degree notation to side notation
Functions ¶
func Apply ¶
Apply returns a copy of the given image with the given color function applied to each pixel of the image. It handles image.Uniform and Gradient as special cases, only calling the function for the uniform color and each stop color, respectively.
func ApplyOpacity ¶
ApplyOpacity applies the given opacity to the given image, handling image.Uniform and Gradient as special cases.
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 FixGradientStops ¶
func FixGradientStops(stops []Stop)
FixGradientStops applies the CSS rules to regularize the given gradient stops: https://www.w3.org/TR/css3-images/#color-stop-syntax
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 ParseColorStop ¶
ParseColorStop parses the given color stop based on the given previous color and parent gradient string.
func RayCircleIntersectionF ¶
RayCircleIntersectionF calculates in floating point the points of intersection of a ray starting at s2 passing through s1 and a circle in fixed point. Returns intersects == false if no solution is possible. If two solutions are possible, the point closest to s2 is returned
func ReadFraction ¶
ReadFraction reads a decimal value from the given string.
func ReadGradAttr ¶
ReadGradAttr reads the given xml attribute onto the given gradient.
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 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 mat32.Box2 // Transform is the transformation matrix applied to the gradient's points. Transform mat32.Mat2 // ObjectMatrix is the computed effective object transformation matrix for a gradient // with [Units] of [ObjectBoundingBox]. It should not be set by end users. ObjectMatrix mat32.Mat2 `set:"-"` }
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) BlendStops ¶
BlendStops blends the given two gradient stops together based on the given position, using the gradient's blending algorithm. If flip is true, it flips the given position.
func (*Base) ColorModel ¶
ColorModel returns the color model used by the gradient image, which is color.RGBAModel
func (*Base) ComputeObjectMatrix ¶
func (b *Base) ComputeObjectMatrix()
ComputeObjectMatrix computes the effective object transformation matrix for a gradient with Units of ObjectBoundingBox, setting [Base.ObjectMatrix].
func (*Base) CopyStopsFrom ¶
CopyStopsFrom copies the base gradient stops from the given base gradient
func (*Base) GetColor ¶
GetColor returns the color at the given normalized position along the gradient's stops using its spread method and blend algorithm.
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) 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 transformation matrix applied to the gradient's points.
func (*Base) UpdateBase ¶
func (b *Base) UpdateBase()
UpdateBase updates the computed fields of the base gradient. It should only be called by other gradient types in their [Gradient.Update] functions. It is named UpdateBase to avoid people accidentally calling it instead of [Gradient.Update].
type Gradient ¶
type Gradient interface { image.Image // AsBase returns the [Base] of the gradient AsBase() *Base // Update updates the computed fields of the gradient. It must be // called before rendering the gradient, and it should only be called then. Update() }
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 mat32.Vec2 // the ending point of the gradient (x2 and y2 in SVG) End mat32.Vec2 // EffStart is the computed effective transformed starting point of the gradient. // It should not be set by end users. EffStart mat32.Vec2 `set:"-"` // EffEnd is the computed effective transformed ending point of the gradient. // It should not be set by end users. EffEnd mat32.Vec2 `set:"-"` }
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 and position to the linear gradient.
func (*Linear) SetBlend ¶
func (t *Linear) SetBlend(v colors.BlendTypes) *Linear
SetBlend sets the [Linear.Blend]
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) SetTransform ¶
SetTransform sets the [Linear.Transform]
type Radial ¶
type Radial struct { Base // the center point of the gradient (cx and cy in SVG) Center mat32.Vec2 // the focal point of the gradient (fx and fy in SVG) Focal mat32.Vec2 // the radius of the gradient (rx and ry in SVG) Radius mat32.Vec2 }
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 and position to the radial gradient.
func (*Radial) SetBlend ¶
func (t *Radial) SetBlend(v colors.BlendTypes) *Radial
SetBlend sets the [Radial.Blend]
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) SetTransform ¶
SetTransform sets the [Radial.Transform]
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 Color color.RGBA // the position of the stop between 0 and 1 Pos float32 }
Stop represents a single stop in a gradient
type Units ¶
type Units int32 //enums:enum -transform camel-lower
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/gi 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.