colors

package module
v0.0.0-...-b138811 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 20 Imported by: 0

README

colors

Go Reference Go Version

A Go library for color space conversions, interpolation, and gradients.

Installation

go get github.com/thmalt/colors

Usage

import "github.com/thmalt/colors"
Example
package main

import (
	"fmt"

	"github.com/thmalt/colors"
	"github.com/thmalt/colors/space"
)

func main() {
	c := colors.Rgba(255, 0, 0, .5)

	fmt.Println(c.Hex())
	fmt.Println(c.To(space.Oklab))
	fmt.Println(c.Oklab())
}

Color Conversion

The github.com/thmalt/colors/convert package provides direct conversion functions between color spaces.

Direct conversion functions are generated for the supported color-space pairs. The convert package provides low-overhead conversion APIs, but it is not necessarily the fastest possible implementation.

l, a, b := convert.SrgbToOklab(r, g, b)

The root github.com/thmalt/colors package builds on github.com/thmalt/colors/convert and provides two conversion modes.

colors build modes
Default build

The default build uses a hub-based conversion strategy to reduce binary size:

go build

Cross-family conversions may be routed through the XyzD50 or XyzD65 conversion hubs, while conversions within the same color-space family remain direct.

Full conversion build

For applications where conversion performance is more important than binary size, the full direct conversion graph can be enabled with the colors_full build tag:

go build -tags=colors_full

This generates direct conversion paths between color spaces instead of routing cross-family conversions through the XYZ hubs.

Package / Build tag Conversion strategy Binary size Performance
colors/convert Direct conversions On demand Higher
colors (-tags=colors_full) Direct paths Larger Medium
colors (default) Hub-based paths Smaller Lower
Binary size

Conversion code is included in the final binary based on how the conversion API is used.

Specific methods such as Color.Srgb(), Color.Oklab(), and Color.XyzD50() only reference conversions required for their respective color spaces. Unused conversions can be removed by the Go linker.

Generic methods such as Color.To(dst space.Space) and Color.MustTo(dst space.Space) reference the complete generated conversion dispatch for the selected build mode.

For smaller production binaries, use -s and -w to strip symbol and DWARF information. -trimpath can also be used to remove local filesystem paths:

go build -ldflags="-s -w" -trimpath

Examples

For complete, runnable examples, see the examples directory.

Color and Color Spaces
package main

import (
	"fmt"

	"github.com/thmalt/colors"
	"github.com/thmalt/colors/space"
)

func main() {
	c := colors.Hex("#323c46fe")

	// Convert a color to different color spaces.
	fmt.Println("sRGB: ", c)
	fmt.Println("Oklab:", c.To(space.Oklab))
	fmt.Println("Oklch:", c.To(space.Oklch))
	fmt.Println("Lab:  ", c.To(space.Lab))
	fmt.Println("LCh:  ", c.To(space.Lch))

	fmt.Println()

	// Serialize the color as hexadecimal.
	fmt.Println("Hex:           ", c.WithAlpha(1).Hex())
	fmt.Println("Hex with alpha:", c.Hex())

	fmt.Println()

	// Inspect color space metadata.
	info := c.Space().Info()
	if info == nil {
		fmt.Println("invalid color space:", c.Space())
		return
	}

	fmt.Println("Color space:", info.DisplayName())

	whitePoint := info.WhitePoint()
	fmt.Println("White point:", whitePoint.Name)
	fmt.Println("  X:", whitePoint.X)
	fmt.Println("  Y:", whitePoint.Y)
	fmt.Println("  Z:", whitePoint.Z)

	fmt.Println("Channels:", info.ChannelCount())
	for i, ch := range info.Channels() {
		fmt.Printf(
			"  %d: %s [%g, %g], unrestricted=%t\n",
			i, ch.Name, ch.Min, ch.Max, ch.Unrestricted,
		)
	}
}
Gradient Examples
blue   = colors.Oklab(0.42, -0.02, -0.15)
purple = colors.Oklab(0.52, 0.08, -0.14)
orange = colors.Oklab(0.72, 0.12, 0.12)

// CSS:
// linear-gradient(
//   to bottom right in oklab,
//   #1b469e,
//   #504baa,
//   #784db6,
//   #b56c8a,
//   #f87c3e
// )
linearGradient = colors.NewGradientBuilder().
	AddStop(blue).
	AddStop(blue.Mix(purple, 0.5), 0.25).
	AddStop(purple, 0.5).
	AddStop(purple.Mix(orange, 0.5), 0.75).
	AddStop(orange, 1).
	Build()

linearSpec := gradient.NewLinearSpec()
linearSpec.SetSize(width, height)
linearSpec.SetDirection(gradient.ToBottomRight)

linear, err := linearSpec.Build()

Linear

// CSS:
// conic-gradient(
//   from 0.25turn at 50% 30% in oklab,
//   #f69d3c,
//   10deg,
//   #3f87a6,
//   350deg,
//   #ebf8e1
// )
conicGradient = colors.NewGradientBuilder().
	AddStop(colors.Hex("#f69d3c")).
	AddHint(gradient.DegToTurn(10)).
	AddStop(colors.Hex("#3f87a6")).
	AddHint(gradient.DegToTurn(350)).
	AddStop(colors.Hex("#ebf8e1")).
	Build()

conicSpec := gradient.NewConicSpec()
conicSpec.SetSize(width, height)
conicSpec.SetCenter(0.5, 0.3)
conicSpec.SetStartAngle(gradient.ToRight)

conic, err := conicSpec.Build()

Conic

// CSS:
// radial-gradient(
//   farthest-corner in oklab,
//   #3f5efb,
//   #00ff88,
//   #ce436d,
//   #0413bf 50%,
//   #fc466b
// )
radialGradient = colors.NewGradientBuilder().
	AddStop(colors.Hex("#3f5efb")).
	AddStop(colors.Hex("#00ff88")).
	AddStop(colors.Hex("#ce436d")).
	AddStop(colors.Hex("#0413bf"), 0.5).
	AddStop(colors.Hex("#fc466b")).
	Build()

radialSpec := gradient.NewRadialSpec()
radialSpec.SetSize(width, height)

radialSpec.SetShape(gradient.RadialCircle)
circle, err := radialSpec.Build()

radialSpec.SetShape(gradient.RadialEllipse)
ellipse, err := radialSpec.Build()

Radial circle Radial ellipse

More examples are available in the examples directory.

Development

AI tools are used for research, problem solving, including advanced topics in color science and rendering.

Documentation

Index

Examples

Constants

View Source
const (
	HueShorter    = interp.HueShorter
	HueLonger     = interp.HueLonger
	HueIncreasing = interp.HueIncreasing
	HueDecreasing = interp.HueDecreasing
)

Variables

View Source
var (
	ErrInvalidSpace = errors.New("invalid space")
	ErrUnknownSpace = errors.New("unknown space")

	ErrInvalidConversion = errors.New("colors: invalid color conversion")
)

