gradient

package
v0.8.44 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 26, 2023 License: BSD-3-Clause Imports: 16 Imported by: 7

Documentation

Overview

Package gradient provides linear, radial, and conic color gradients.

Index

Examples

Constants

This section is empty.

Variables

View Source
var Cache map[string]image.Image

Cache is a cache of the image.Image results of FromString calls for each string passed to FromString.

View Source
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

func Apply(img image.Image, f func(c color.RGBA) color.RGBA) image.Image

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

func ApplyOpacity(img image.Image, opacity float32) image.Image

ApplyOpacity applies the given opacity to the given image, handling image.Uniform and Gradient as special cases.

func CopyFrom

func CopyFrom(g Gradient, cp Gradient)

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

func FromAny(val any, ctx ...colors.Context) (image.Image, error)

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

func FromString(str string, ctx ...colors.Context) (image.Image, error)

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

func ParseColorStop(stop *Stop, prev color.RGBA, par string) error

ParseColorStop parses the given color stop based on the given previous color and parent gradient string.

func RayCircleIntersectionF

func RayCircleIntersectionF(s1, s2, c mat32.Vec2, r float32) (pt mat32.Vec2, intersects bool)

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

func ReadFraction(v string) (float32, error)

ReadFraction reads a decimal value from the given string.

func ReadGradAttr

func ReadGradAttr(g Gradient, attr xml.Attr) error

ReadGradAttr reads the given xml attribute onto the given gradient.

func ReadXML

func ReadXML(g *Gradient, reader io.Reader) error

ReadXML reads an XML-formatted gradient color from the given io.Reader and sets the properties of the given gradient accordingly.

func UnmarshalXML

func UnmarshalXML(g *Gradient, decoder *xml.Decoder, se xml.StartElement) error

UnmarshalXML parses the given XML gradient color data and sets the properties of the given gradient accordingly.

func XMLAttr

func XMLAttr(name string, attrs []xml.Attr) string

XMLAttr searches for given attribute in slice of xml attributes -- returns "" if not found

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) AddStop

func (b *Base) AddStop(color color.RGBA, pos float32)

AddStop adds a new stop with the given color and position to the gradient.

func (*Base) AsBase

func (b *Base) AsBase() *Base

AsBase returns the Base of the gradient

func (*Base) BlendStops

func (b *Base) BlendStops(pos float32, s1, s2 Stop, flip bool) color.Color

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) Bounds

func (b *Base) Bounds() image.Rectangle

Bounds returns the bounds of the gradient image, which are infinite.

func (*Base) ColorModel

func (b *Base) ColorModel() color.Model

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

func (b *Base) CopyStopsFrom(cp *Base)

CopyStopsFrom copies the base gradient stops from the given base gradient

func (*Base) GetColor

func (b *Base) GetColor(pos float32) color.Color

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

func (t *Base) SetBox(v mat32.Box2) *Base

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

func (t *Base) SetSpread(v Spreads) *Base

SetSpread sets the [Base.Spread]: the spread method used for the gradient if it stops before the end

func (*Base) SetTransform

func (t *Base) SetTransform(v mat32.Mat2) *Base

SetTransform sets the [Base.Transform]: Transform is the transformation matrix applied to the gradient's points.

func (*Base) SetUnits

func (t *Base) SetUnits(v Units) *Base

SetUnits sets the [Base.Units]: the units to use for the gradient

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.

func CopyOf

func CopyOf(g Gradient) Gradient

CopyOf returns a copy of the given gradient, making copies of the stops instead of re-using pointers.

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 NewLinear

func NewLinear() *Linear

NewLinear returns a new left-to-right Linear gradient.

func (*Linear) AddStop

func (l *Linear) AddStop(color color.RGBA, pos float32) *Linear

AddStop adds a new stop with the given color and position to the linear gradient.

func (*Linear) At

func (l *Linear) At(x, y int) color.Color

At returns the color of the linear gradient at the given point

func (*Linear) SetBlend

func (t *Linear) SetBlend(v colors.BlendTypes) *Linear

SetBlend sets the [Linear.Blend]

func (*Linear) SetBox

func (t *Linear) SetBox(v mat32.Box2) *Linear

SetBox sets the [Linear.Box]

func (*Linear) SetEnd

func (t *Linear) SetEnd(v mat32.Vec2) *Linear

SetEnd sets the [Linear.End]: the ending point of the gradient (x2 and y2 in SVG)

