gradient

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 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.

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

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

ApplyOpacityImage 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 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.Color, 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 math32.Vector2, r float32) (pt math32.Vector2, 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 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)

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 given opacity

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.boxTransform].

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) GetColorImpl added in v0.0.5

func (b *Base) GetColorImpl(pos float32, stops []Stop) color.Color

GetColorImpl implements GetColor with given stops

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

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

func (*Base) UpdateRGBStops added in v0.0.5

func (b *Base) UpdateRGBStops()

UpdateRGBStops updates StopsRGB from original Stops, for other blend types

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

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

SetBlend sets the [Linear.Blend]

func (*Linear) SetBox

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

SetBox sets the [Linear.Box]

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) SetOpacity added in v0.0.5

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

SetOpacity sets the [Linear.Opacity]

func (*Linear) SetSpread

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

SetSpread sets the [Linear.Spread]

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

func (t *Linear) SetTransform(v math32.Matrix2) *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(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) SetBlend

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

SetBlend sets the [Radial.Blend]

func (*Radial) SetBox

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

SetBox sets the [Radial.Box]

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) SetOpacity added in v0.0.5

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

SetOpacity sets the [Radial.Opacity]

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