Functions

func Contrast

func Contrast(c1, c2 Color) float64

Contrast returns the WCAG 2 contrast ratio between two colors.

func DeltaE76

func DeltaE76(c1, c2 Color) float64

DeltaE76 returns the CIE76 color difference between two [Color]s in space.LabD50.

func DeltaE76LabD65

func DeltaE76LabD65(c1, c2 Color) float64

DeltaE76LabD65 returns the CIE76 color difference between two [Color]s in space.LabD65.

func DeltaE94

func DeltaE94(c1, c2 Color) float64

DeltaE94 returns the CIE94 color difference between two [Color]s in space.LabD50.

func DeltaE94LabD65

func DeltaE94LabD65(c1, c2 Color) float64

DeltaE94LabD65 returns the CIE94 color difference between two [Color]s in space.LabD65.

func DeltaE2000

func DeltaE2000(c1, c2 Color) float64

DeltaE2000 returns the CIEDE2000 color difference between two [Color]s in space.LabD50.

func DeltaE2000LabD65

func DeltaE2000LabD65(c1, c2 Color) float64

DeltaE2000LabD65 returns the CIEDE2000 color difference between two [Color]s in space.LabD65.

func DeltaEOK

func DeltaEOK(c1, c2 Color) float64

DeltaEOK returns the Euclidean distance between two [Color]s in the space.Oklab color space.

func DeltaEOK2

func DeltaEOK2(c1, c2 Color) float64

DeltaEOK2 returns the Euclidean distance between two [Color]s in the space.Oklab color space, with the a and b components scaled by 2.

func InGamut

func InGamut(c Color) bool

InGamut reports whether c is within the gamut of its color space.

func InGamutSpace

func InGamutSpace(c Color, dst space.Space) bool

InGamutSpace reports whether c is within the gamut of the specified color space.

func Luminance

func Luminance(c Color) float64

Luminance returns the relative luminance of the color.

Types

type Adjuster

type Adjuster struct {
	// contains filtered or unexported fields
}

Adjuster provides chained adjustments for a color in space.Oklch.

func NewAdjuster

func NewAdjuster(c Color) *Adjuster

NewAdjuster returns an Adjuster initialized with the specified color. The color is converted to space.Oklch for adjustment.

func (*Adjuster) AdjustHue

func (a *Adjuster) AdjustHue(amount float64) *Adjuster

AdjustHue adjusts the hue in space.Oklch by amount.

func (*Adjuster) Chroma

func (a *Adjuster) Chroma() float64

Chroma returns the current chroma in space.Oklch.

func (*Adjuster) Color

func (a *Adjuster) Color() Color

Color returns the current color in space.Oklch.

func (*Adjuster) Darken

func (a *Adjuster) Darken(amount float64) *Adjuster

Darken decreases the lightness in space.Oklch by amount.

func (*Adjuster) Desaturate

func (a *Adjuster) Desaturate(amount float64) *Adjuster

Desaturate decreases the chroma in space.Oklch by amount.

func (*Adjuster) Grayscale

func (a *Adjuster) Grayscale() *Adjuster

Grayscale sets the chroma in space.Oklch to 0.

func (*Adjuster) Hue

func (a *Adjuster) Hue() float64

Hue returns the current hue in space.Oklch in degrees.

func (*Adjuster) Lighten

func (a *Adjuster) Lighten(amount float64) *Adjuster

Lighten increases the lightness in space.Oklch by amount.

func (*Adjuster) Lightness

func (a *Adjuster) Lightness() float64

Lightness returns the current lightness in space.Oklch.

func (*Adjuster) Result

func (a *Adjuster) Result() Color

Result returns the adjusted color in its original color space.

func (*Adjuster) Saturate

func (a *Adjuster) Saturate(amount float64) *Adjuster

Saturate increases the chroma in space.Oklch by amount.

func (*Adjuster) SetChroma

func (a *Adjuster) SetChroma(v float64) *Adjuster

SetChroma sets the chroma in space.Oklch to v. Negative values are clamped to 0.

func (*Adjuster) SetHue

func (a *Adjuster) SetHue(v float64) *Adjuster

SetHue sets the hue in space.Oklch to v degrees. The value is normalized to [0, 360).

func (*Adjuster) SetLightness

func (a *Adjuster) SetLightness(v float64) *Adjuster

SetLightness sets the lightness in space.Oklch to v. The value is clamped to [0, 1].

type Color

type Color struct {
	// contains filtered or unexported fields
}

Color represents a color in a space.Space.

func A98

func A98(r, g, b float64) Color

A98 returns a Color from Adobe RGB (1998) components.

r: [0, 1]
g: [0, 1]
b: [0, 1]

func A98Alpha

func A98Alpha(r, g, b, alpha float64) Color

A98Alpha returns a Color from Adobe RGB (1998) components with alpha.

r: [0, 1]
g: [0, 1]
b: [0, 1]
alpha: [0, 1]

func Clamp

func Clamp(c Color) Color

Clamp clamps the color channels to their valid ranges and alpha to [0, 1].

func DisplayP3

func DisplayP3(r, g, b float64) Color

DisplayP3 returns a Color from Display P3 components.

r: [0, 1]
g: [0, 1]
b: [0, 1]

func DisplayP3Alpha

func DisplayP3Alpha(r, g, b, alpha float64) Color

DisplayP3Alpha returns a Color from Display P3 components with alpha.

r: [0, 1]
g: [0, 1]
b: [0, 1]
alpha: [0, 1]

func FromStd

func FromStd[T color.Color](c T) Color

FromStd converts a color.Color to a Color in space.Srgb. A nil input returns an invalid Color.

func FromStdToLinear

func FromStdToLinear[T color.Color](c T) Color

FromStdToLinear converts a color.Color to a Color in space.LinearSrgb. A nil input returns an invalid Color.

func Hex

func Hex(s string) Color

Hex returns an sRGB color from a hexadecimal color string.

func Hsl

func Hsl(h, s, l float64) Color

Hsl returns a Color from HSL components.

h: [0, 360)
s: [0, 1]
l: [0, 1]

func HslAlpha

func HslAlpha(h, s, l, alpha float64) Color

HslAlpha returns a Color from HSL components with alpha.

h: [0, 360)
s: [0, 1]
l: [0, 1]
alpha: [0, 1]

func Hsv

func Hsv(h, s, v float64) Color

Hsv returns a Color from HSV components.

h: [0, 360)
s: [0, 1]
v: [0, 1]

func HsvAlpha

func HsvAlpha(h, s, v, alpha float64) Color

HsvAlpha returns a Color from HSV components with alpha.

h: [0, 360)
s: [0, 1]
v: [0, 1]
alpha: [0, 1]

func Hwb

func Hwb(h, w, b float64) Color

Hwb returns a Color from HWB components.