func (*Linear) SetSpread

func (t *Linear) SetSpread(v Spreads) *Linear

SetSpread sets the [Linear.Spread]

func (*Linear) SetStart

func (t *Linear) SetStart(v mat32.Vec2) *Linear

SetStart sets the [Linear.Start]: the starting point of the gradient (x1 and y1 in SVG)

func (*Linear) SetString

func (l *Linear) SetString(str string) error

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

func (t *Linear) SetTransform(v mat32.Mat2) *Linear

SetTransform sets the [Linear.Transform]

func (*Linear) SetUnits

func (t *Linear) SetUnits(v Units) *Linear

SetUnits sets the [Linear.Units]

func (*Linear) Update

func (l *Linear) Update()

Update updates the computed fields of the gradient. It 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 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 NewRadial

func NewRadial() *Radial

NewRadial returns a new centered Radial gradient.

func (*Radial) AddStop

func (r *Radial) AddStop(color color.RGBA, pos float32) *Radial

AddStop adds a new stop with the given color and position to the radial gradient.

func (*Radial) At

func (r *Radial) At(x, y int) color.Color

At returns the color of the radial gradient at the given point

func (*Radial) SetBlend

func (t *Radial) SetBlend(v colors.BlendTypes) *Radial

SetBlend sets the [Radial.Blend]

func (*Radial) SetBox

func (t *Radial) SetBox(v mat32.Box2) *Radial

SetBox sets the [Radial.Box]

func (*Radial) SetCenter

func (t *Radial) SetCenter(v mat32.Vec2) *Radial

SetCenter sets the [Radial.Center]: the center point of the gradient (cx and cy in SVG)

func (*Radial) SetFocal

func (t *Radial) SetFocal(v mat32.Vec2) *Radial

SetFocal sets the [Radial.Focal]: the focal point of the gradient (fx and fy in SVG)

func (*Radial) SetRadius

func (t *Radial) SetRadius(v mat32.Vec2) *Radial

SetRadius sets the [Radial.Radius]: the radius of the gradient (rx and ry in SVG)

func (*Radial) SetSpread

func (t *Radial) SetSpread(v Spreads) *Radial

SetSpread sets the [Radial.Spread]

func (*Radial) SetString

func (r *Radial) SetString(str string) error

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

func (t *Radial) SetTransform(v mat32.Mat2) *Radial

SetTransform sets the [Radial.Transform]

func (*Radial) SetUnits

func (t *Radial) SetUnits(v Units) *Radial

SetUnits sets the [Radial.Units]

func (*Radial) Update

func (r *Radial) Update()

Update updates the computed fields of the gradient. It 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) Desc

func (i Spreads) Desc() string

Desc returns the description of the Spreads value.

func (Spreads) Int64

func (i Spreads) Int64() int64

Int64 returns the Spreads value as an int64.

func (Spreads) IsValid

func (i Spreads) IsValid() bool

IsValid returns whether the value is a valid option for type Spreads.

func (Spreads) MarshalText

func (i Spreads) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Spreads) SetInt64

func (i *Spreads) SetInt64(in int64)

SetInt64 sets the Spreads value from an int64.

func (*Spreads) SetString

func (i *Spreads) SetString(s string) error

SetString sets the Spreads value from its string representation, and returns an error if the string is invalid.

func (Spreads) String

func (i Spreads) String() string

String returns the string representation of this Spreads value.

func (*Spreads) UnmarshalText

func (i *Spreads) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Spreads) Values

func (i Spreads) Values() []enums.Enum

Values returns all possible values for the type Spreads.

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) Desc

func (i Units) Desc() string

Desc returns the description of the Units value.

func (Units) Int64

func (i Units) Int64() int64

Int64 returns the Units value as an int64.

func (Units) IsValid

func (i Units) IsValid() bool

IsValid returns whether the value is a valid option for type Units.

func (Units) MarshalText

func (i Units) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Units) SetInt64

func (i *Units) SetInt64(in int64)

SetInt64 sets the Units value from an int64.

func (*Units) SetString

func (i *Units) SetString(s string) error

SetString sets the Units value from its string representation, and returns an error if the string is invalid.

func (Units) String

func (i Units) String() string

String returns the string representation of this Units value.

func (*Units) UnmarshalText

func (i *Units) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Units) Values

func (i Units) Values() []enums.Enum

Values returns all possible values for the type Units.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL