gradient

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2024 License: BSD-3-Clause Imports: 12 Imported by: 2

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.

Functions

func Apply

func Apply(img image.Image, f ApplyFunc) image.Image

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

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

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

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 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 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 Applier added in v0.0.3

type Applier struct {
	image.Image
	Func ApplyFunc
}

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

func NewApplier(img image.Image, fun func(c color.Color) color.Color) *Applier

NewApplier returns a new applier for given image and apply function

func (*Applier) At added in v0.0.3

func (ap *Applier) At(x, y int) color.Color

type ApplyFunc added in v0.0.3

type ApplyFunc func(c color.Color) color.Color

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

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

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

func (*Base) ApplyOpacityToStops added in v0.0.3

func (b *Base) ApplyOpacityToStops(opacity float32)

ApplyOpacityToStops multiplies all stop opacities by the given opacity.

func (*Base) AsBase

func (b *Base) AsBase() *Base

AsBase returns the Base of the gradient

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

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

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

func (t *Base) SetBox(v math32.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) SetOpacity added in v0.0.5

func (t *Base) SetOpacity(v float32) *Base

SetOpacity sets the [Base.Opacity]: Opacity is the overall object opacity multiplier, applied in conjunction with the stop-level opacity blending.

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 math32.Matrix2) *Base

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.

func (*Base) SetUnits

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

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

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.

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 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 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, opacity ...float32) *Linear

AddStop adds a new stop with the given color, position, and optional opacity to the 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) SetEnd

func (t *Linear) SetEnd(v math32.Vector2) *Linear

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

func (*Linear) SetStart

func (t *Linear) SetStart(v math32.Vector2) *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) Update

func (l *Linear) Update(opacity float32, box math32.Box2, objTransform math32.Matrix2)

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 NewRadial

func NewRadial() *Radial

NewRadial returns a new centered Radial gradient.

func (*Radial) AddStop

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

AddStop adds a new stop with the given color, position, and optional opacity to the 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) SetCenter

func (t *Radial) SetCenter(v math32.Vector2) *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 math32.Vector2) *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 math32.Vector2) *Radial

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

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

func (r *Radial) Update(opacity float32, box math32.Box2, objTransform math32.Matrix2)

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