h: [0, 360)
w: [0, 1]
b: [0, 1]

func HwbAlpha

func HwbAlpha(h, w, b, alpha float64) Color

HwbAlpha returns a Color from HWB components with alpha.

h: [0, 360)
w: [0, 1]
b: [0, 1]
alpha: [0, 1]

func LabD50

func LabD50(l, a, b float64) Color

LabD50 returns a Color from CIE Lab D50 components.

l: [0, 100]
a: [-125, 125] (typical)
b: [-125, 125] (typical)

func LabD50Alpha

func LabD50Alpha(l, a, b, alpha float64) Color

LabD50Alpha returns a Color from CIE Lab D50 components with alpha.

l: [0, 100]
a: [-125, 125] (typical)
b: [-125, 125] (typical)
alpha: [0, 1]

func LabD65

func LabD65(l, a, b float64) Color

LabD65 returns a Color from CIE Lab D65 components.

l: [0, 100]
a: [-125, 125] (typical)
b: [-125, 125] (typical)

func LabD65Alpha

func LabD65Alpha(l, a, b, alpha float64) Color

LabD65Alpha returns a Color from CIE Lab D65 components with alpha.

l: [0, 100]
a: [-125, 125] (typical)
b: [-125, 125] (typical)
alpha: [0, 1]

func LchD50

func LchD50(l, c, h float64) Color

LchD50 returns a Color from CIE LCh D50 components.

l: [0, 100]
c: [0, 150] (typical)
h: [0, 360)

func LchD50Alpha

func LchD50Alpha(l, c, h, alpha float64) Color

LchD50Alpha returns a Color from CIE LCh D50 components with alpha.

l: [0, 100]
c: [0, 150] (typical)
h: [0, 360)
alpha: [0, 1]

func LchD65

func LchD65(l, c, h float64) Color

LchD65 returns a Color from CIE LCh D65 components.

l: [0, 100]
c: [0, 150] (typical)
h: [0, 360)

func LchD65Alpha

func LchD65Alpha(l, c, h, alpha float64) Color

LchD65Alpha returns a Color from CIE LCh D65 components with alpha.

l: [0, 100]
c: [0, 150] (typical)
h: [0, 360)
alpha: [0, 1]

func LchuvD50

func LchuvD50(l, c, h float64) Color

LchuvD50 returns a Color from CIE LChuv D50 components.

l: [0, 100]
c: [0, 180] (typical)
h: [0, 360)

func LchuvD50Alpha

func LchuvD50Alpha(l, c, h, alpha float64) Color

LchuvD50Alpha returns a Color from CIE LChuv D50 components with alpha.

l: [0, 100]
c: [0, 180] (typical)
h: [0, 360)
alpha: [0, 1]

func LchuvD65

func LchuvD65(l, c, h float64) Color

LchuvD65 returns a Color from CIE LChuv D65 components.

l: [0, 100]
c: [0, 180] (typical)
h: [0, 360)

func LchuvD65Alpha

func LchuvD65Alpha(l, c, h, alpha float64) Color

LchuvD65Alpha returns a Color from CIE LChuv D65 components with alpha.

l: [0, 100]
c: [0, 180] (typical)
h: [0, 360)
alpha: [0, 1]

func LinearA98

func LinearA98(r, g, b float64) Color

LinearA98 returns a Color from Linear Adobe RGB (1998) components.

r: [0, 1]
g: [0, 1]
b: [0, 1]

func LinearA98Alpha

func LinearA98Alpha(r, g, b, alpha float64) Color

LinearA98Alpha returns a Color from Linear Adobe RGB (1998) components with alpha.

r: [0, 1]
g: [0, 1]
b: [0, 1]
alpha: [0, 1]

func LinearDisplayP3

func LinearDisplayP3(r, g, b float64) Color

LinearDisplayP3 returns a Color from Linear Display P3 components.

r: [0, 1]
g: [0, 1]
b: [0, 1]

func LinearDisplayP3Alpha

func LinearDisplayP3Alpha(r, g, b, alpha float64) Color

LinearDisplayP3Alpha returns a Color from Linear Display P3 components with alpha.

r: [0, 1]
g: [0, 1]
b: [0, 1]
alpha: [0, 1]

func LinearProPhoto

func LinearProPhoto(r, g, b float64) Color

LinearProPhoto returns a Color from Linear ProPhoto components.

r: [0, 1]
g: [0, 1]
b: [0, 1]

func LinearProPhotoAlpha

func LinearProPhotoAlpha(r, g, b, alpha float64) Color

LinearProPhotoAlpha returns a Color from Linear ProPhoto components with alpha.

r: [0, 1]
g: [0, 1]
b: [0, 1]
alpha: [0, 1]

func LinearRec2020

func LinearRec2020(r, g, b float64) Color

LinearRec2020 returns a Color from Linear Rec. 2020 components.

r: [0, 1]
g: [0, 1]
b: [0, 1]

func LinearRec2020Alpha

func LinearRec2020Alpha(r, g, b, alpha float64) Color

LinearRec2020Alpha returns a Color from Linear Rec. 2020 components with alpha.

r: [0, 1]
g: [0, 1]
b: [0, 1]
alpha: [0, 1]

func LinearRec2100

func LinearRec2100(r, g, b float64) Color

LinearRec2100 returns a Color from Linear Rec. 2100 components.

r: [0, 1]
g: [0, 1]
b: [0, 1]

func LinearRec2100Alpha

func LinearRec2100Alpha(r, g, b, alpha float64) Color

LinearRec2100Alpha returns a Color from Linear Rec. 2100 components with alpha.

r: [0, 1]
g: [0, 1]
b: [0, 1]
alpha: [0, 1]

func LinearSrgb

func LinearSrgb(r, g, b float64) Color

LinearSrgb returns a Color from Linear sRGB components.

r: [0, 1]
g: [0, 1]
b: [0, 1]

func LinearSrgbAlpha

func LinearSrgbAlpha(r, g, b, alpha float64) Color

LinearSrgbAlpha returns a Color from Linear sRGB components with alpha.

r: [0, 1]
g: [0, 1]
b: [0, 1]
alpha: [0, 1]

func LuvD50

func LuvD50(l, u, v float64) Color

LuvD50 returns a Color from CIE Luv D50 components.

l: [0, 100]
u: [-134, 220] (typical)
v: [-140, 122] (typical)

func LuvD50Alpha

func LuvD50Alpha(l, u, v, alpha float64) Color

LuvD50Alpha returns a Color from CIE Luv D50 components with alpha.

l: [0, 100]
u: [-134, 220] (typical)
v: [-140, 122] (typical)
alpha: [0, 1]

func LuvD65

func LuvD65(l, u, v float64) Color

LuvD65 returns a Color from CIE Luv D65 components.

l: [0, 100]
u: [-134, 220] (typical)
v: [-140, 122] (typical)

func LuvD65Alpha

func LuvD65Alpha(l, u, v, alpha float64) Color

LuvD65Alpha returns a Color from CIE Luv D65 components with alpha.

l: [0, 100]
u: [-134, 220] (typical)
v: [-140, 122] (typical)
alpha: [0, 1]

func MapToGamut

func MapToGamut(c Color, dst space.Space) Color

MapToGamut maps the color to the gamut of the specified color space. It returns the original color if mapping fails.

func Mix

func Mix(c1, c2 Color, t float64) Color

Mix interpolates between two colors using the default mix options. See NewMixer for the default mix options.

func MixWith

func MixWith(c1, c2 Color, t float64, opts InterpOptions) Color

MixWith interpolates between two colors using the specified interpolation options. The interpolation is performed in opts.Space. An invalid space defaults to space.Oklab.

func New

func New(space space.Space, channels ...float64) Color

New creates a Color from a color space and its channel values. The number of values must match the color space's channel count, optionally followed by an alpha value. Alpha defaults to 1.

It returns an invalid Color if the color space or number of values is invalid.

func Oklab

func Oklab(l, a, b float64) Color

Oklab returns a Color from Oklab components.

l: [0, 1]
a: [-0.4, 0.4] (typical)
b: [-0.4, 0.4] (typical)

func OklabAlpha

func OklabAlpha(l, a, b, alpha float64) Color

OklabAlpha returns a Color from Oklab components with alpha.

l: [0, 1]
a: [-0.4, 0.4] (typical)
b: [-0.4, 0.4] (typical)
alpha: [0, 1]

func Oklch

func Oklch(l, c, h float64) Color

Oklch returns a Color from Oklch components.

l: [0, 1]
c: [0, 0.4] (typical)
h: [0, 360)

func OklchAlpha

func OklchAlpha(l, c, h, alpha float64) Color

OklchAlpha returns a Color from Oklch components with alpha.

l: [0, 1]
c: [0, 0.4] (typical)
h: [0, 360)
alpha: [0, 1]

func Parse

func Parse(s string) (Color, error)

Parse parses a CSS color value from s.

func ParseBytes

func ParseBytes(b []byte) (Color, error)

ParseBytes parses a CSS color value from b.

func ProPhoto

func ProPhoto(r, g, b float64) Color

ProPhoto returns a Color from ProPhoto components.

r: [0, 1]
g: [0, 1]
b: [0, 1]

func ProPhotoAlpha

func ProPhotoAlpha(r, g, b, alpha float64) Color

ProPhotoAlpha returns a Color from ProPhoto components with alpha.

r: [0, 1]
g: [0, 1]
b: [0, 1]
alpha: [0, 1]

func Rec2020

func Rec2020(r, g, b float64) Color

Rec2020 returns a Color from Rec. 2020 components.

r: [0, 1]
g: [0, 1]
b: [0, 1]

func Rec2020Alpha

func Rec2020Alpha(r, g, b, alpha float64) Color

Rec2020Alpha returns a Color from Rec. 2020 components with alpha.

r: [0, 1]
g: [0, 1]
b: [0, 1]
alpha: [0, 1]

func Rec2020OETF

func Rec2020OETF(r, g, b float64) Color

Rec2020OETF returns a Color from Rec. 2020 Scene Referred components.

r: [0, 1]
g: [0, 1]
b: [0, 1]

func Rec2020OETFAlpha

func Rec2020OETFAlpha(r, g, b, alpha float64) Color

Rec2020OETFAlpha returns a Color from Rec. 2020 Scene Referred components with alpha.

r: [0, 1]
g: [0, 1]
b: [0, 1]
alpha: [0, 1]

func Rec2100HLG

func Rec2100HLG(r, g, b float64) Color

Rec2100HLG returns a Color from Rec. 2100 HLG components.

r: [0, 1]
g: [0, 1]
b: [0, 1]

func Rec2100HLGAlpha

func Rec2100HLGAlpha(r, g, b, alpha float64) Color

Rec2100HLGAlpha returns a Color from Rec. 2100 HLG components with alpha.

r: [0, 1]
g: [0, 1]
b: [0, 1]
alpha: [0, 1]

func Rec2100PQ

func Rec2100PQ(r, g, b float64) Color

Rec2100PQ returns a Color from Rec. 2100 PQ components.

r: [0, 1]
g: [0, 1]
b: [0, 1]

func Rec2100PQAlpha

func Rec2100PQAlpha(r, g, b, alpha float64) Color

Rec2100PQAlpha returns a Color from Rec. 2100 PQ components with alpha.

r: [0, 1]
g: [0, 1]
b: [0, 1]
alpha: [0, 1]

func Rgb

func Rgb(r, g, b float64) Color

Rgb returns an sRGB Color from RGB components.

r, g, b: [0, 255]

func Rgba

func Rgba(r, g, b, a float64) Color

Rgb returns an sRGB Color from RGB components and alpha.

r, g, b: [0, 255]
a:       [0, 1]

func Srgb

func Srgb(r, g, b float64) Color

Srgb returns a Color from sRGB components.

r: [0, 1]
g: [0, 1]
b: [0, 1]

func Srgb8

func Srgb8(r, g, b uint8) Color

Srgb8 returns a Color from 8-bit sRGB components.

func Srgb8ToLinear

func Srgb8ToLinear(r, g, b uint8) Color

Srgb8ToLinear returns a Color in linear sRGB from 8-bit sRGB components.

It provides fast sRGB-to-linear conversion.

func Srgb16

func Srgb16(r, g, b uint16) Color

Srgb16 returns a Color from 16-bit sRGB components.

func Srgb16ToLinear

func Srgb16ToLinear(r, g, b uint16) Color

Srgb16ToLinear returns a Color in linear sRGB from 16-bit sRGB components.

It provides fast sRGB-to-linear conversion.

func SrgbAlpha

func SrgbAlpha(r, g, b, alpha float64) Color

SrgbAlpha returns a Color from sRGB components with alpha.

r: [0, 1]
g: [0, 1]
b: [0, 1]
alpha: [0, 1]

func Srgba8

func Srgba8(r, g, b, a uint8) Color

Srgb8 returns a Color from 8-bit sRGB components and alpha.

func Srgba8ToLinear

func Srgba8ToLinear(r, g, b, a uint8) Color

Srgba8ToLinear returns a Color in linear sRGB from 8-bit sRGB components and alpha.

It provides fast sRGB-to-linear conversion.

func Srgba16

func Srgba16(r, g, b, a uint16) Color

Srgba16 returns a Color from 16-bit sRGB components and alpha.

func Srgba16ToLinear

func Srgba16ToLinear(r, g, b, a uint16) Color

Srgba16ToLinear returns a Color in linear sRGB from 16-bit sRGB components and alpha.

It provides fast sRGB-to-linear conversion.

func StdAlpha

func StdAlpha(c color.Alpha) Color

StdAlpha returns a sRGB Color from a color.Alpha.

func StdAlpha16

func StdAlpha16(c color.Alpha16) Color

StdAlpha16 returns a sRGB Color from a color.Alpha16.

func StdAlpha16ToLinear

func StdAlpha16ToLinear(c color.Alpha16) Color

StdAlpha16ToLinear returns a linear sRGB Color from a color.Alpha16.

func StdAlphaToLinear

func StdAlphaToLinear(c color.Alpha) Color

StdAlphaToLinear returns a linear sRGB Color from a color.Alpha.

func StdCMYK

func StdCMYK(c color.CMYK) Color

StdCMYK returns a sRGB Color from a color.CMYK.

func StdCMYKToLinear

func StdCMYKToLinear(c color.CMYK) Color

StdCMYKToLinear returns a linear sRGB Color from a color.CMYK.

func StdGray

func StdGray(c color.Gray) Color

StdGray returns a sRGB Color from a color.Gray.

func StdGray16

func StdGray16(c color.Gray16) Color

StdGray16 returns a sRGB Color from a color.Gray16.

func StdGray16ToLinear

func StdGray16ToLinear(c color.Gray16) Color

StdGray16ToLinear returns a linear sRGB Color from a color.Gray16.

func StdGrayToLinear

func StdGrayToLinear(c color.Gray) Color

StdGrayToLinear returns a linear sRGB Color from a color.Gray.

func StdNRGBA

func StdNRGBA(c color.NRGBA) Color

StdNRGBA returns a sRGB Color from a color.NRGBA.

func StdNRGBA64

func StdNRGBA64(c color.NRGBA64) Color

StdNRGBA64 returns a sRGB Color from a color.NRGBA64.

func StdNRGBA64ToLinear

func StdNRGBA64ToLinear(c color.NRGBA64) Color

StdNRGBA64ToLinear returns a linear sRGB Color from a color.NRGBA64.

func StdNRGBAToLinear

func StdNRGBAToLinear(c color.NRGBA) Color

StdNRGBAToLinear returns a linear sRGB Color from a color.NRGBA.

func StdNYCbCrA

func StdNYCbCrA(c color.NYCbCrA) Color

StdNYCbCrA returns a sRGB Color from a color.NYCbCrA.

func StdNYCbCrAToLinear

func StdNYCbCrAToLinear(c color.NYCbCrA) Color

StdNYCbCrAToLinear returns a linear sRGB Color from a color.NYCbCrA.

func StdRGBA

func StdRGBA(c color.RGBA) Color

StdRGBA returns a sRGB Color from a color.RGBA.

func StdRGBA64

func StdRGBA64(c color.RGBA64) Color

StdRGBA64 returns a sRGB Color from a color.RGBA64.

func StdRGBA64ToLinear

func StdRGBA64ToLinear(c color.RGBA64) Color

StdRGBA64ToLinear returns a linear sRGB Color from a color.RGBA64.

func StdRGBAToLinear

func StdRGBAToLinear(c color.RGBA) Color

StdRGBAToLinear returns a linear sRGB Color from a color.RGBA.

func StdYCbCr

func StdYCbCr(c color.YCbCr) Color

StdYCbCr returns a sRGB Color from a color.YCbCr.

func StdYCbCrToLinear

func StdYCbCrToLinear(c color.YCbCr) Color

StdYCbCrToLinear returns a linear sRGB Color from a color.YCbCr.

func TryHex

func TryHex(s string) (Color, bool)

TryHex returns an sRGB color from a hexadecimal color string and reports whether the string was successfully parsed.

func TryMapToGamut

func TryMapToGamut(c Color, dst space.Space) (Color, bool)

TryMapToGamut maps the color to the gamut of the specified color space and reports whether the mapping succeeded.

func TryNew

func TryNew(space space.Space, channels ...float64) (Color, bool)

TryNew creates a Color from a color space and its channel values. The number of values must match the color space's channel count, optionally followed by an alpha value. Alpha defaults to 1.

It returns an invalid Color and [false] if the color space or number of values is invalid.

func XyYD50

func XyYD50(x, y, luminance float64) Color

XyYD50 returns a Color from CIE xyY components.

x: [0, 1]
y: [0, 1]
luminance: [0, 1] (typical)

func XyYD50Alpha

func XyYD50Alpha(x, y, luminance, alpha float64) Color

XyYD50Alpha returns a Color from CIE xyY components with alpha.

x: [0, 1]
y: [0, 1]
luminance: [0, 1] (typical)
alpha: [0, 1]

func XyYD65

func XyYD65(x, y, luminance float64) Color

XyYD65 returns a Color from CIE xyY components.

x: [0, 1]
y: [0, 1]
luminance: [0, 1] (typical)

func XyYD65Alpha

func XyYD65Alpha(x, y, luminance, alpha float64) Color

XyYD65Alpha returns a Color from CIE xyY components with alpha.

x: [0, 1]
y: [0, 1]
luminance: [0, 1] (typical)
alpha: [0, 1]

func XyzAbsD65

func XyzAbsD65(x, y, z float64) Color

XyzAbsD65 returns a Color from Absolute XYZ D65 components.

x: [0, 193] (typical)
y: [0, 203] (typical)
z: [0, 221] (typical)

func XyzAbsD65Alpha

func XyzAbsD65Alpha(x, y, z, alpha float64) Color

XyzAbsD65Alpha returns a Color from Absolute XYZ D65 components with alpha.

x: [0, 193] (typical)
y: [0, 203] (typical)
z: [0, 221] (typical)
alpha: [0, 1]

func XyzD50

func XyzD50(x, y, z float64) Color

XyzD50 returns a Color from CIE XYZ D50 components.

x: [0, 0.9642956764295677] (typical)
y: [0, 1] (typical)
z: [0, 0.8251046025104602] (typical)

func XyzD50Alpha

func XyzD50Alpha(x, y, z, alpha float64) Color

XyzD50Alpha returns a Color from CIE XYZ D50 components with alpha.

x: [0, 0.9642956764295677] (typical)
y: [0, 1] (typical)
z: [0, 0.8251046025104602] (typical)
alpha: [0, 1]

func XyzD65

func XyzD65(x, y, z float64) Color

XyzD65 returns a Color from CIE XYZ D65 components.

x: [0, 0.9504559270516716] (typical)
y: [0, 1] (typical)
z: [0, 1.0890577507598784] (typical)

func XyzD65Alpha

func XyzD65Alpha(x, y, z, alpha float64) Color

XyzD65Alpha returns a Color from CIE XYZ D65 components with alpha.

x: [0, 0.9504559270516716] (typical)
y: [0, 1] (typical)
z: [0, 1.0890577507598784] (typical)
alpha: [0, 1]

func (Color) A98

func (c Color) A98() (r, g, b float64)

A98 returns the color components in the space.A98 color space.

func (Color) AdjustHue

func (c Color) AdjustHue(amount float64) Color

AdjustHue returns a copy of the color with its hue in space.Oklch adjusted by amount.

The resulting hue is normalized to [0, 360).

func (Color) Adjuster

func (c Color) Adjuster() *Adjuster

Adjuster returns an Adjuster for the color.

func (Color) Alpha

func (c Color) Alpha() float64

Alpha returns the alpha value.

func (Color) Alpha8

func (c Color) Alpha8() uint8

Alpha8 returns the alpha value as an 8-bit value in the range [0, 255].

func (Color) AppendChannels

func (c Color) AppendChannels(dst []float64) []float64

AppendChannels appends the color channels to dst and returns the resulting slice.

func (Color) Channel

func (c Color) Channel(index int) (float64, bool)

Channel returns the channel value at index in the order defined by the color's space.Space. The returned boolean reports whether index is valid.

func (Color) Channel3

func (c Color) Channel3() (c1, c2, c3 float64)

Channel3 returns the first 3 channels.

func (Color) Channel4

func (c Color) Channel4() (c1, c2, c3, c4 float64)

Channel4 returns the first 4 channels.

func (Color) ChannelCount

func (c Color) ChannelCount() int

ChannelCount returns the number of color channels.

func (Color) Channels

func (c Color) Channels() []float64

Channels returns the channel values of c in the order defined by its space.Space.

func (Color) Clamp

func (c Color) Clamp() Color

Clamp clamps the color channels to the valid range of the color space.

func (Color) Contrast

func (c Color) Contrast(other Color) float64

Contrast returns the WCAG 2 contrast ratio between the color and another color.

func (Color) CoordinateSystem

func (c Color) CoordinateSystem() space.CoordinateSystem

CoordinateSystem returns the coordinate system of the color space.

func (Color) Darken

func (c Color) Darken(amount float64) Color

Darken returns a copy of the color with its lightness in space.Oklch decreased by amount.

The resulting lightness is clamped to [0, 1].

func (Color) DeltaE76

func (c Color) DeltaE76(other Color) float64

DeltaE76 returns the CIE76 color difference between two [Color]s in space.LabD50.

func (Color) DeltaE76LabD65

func (c Color) DeltaE76LabD65(other Color) float64

DeltaE76LabD65 returns the CIE76 color difference between two [Color]s in space.LabD65.

func (Color) DeltaE94

func (c Color) DeltaE94(other Color) float64

DeltaE94 returns the CIE94 color difference between two [Color]s in space.LabD50.

func (Color) DeltaE94LabD65

func (c Color) DeltaE94LabD65(other Color) float64

DeltaE94LabD65 returns the CIE94 color difference between two [Color]s in space.LabD65.

func (Color) DeltaE2000

func (c Color) DeltaE2000(other Color) float64

DeltaE2000 returns the CIEDE2000 color difference between two [Color]s in space.LabD50.

func (Color) DeltaE2000LabD65

func (c Color) DeltaE2000LabD65(other Color) float64

DeltaE2000LabD65 returns the CIEDE2000 color difference between two [Color]s in space.LabD65.

func (Color) DeltaEOK

func (c Color) DeltaEOK(other Color) float64

DeltaEOK returns the Euclidean distance between two [Color]s in the space.Oklab color space.

func (Color) DeltaEOK2

func (c Color) DeltaEOK2(other Color) float64

DeltaEOK2 returns the Euclidean distance between two [Color]s in the space.Oklab color space, with the a and b components scaled by 2.

func (Color) Desaturate

func (c Color) Desaturate(amount float64) Color

Desaturate returns a copy of the color with its chroma in space.Oklch decreased by amount.

The resulting chroma is clamped to [0, +Inf).

func (Color) DisplayP3

func (c Color) DisplayP3() (r, g, b float64)

DisplayP3 returns the color components in the space.DisplayP3 color space.

func (Color) Dither

func (c Color) Dither(x, y int) Color

Dither applies ordered dithering to the sRGB color channels at pixel position (x, y). The dithering offset is scaled to the normalized sRGB [0, 1] range; the alpha channel is left unchanged. The returned color is always in space.Srgb.

func (Color) Grayscale

func (c Color) Grayscale() Color

Grayscale returns a copy of the color with its chroma in space.Oklch set to 0.

func (Color) Hex

func (c Color) Hex() string

Hex returns the hexadecimal representation of the color.

func (Color) Hsl

func (c Color) Hsl() (h, s, l float64)

Hsl returns the color components in the space.Hsl color space.

func (Color) Hsv

func (c Color) Hsv() (h, s, v float64)

Hsv returns the color components in the space.Hsv color space.

func (Color) Hwb

func (c Color) Hwb() (h, w, b float64)

Hwb returns the color components in the space.Hwb color space.

func (Color) InGamut

func (c Color) InGamut() bool

InGamut reports whether the color is within the gamut of its color space.

func (Color) InGamutSpace

func (c Color) InGamutSpace(dst space.Space) bool

InGamutSpace reports whether the color is within the gamut of the specified color space.

func (Color) IsOpaque

func (c Color) IsOpaque() bool

IsOpaque reports whether the color has an alpha value of at least 1.

func (Color) IsValid

func (c Color) IsValid() bool

IsValid reports whether the color's color space is valid.

func (Color) Lab

func (c Color) Lab() (l, a, b float64)

Lab is an alias for Color.LabD50.

func (Color) LabD50

func (c Color) LabD50() (l, a, b float64)

LabD50 returns the color components in the space.LabD50 color space.

func (Color) LabD65

func (c Color) LabD65() (l, a, b float64)

LabD65 returns the color components in the space.LabD65 color space.

func (Color) Lch

func (c Color) Lch() (float64, float64, float64)

Lch is an alias for Color.LchD50.

func (Color) LchD50

func (c Color) LchD50() (float64, float64, float64)

LchD50 returns the color components in the space.LchD50 color space.

func (Color) LchD65

func (c Color) LchD65() (float64, float64, float64)

LchD65 returns the color components in the space.LchD65 color space.

func (Color) Lchuv

func (c Color) Lchuv() (float64, float64, float64)

Lchuv is an alias for Color.LchuvD50.

func (Color) LchuvD50

func (c Color) LchuvD50() (float64, float64, float64)

LchuvD50 returns the color components in the space.LchuvD50 color space.

func (Color) LchuvD65

func (c Color) LchuvD65() (float64, float64, float64)

LchuvD65 returns the color components in the space.LchuvD65 color space.

func (Color) Lighten

func (c Color) Lighten(amount float64) Color

Lighten returns a copy of the color with its lightness in space.Oklch increased by amount.

The resulting lightness is clamped to [0, 1].

func (Color) LinearA98

func (c Color) LinearA98() (r, g, b float64)

LinearA98 returns the color components in the space.LinearA98 color space.

func (Color) LinearDisplayP3

func (c Color) LinearDisplayP3() (r, g, b float64)

LinearDisplayP3 returns the color components in the space.LinearDisplayP3 color space.

func (Color) LinearProPhoto

func (c Color) LinearProPhoto() (r, g, b float64)

LinearProPhoto returns the color components in the space.LinearProPhoto color space.

func (Color) LinearRec2020

func (c Color) LinearRec2020() (r, g, b float64)

LinearRec2020 returns the color components in the space.LinearRec2020 color space.

func (Color) LinearRec2100

func (c Color) LinearRec2100() (r, g, b float64)

LinearRec2100 returns the color components in the space.LinearRec2100 color space.

func (Color) LinearSrgb

func (c Color) LinearSrgb() (r, g, b float64)

LinearSrgb returns the color components in the space.LinearSrgb color space.

func (Color) Luminance

func (c Color) Luminance() float64

Luminance returns the relative luminance of the color.

func (Color) Luv

func (c Color) Luv() (l, u, v float64)

Luv is an alias for Color.LuvD50.

func (Color) LuvD50

func (c Color) LuvD50() (l, u, v float64)

LuvD50 returns the color components in the space.LuvD50 color space.

func (Color) LuvD65

func (c Color) LuvD65() (l, u, v float64)

LuvD65 returns the color components in the space.LuvD65 color space.

func (Color) MapToGamut

func (c Color) MapToGamut(dst space.Space) Color

MapToGamut maps the color to the gamut of the specified color space. It returns the original color if mapping fails.

func (Color) MarshalText

func (c Color) MarshalText() ([]byte, error)

MarshalText encodes the color as a hexadecimal text representation.

func (Color) Mix

func (c Color) Mix(other Color, t float64) Color

Mix is shorthand for Mix(c, other, t).

func (Color) MixWith

func (c Color) MixWith(other Color, t float64, opts InterpOptions) Color

MixWith is shorthand for MixWith(c, other, t, opts).

func (*Color) MutTo

func (c *Color) MutTo(dst space.Space) *Color

MutTo converts the color to the specified color space in place and returns the receiver.

func (Color) Oklab

func (c Color) Oklab() (l, a, b float64)

Oklab returns the color components in the space.Oklab color space.

func (Color) Oklch

func (c Color) Oklch() (float64, float64, float64)

Oklch returns the color components in the space.Oklch color space.

func (Color) Opaque

func (c Color) Opaque() Color

Opaque returns a copy of Color with its alpha set to 1.

func (Color) ProPhoto

func (c Color) ProPhoto() (r, g, b float64)

ProPhoto returns the color components in the space.ProPhoto color space.

func (Color) RGBA

func (c Color) RGBA() (r, g, b, a uint32)

RGBA implements the color.Color interface

func (Color) Rec2020

func (c Color) Rec2020() (r, g, b float64)

Rec2020 returns the color components in the space.Rec2020 color space.

func (Color) Rec2020OETF

func (c Color) Rec2020OETF() (r, g, b float64)

Rec2020OETF returns the color components in the space.Rec2020OETF color space.

func (Color) Rec2100HLG

func (c Color) Rec2100HLG() (r, g, b float64)

Rec2100HLG returns the color components in the space.Rec2100HLG color space.

func (Color) Rec2100PQ

func (c Color) Rec2100PQ() (r, g, b float64)

Rec2100PQ returns the color components in the space.Rec2100PQ color space.

func (Color) Rgb

func (c Color) Rgb() (r, g, b float64)

Rgb returns the sRGB components scaled by 255.

func (Color) Rgba

func (c Color) Rgba() (r, g, b, a float64)

Rgba returns the sRGB components scaled by 255 and alpha in [0, 1].

func (Color) Saturate

func (c Color) Saturate(amount float64) Color

Saturate returns a copy of the color with its chroma in space.Oklch increased by amount.

The resulting chroma is clamped to [0, +Inf).

func (Color) Space

func (c Color) Space() space.Space

Space returns the color space of the color.

Example
package main

import (
	"fmt"

	"github.com/thmalt/colors"
)

func main() {
	c := colors.Rgba(50, 60, 70, 0.995)

	// color space info
	inf := c.Space().Info()
	if inf == nil {
		fmt.Println("invalid color space: ", c.Space())
		return
	}

	fmt.Println("Space display name:", inf.DisplayName())

}
Output:
Space display name: sRGB

func (Color) Srgb

func (c Color) Srgb() (r, g, b float64)

Srgb returns the color components in the space.Srgb color space.

func (Color) Srgb8

func (c Color) Srgb8() (r, g, b uint8)

Srgb8 returns the sRGB components of the Color as 8-bit values in the range [0, 255].

func (Color) Srgb16

func (c Color) Srgb16() (r, g, b uint16)

Srgb16 returns the sRGB components of the Color as 16-bit values in the range [0, 65535].

func (Color) Srgba8

func (c Color) Srgba8() (r, g, b, a uint8)

Srgba8 returns the sRGB components and alpha of the Color as 8-bit values in the range [0, 255].

func (Color) Srgba16

func (c Color) Srgba16() (r, g, b, a uint16)

Srgba16 returns the sRGB components and alpha of the Color as 16-bit values in the range [0, 65535].

func (Color) String

func (c Color) String() string

func (Color) To

func (c Color) To(dst space.Space) Color

To converts the color to the destination color space.

Example
package main

import (
	"fmt"

	"github.com/thmalt/colors"
	"github.com/thmalt/colors/space"
)

func main() {
	c := colors.Rgba(50, 60, 70, 0.995)

	c1 := c.To(space.XyzD65)
	if !c1.IsValid() {
		return
	}

	fmt.Println(c1.Space())

	c2 := c.To(space.Oklab)
	if !c2.IsValid() {
		return
	}

	fmt.Println(c2.Space())

}
Output:
XyzD65
Oklab

func (Color) ToNRGBA

func (c Color) ToNRGBA() color.NRGBA

ToNRGBA converts the color to an sRGB color.NRGBA.

func (Color) ToNRGBA64

func (c Color) ToNRGBA64() color.NRGBA64

ToNRGBA64 converts the color to an sRGB color.NRGBA64.

func (Color) ToRGBA

func (c Color) ToRGBA() color.RGBA

ToRGBA converts the color to an sRGB color.RGBA.

func (Color) ToRGBA64

func (c Color) ToRGBA64() color.RGBA64

ToRGBA64 converts the color to an sRGB color.RGBA64.

func (Color) TryMapToGamut

func (c Color) TryMapToGamut(dst space.Space) (Color, bool)

TryMapToGamut maps the color to the gamut of the specified color space and reports whether the mapping succeeded.

func (*Color) TryMutTo

func (c *Color) TryMutTo(dst space.Space) bool

TryMutTo converts the color to the specified color space in place and reports whether the conversion succeeded.

func (Color) TryTo

func (c Color) TryTo(dst space.Space) (Color, bool)

TryTo converts the color to destination color space and reports whether the conversion succeeded.

func (*Color) UnmarshalText

func (c *Color) UnmarshalText(text []byte) error

UnmarshalText decodes a color from its CSS text representation.

func (Color) WithAlpha

func (c Color) WithAlpha(alpha float64) Color

WithAlpha returns a copy of Color with the specified alpha value. Alpha values in the range [0, 1] are typical.

func (Color) WithAlpha8

func (c Color) WithAlpha8(alpha uint8) Color

WithAlpha8 returns a copy of Color with the specified 8-bit alpha value in the range [0, 255].

func (Color) XyY

func (c Color) XyY() (x, y, luminance float64)

XyY is an alias for Color.XyYD65.

func (Color) XyYD50

func (c Color) XyYD50() (x, y, luminance float64)

XyYD50 returns the color components in the space.XyYD50 color space.

func (Color) XyYD65

func (c Color) XyYD65() (x, y, luminance float64)

XyYD65 returns the color components in the space.XyYD65 color space.

func (Color) Xyz

func (c Color) Xyz() (x, y, z float64)

Xyz is an alias for Color.XyzD65.

func (Color) XyzAbsD65

func (c Color) XyzAbsD65() (x, y, z float64)

XyzAbsD65 returns the color components in the space.XyzAbsD65 color space.

func (Color) XyzD50

func (c Color) XyzD50() (x, y, z float64)

XyzD50 returns the color components in the space.XyzD50 color space.

func (Color) XyzD65

func (c Color) XyzD65() (x, y, z float64)

XyzD65 returns the color components in the space.XyzD65 color space.

type Gradient

type Gradient struct {
	// contains filtered or unexported fields
}

Gradient stores a mixer and color stops for color interpolation. An empty gradient, including the zero value, returns an invalid zero Color when sampled. A single color stop produces a constant gradient in the interpolation space.

func NewGradient

func NewGradient(stops ...GradientStop) Gradient

NewGradient returns a new Gradient using DefaultInterpOptions.

func NewGradientWithOptions

func NewGradientWithOptions(opts InterpOptions, stops ...GradientStop) Gradient

NewGradientWithOptions creates a gradient with the specified interpolation options and color stops.

func (Gradient) At

func (g Gradient) At(t float64) Color

At returns the interpolated color at position t.

func (Gradient) AtReflect

func (g Gradient) AtReflect(t float64) Color

AtReflect returns the interpolated color at position t using reflect spreading.

func (Gradient) AtRepeat

func (g Gradient) AtRepeat(t float64) Color

AtRepeat returns the interpolated color at position t using repeat spreading.

func (Gradient) AtSpread

func (g Gradient) AtSpread(t float64, spread Spread) Color

AtSpread returns the interpolated color at position t using the specified spread method.

func (Gradient) Stops

func (g Gradient) Stops() []GradientStop

Stops returns a copy of the gradient's color stops.

type GradientBuilder

type GradientBuilder struct {
	// contains filtered or unexported fields
}

GradientBuilder builds a gradient from a sequence of color stops and hints.

func NewGradientBuilder

func NewGradientBuilder() *GradientBuilder

NewGradientBuilder returns a new GradientBuilder.

func (*GradientBuilder) AddHint

func (b *GradientBuilder) AddHint(offset float64) *GradientBuilder

AddHint adds a color interpolation hint at the given offset.

func (*GradientBuilder) AddStop

func (b *GradientBuilder) AddStop(color Color, offsets ...float64) *GradientBuilder

AddStop adds a color stop to the builder.

If no offsets are provided, the stop has no explicit position. If multiple offsets are provided, a stop is added at each offset.

func (*GradientBuilder) Build

func (b *GradientBuilder) Build() Gradient

Build builds a Gradient using the builder's stops and default interpolation options.

func (*GradientBuilder) BuildWithOptions

func (b *GradientBuilder) BuildWithOptions(opts InterpOptions) Gradient

BuildWithOptions builds a Gradient using the builder's stops and the given interpolation options.

func (*GradientBuilder) Reset

func (b *GradientBuilder) Reset() *GradientBuilder

Reset removes all gradient stops and retains the allocated storage for reuse.

func (*GradientBuilder) Stops

func (b *GradientBuilder) Stops() []GradientStop

Stops returns the gradient stops currently added to the builder

type GradientStop

type GradientStop struct {
	Color  Color
	Offset float64
	// contains filtered or unexported fields
}

GradientStop represents a color and its position within a gradient.

func NewHint

func NewHint(offset float64) GradientStop

func NewStop

func NewStop(color Color) GradientStop

func NewStopAt

func NewStopAt(color Color, offset float64) GradientStop

func NewStopsAt

func NewStopsAt(color Color, offsets ...float64) []GradientStop

func (GradientStop) HasOffset

func (s GradientStop) HasOffset() bool

HasOffset reports whether the stop has an explicit offset.

func (GradientStop) IsHint

func (s GradientStop) IsHint() bool

IsHint reports whether the stop is an interpolation hint.

type HueInterpolation

type HueInterpolation = interp.HueInterpolation

type InterpOptions

type InterpOptions struct {
	// Space specifies the color space used for interpolation.
	Space space.Space

	// Premultiplied specifies whether alpha-premultiplied interpolation is used.
	Premultiplied bool

	Hue HueInterpolation
}

func DefaultInterpOptions

func DefaultInterpOptions() InterpOptions

DefaultInterpOptions returns a Mixer configured with the default options. The defaults are:

type Mixer

type Mixer struct {
	// contains filtered or unexported fields
}

func NewMixer

func NewMixer() Mixer

NewMixer returns a new Mixer using DefaultInterpOptions.

func NewMixerWithOptions

func NewMixerWithOptions(opts InterpOptions) Mixer

NewMixerWithOptions creates a Mixer that interpolates colors in opts.Space. An invalid space defaults to space.Oklab.

func (Mixer) Mix

func (m Mixer) Mix(c1, c2 Color, t float64) Color

Mix interpolates between two colors in the mixer's color space.

func (Mixer) Raw

func (m Mixer) Raw() mixer.RawMixer

Raw returns the mixer.RawMixer.

func (Mixer) Space

func (m Mixer) Space() space.Space

Space returns the Mixer's color space.

func (Mixer) UnsafeMix

func (m Mixer) UnsafeMix(c1, c2 Color, t float64) Color

UnsafeMix interpolates between two colors in the mixer's color space. The input colors must already be in the mixer's color space.

type Spread

type Spread uint8
const (
	SpreadPad Spread = iota
	SpreadRepeat
	SpreadReflect
)

Directories

Path Synopsis
examples
conversion command
gradient command
gen
codegen/internal/convert
primitives functions
primitives functions
internal

Jump to

Keyboard shortcuts

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