color

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2025 License: MIT Imports: 6 Imported by: 0

README

color

Package color provides a robust implementation for color math as well as color manipulation methods.

Here are some of its capabilities at a glance:

package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	// Parse colors from CSS functions.
	scarlet := color.Must(color.Parse("hsl(8deg, 100%, 50%)"))

	// Generate shades, tints, and tones.
	for _, tone := range color.Tones(scarlet, 5) {
		// Color wheel functions.
		c1, c2 := color.SplitComplementary(tone)

		fmt.Println(tone.Hex(), c1.Hex(), c2.Hex())
	}

	mint := color.NewRGB(209, 227, 217)

	// Mix colors in different colorspaces.
	peach := color.MixLab(scarlet, mint)

	// Convert colors to different colorspaces.
	fmt.Println(peach.XYZWithWhitepoint(color.D55))
}

Features

  • Consistent chainable API
  • Parsing from CSS-like color functions
  • Color conversion and mixing
  • Lightness and contrast calculation
  • Color wheel functions
  • Gradients

Installation

go get github.com/shelepuginivan/color

Documentation

Is available on pkg.go.dev.

License

MIT

Documentation

Overview

Package color implements common color math functions and provides color manipulation capabilities.

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	// Parse color from CSS functions.
	scarlet := color.Must(color.Parse("hsl(8deg, 100%, 50%)"))

	// Generate shades, tints, and tones.
	for _, tone := range color.Tones(scarlet, 5) {
		// Color wheel functions.
		c1, c2 := color.SplitComplementary(tone)

		fmt.Println(tone.Hex(), c1.Hex(), c2.Hex())
	}

	mint := color.NewRGB(209, 227, 217)

	// Mix colors in different colorspaces.
	peach := color.MixLab(scarlet, mint)

	// Convert colors to different colorspaces.
	fmt.Println(peach.XYZWithWhitepoint(color.D55))
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// CIE standard illuminant A. Simulates typical, domestic,
	// tungsten-filament lighting with correlated color temperature of 2856 K.
	A = &XYZ{1.0985, 1.0000, 0.3558}

	// CIE standard illuminant C. Simulates average or north sky daylight with
	// correlated color temperature of 6774 K. Deprecated by CIE.
	C = &XYZ{0.9807, 1.0000, 1.1822}

	// Equal-energy radiator. Useful as a theoretical reference.
	E = &XYZ{1.0000, 1.0000, 1.0000}

	// CIE standard illuminant D50. Simulates warm daylight at sunrise or
	// sunset with correlated color temperature of 5003 K. Also known as
	// horizon light.
	D50 = &XYZ{0.9642, 1.0000, 0.8251}

	// CIE standard illuminant D55. Simulates mid-morning or mid-afternoon
	// daylight with correlated color temperature of 5500 K.
	D55 = &XYZ{0.9568, 1.0000, 0.9214}

	// CIE standard illuminant D65. Simulates noon daylight with correlated
	// color temperature of 6504 K.
	D65 = &XYZ{0.9505, 1.0000, 1.0888}

	// Profile Connection Space (PCS) illuminant used in ICC profiles.
	ICC = &XYZ{0.9642, 1.000, 0.8249}
)

Predefined whitepoints.

Functions

func Analogous

func Analogous(c Color) (Color, Color)

Analogous returns two colors adjacent to the given one.

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	orange := color.Must(color.ParseHex("#ff8000"))

	// Red and yellow are adjacent to orange.
	red, yellow := color.Analogous(orange)

	fmt.Println(red.Hex())
	fmt.Println(yellow.Hex())

}
Output:
#ff0000
#ffff00

func Contrast

func Contrast(c1, c2 Color) float64

Contrast calculates contrast ratio between two colors.

func Luminance

func Luminance(color Color) float64

Luminance calculates relative luminance of the color, normalized to 0 for darkest black and 1 for lightest white.

Relative luminance is calculates as per WCAG 2.2.

func SplitComplementary

func SplitComplementary(c Color) (Color, Color)

SplitComplementary returns two colors adjacent to the complementary color of the given one.

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	cyan := color.Must(color.ParseHex("#00ffff"))

	// Crimson and orange are split complementary colors of cyan.
	crimson, orange := color.SplitComplementary(cyan)

	fmt.Println(crimson.Hex())
	fmt.Println(orange.Hex())

}
Output:
#ff0080
#ff8000

func Tetradic

func Tetradic(c Color) (Color, Color, Color)

Tetradic returns 3 of the remaining colors from the tetradic colorscheme for the given color.

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	magenta := color.Must(color.ParseHex("ff00ff"))

	// Orange, green and blue are the colors in the tetradic colorscheme with magenta.
	orange, green, blue := color.Tetradic(magenta)

	fmt.Println(orange.Hex())
	fmt.Println(green.Hex())
	fmt.Println(blue.Hex())

}
Output:
#ff8000
#00ff00
#007fff

func Triadic

func Triadic(c Color) (Color, Color)

Triadic returns 2 of the remaining colors from the triadic colorscheme for the given color.

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	red := color.Must(color.ParseHex("#ff0000"))

	// Green and blue are the colors in the triadic colorscheme with red.
	green, blue := color.Triadic(red)

	fmt.Println(green.Hex())
	fmt.Println(blue.Hex())

}
Output:
#00ff00
#0000ff

Types

type CMYK

type CMYK struct {
	C int // Cyan (in percents).
	M int // Magenta (in percents).
	Y int // Yellow (in percents).
	K int // Black key (in percents).
}

CMYK represents a color in CMYK color space.

func MixCMYK

func MixCMYK(colors ...Color) *CMYK

MixCMYK calculates the average color in CMYK colorspace from an arbitrary number of colors.

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	var (
		cyan    = color.NewCMYK(100, 0, 0, 40)
		magenta = color.NewCMYK(0, 100, 0, 20)
		yellow  = color.NewCMYK(0, 0, 100, 60)
	)

	grey := color.MixCMYK(cyan, magenta, yellow)

	fmt.Printf("cmyk(%d%%, %d%%, %d%%, %d%%)", grey.C, grey.M, grey.Y, grey.K)
}
Output:
cmyk(33%, 33%, 33%, 40%)

func NewCMYK

func NewCMYK(c, m, y, k int) *CMYK

NewCMYK returns a new instance of CMYK.

func (CMYK) CMYK

func (c CMYK) CMYK() *CMYK

CMYK returns the color unchanged. This method is required to implement the Color interface.

func (*CMYK) Edit

func (c *CMYK) Edit(editfn func(c *CMYK)) *CMYK

Edit allows in-place modification of the CMYK color instance using the provided editing function.

The returned value is a pointer to the same instance of CMYK, so it should not be used to assign values to other variables. It is intended for method chaining.

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	orange := color.NewCMYK(0, 47, 90, 0) // rgb(255, 134, 26)

	// Make it darker and print RGB.
	fmt.Println(orange.Edit(func(c *color.CMYK) {
		c.K = 50
	}).RGB())
}
Output:
rgb(128, 68, 13)

func (CMYK) HSL

func (c CMYK) HSL() *HSL

HSL returns HSL representation of color (hue, saturation, lightness).

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	skyblue := color.NewCMYK(43, 12, 0, 8)
	fmt.Println(skyblue.HSL())
}
Output:
hsl(197, 72%, 72%)

func (CMYK) HSV

func (c CMYK) HSV() *HSV

HSV returns HSV representation of color (hue, saturation, value).

func (CMYK) Hex

func (c CMYK) Hex() string

Hex returns hexadecimal representation of color.

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	coral := color.NewCMYK(0, 50, 70, 0)
	fmt.Println(coral.Hex())
}
Output:
#ff804d

func (CMYK) Lab

func (c CMYK) Lab() *Lab

Lab returns Lab representation of color (lightness, red-green, yellow-blue).

D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.

func (CMYK) Lch

func (c CMYK) Lch() *Lch

Lch returns Lch representation of color (lightness, chroma, hue).

D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.

func (CMYK) Oklab added in v0.2.0

func (c CMYK) Oklab() *Oklab

Oklab returns Oklab representation of color (lightness, red-green, yellow-blue).

func (CMYK) Oklch added in v0.2.0

func (c CMYK) Oklch() *Oklch

Oklch returns Oklch representation of color (lightness, chroma, hue).

func (CMYK) RGB

func (c CMYK) RGB() *RGB

RGB returns RGB representation of color (red, green, blue).

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	yellow := color.NewCMYK(0, 0, 100, 0)
	fmt.Println(yellow.RGB())
}
Output:
rgb(255, 255, 0)

func (CMYK) String

func (c CMYK) String() string

String returns string representation of CMYK.

func (CMYK) XYZ

func (c CMYK) XYZ() *XYZ

XYZ returns XYZ representation of color (long wavelengths, brightness, short wavelengths).

type Color

type Color interface {
	CMYK() *CMYK
	Hex() string
	HSL() *HSL
	HSV() *HSV
	Lab() *Lab
	Lch() *Lch
	Oklab() *Oklab
	Oklch() *Oklch
	RGB() *RGB
	XYZ() *XYZ
	String() string
}

Color is a common interface for all package structures. It is used for methods that accept colors in any color space.

func Complementary

func Complementary(c Color) Color

Complementary returns a complementary color for the given one. Complementary colors are two colors that are directly across from one another on the color wheel.

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	blue := color.Must(color.ParseHex("#0000ff"))

	// Yellow is the complementary color for blue.
	yellow := color.Complementary(blue)

	fmt.Println(yellow.Hex())
}
Output:
#ffff00

func ContrastBlackWhite

func ContrastBlackWhite(c1 Color) Color

ContrastBlackWhite returns black or white depending on luminance of the given color.

func Must

func Must(c Color, err error) Color

Must is a helper that wraps a call to a function returning (Color, error) and panics if the error is not nil. It is intended for use in variable initializations such as

c := color.Must(color.ParseHex("#ffffff"))

func Parse

func Parse(s string) (Color, error)

Parse is a generic color parsing function.

The following order is used for parsing:

  1. CSS named colors using color.ParseNamed
  2. Hexadecimal notation using color.ParseHex
  3. Color functions using color.ParseFunc

See color.ParseNamed, color.ParseHex, and color.ParseFunc for more information about color notations supported by Parse.

func ParseFunc

func ParseFunc(fnstring string) (Color, error)

ParseFunc parses the color function and returns a color.

A color function is a string starting with the function name (e.g. rgb), containing its parameters in parentheses. Some examples are:

  • rgb(255, 255, 255)
  • hsl(0.8turn 80% 30%)
  • xyz(0.9642, 1.0000, 0.8251)

Arguments of the function may or may not be comma-separated.

The following units are supported for the arguments:

  • Percents (%)
  • Radians (rad)
  • Turns (turn)
  • Degrees (deg)

Additionally, none is interpreted as zero:

color.ParseFunc("lch(29.2345% 44.2 none)") // &color.Lch{0.29345, 44.2, 0}

Note that ParseFunc does not implement CSS color functions completely. For example, there is no support for relative values; i.e. the following call:

color.ParseFunc("hsl(from green h s l / 0.5)")

results in error.

func ParseHex

func ParseHex(hex string) (Color, error)

ParseHex returns a color by parsing hexadecimal color string. The string may start with hash character (`#`) and may be either short or long hexadecimal color. Hence, all of the following strings are valid arguments:

  • fff
  • #fff
  • ffffff
  • #ffffff

func ParseNamed

func ParseNamed(name string) (Color, error)

ParseNamed returns a CSS named color by name.

func Shades

func Shades(color Color, n int) []Color

Shades returns n shades of the color. New colors are created by reducing lightness of the original color, making them darker.

The first element of the resulting slice is the color itself. The last is black. Special case is n < 2: a slice with a single element being the color itself is returned.

func Tints

func Tints(color Color, n int) []Color

Tints returns n tints shades of the color. New colors are created by increasing lightness of the original color, making them lighter.

The first element of the resulting slice is the color itself. The last is white. Special case is n < 2: a slice with a single element being the color itself is returned.

func Tones

func Tones(color Color, n int) []Color

Tones returns n tones of the color. New colors are created by reducing saturation of the original color and changing lightness towards 50% (in HSL), making them more grayish.

The first element of the resulting slice is the color itself. The last is gray. Special case is n < 2: a slice with a single element being the color itself is returned.

type HSL

type HSL struct {
	H int // Hue (in degrees).
	S int // Saturation (in percents).
	L int // Lightness (in percents).
}

HSL represents color in HSL colorspace.

func MixHSL

func MixHSL(colors ...Color) *HSL

MixHSL calculates the average color in HSL colorspace from an arbitrary number of colors.

func NewHSL

func NewHSL(h, s, l int) *HSL

NewHSL returns a new instance of HSL.

func (HSL) CMYK

func (c HSL) CMYK() *CMYK

CMYK returns CMYK representation of color (cyan, magenta, yellow, key).

func (*HSL) Edit

func (c *HSL) Edit(editfn func(c *HSL)) *HSL

Edit allows in-place modification of the HSL color instance using the provided editing function.

The returned value is a pointer to the same instance of HSL, so it should not be used to assign values to other variables. It is intended for method chaining.

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	cyan := color.NewHSL(180, 100, 50) // rgb(0, 255, 255)

	// Decrease saturation and print as RGB.
	fmt.Println(cyan.Edit(func(c *color.HSL) {
		c.S = 30
	}).RGB())
}
Output:
rgb(89, 166, 166)

func (HSL) HSL

func (c HSL) HSL() *HSL

HSL returns the color unchanged. This method is required to implement the Color interface.

func (HSL) HSV

func (c HSL) HSV() *HSV

HSV returns HSV representation of color (hue, saturation, value).

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	c := color.NewHSL(30, 20, 60)
	hsv := c.HSV()

	fmt.Printf("hsv(%d, %d%%, %d%%)\n", hsv.H, hsv.S, hsv.V)
}
Output:
hsv(30, 24%, 68%)

func (HSL) Hex

func (c HSL) Hex() string

Hex returns hexadecimal representation of color.

func (HSL) Lab

func (c HSL) Lab() *Lab

Lab returns Lab representation of color (lightness, red-green, yellow-blue).

D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.

func (HSL) Lch

func (c HSL) Lch() *Lch

Lch returns Lch representation of color (lightness, chroma, hue).

D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.

func (HSL) Oklab added in v0.2.0

func (c HSL) Oklab() *Oklab

Oklab returns Oklab representation of color (lightness, red-green, yellow-blue).

func (HSL) Oklch added in v0.2.0

func (c HSL) Oklch() *Oklch

Oklch returns Oklch representation of color (lightness, chroma, hue).

func (HSL) RGB

func (c HSL) RGB() *RGB

RGB returns RGB representation of color (red, green, blue).

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	c := color.NewHSL(0, 100, 50)
	fmt.Println(c.RGB())
}
Output:
rgb(255, 0, 0)

func (HSL) String

func (c HSL) String() string

String returns string representation of HSL.

func (HSL) XYZ

func (c HSL) XYZ() *XYZ

XYZ returns XYZ representation of color (long wavelengths, brightness, short wavelengths).

type HSV

type HSV struct {
	H int // Hue (in degrees).
	S int // Saturation (in percents).
	V int // Value (in percents).
}

HSV represents color in HSV colorspace.

func NewHSV

func NewHSV(h, s, v int) *HSV

NewHSV returns a new instance of HSV.

func (HSV) CMYK

func (c HSV) CMYK() *CMYK

CMYK returns CMYK representation of color (cyan, magenta, yellow, key).

func (*HSV) Edit

func (c *HSV) Edit(editfn func(c *HSV)) *HSV

Edit allows in-place modification of the HSV color instance using the provided editing function.

The returned value is a pointer to the same instance of HSV, so it should not be used to assign values to other variables. It is intended for method chaining.

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	green := color.NewHSV(72, 100, 40) // rgb(78, 102, 0)

	// Increase value and print as RGB.
	fmt.Println(green.Edit(func(c *color.HSV) {
		c.V = 100
	}).RGB())
}
Output:
rgb(204, 255, 0)

func (HSV) HSL

func (c HSV) HSL() *HSL

HSL returns HSL representation of color (hue, saturation, lightness).

func (HSV) HSV

func (c HSV) HSV() *HSV

HSV returns the color unchanged. This method is required to implement the Color interface.

func (HSV) Hex

func (c HSV) Hex() string

Hex returns hexadecimal representation of color.

func (HSV) Lab

func (c HSV) Lab() *Lab

Lab returns Lab representation of color (lightness, red-green, yellow-blue).

D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.

func (HSV) Lch

func (c HSV) Lch() *Lch

Lch returns Lch representation of color (lightness, chroma, hue).

D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.

func (HSV) Oklab added in v0.2.0

func (c HSV) Oklab() *Oklab

Oklab returns Oklab representation of color (lightness, red-green, yellow-blue).

func (HSV) Oklch added in v0.2.0

func (c HSV) Oklch() *Oklch

Oklch returns Oklch representation of color (lightness, chroma, hue).

func (HSV) RGB

func (c HSV) RGB() *RGB

RGB returns RGB representation of color (red, green, blue).

func (HSV) String

func (c HSV) String() string

String returns string representation of HSV.

func (HSV) XYZ

func (c HSV) XYZ() *XYZ

XYZ returns XYZ representation of color (long wavelengths, brightness, short wavelengths).

type Lab

type Lab struct {
	L float64 // L represents lightness of the color.
	A float64 // A represents the green-red component of the color.
	B float64 // B represents the yellow-blue component of the color.
}

Lab represents a color in Lab colorspace.

func MixLab

func MixLab(colors ...Color) *Lab

MixLab calculates the average color in Lab colorspace from an arbitrary number of colors.

func NewLab

func NewLab(l, a, b float64) *Lab

NewLab returns a new instance of Lab with the default reference white D65.

func (Lab) CMYK

func (c Lab) CMYK() *CMYK

CMYK returns CMYK representation of color (cyan, magenta, yellow, key).

func (*Lab) Edit

func (c *Lab) Edit(editfn func(c *Lab)) *Lab

Edit allows in-place modification of the Lab color instance using the provided editing function.

The returned value is a pointer to the same instance of Lab, so it should not be used to assign values to other variables. It is intended for method chaining.

func (Lab) HSL

func (c Lab) HSL() *HSL

HSL returns HSL representation of color (hue, saturation, lightness).

func (Lab) HSV

func (c Lab) HSV() *HSV

HSV returns HSV representation of color (hue, saturation, value).

func (Lab) Hex

func (c Lab) Hex() string

Hex returns hexadecimal representation of color.

func (Lab) Lab

func (c Lab) Lab() *Lab

Lab returns the color unchanged. This method is required to implement the Color interface.

func (Lab) Lch

func (c Lab) Lch() *Lch

Lch returns Lch representation of color (lightness, chroma, hue).

func (Lab) Oklab added in v0.2.0

func (c Lab) Oklab() *Oklab

Oklab returns Oklab representation of color (lightness, red-green, yellow-blue).

func (Lab) Oklch added in v0.2.0

func (c Lab) Oklch() *Oklch

Oklch returns Oklch representation of color (lightness, chroma, hue).

func (Lab) RGB

func (c Lab) RGB() *RGB

RGB returns RGB representation of color (red, green, blue).

func (Lab) String

func (c Lab) String() string

String returns string representation of Lab.

func (Lab) XYZ

func (c Lab) XYZ() *XYZ

XYZ returns XYZ representation of color. D65 is used as whitepoint, use Lab.XYZWithWhitepoint to specify a different whitepoint.

func (Lab) XYZWithWhitepoint

func (c Lab) XYZWithWhitepoint(white *XYZ) *XYZ

XYZWithWhitepoint returns XYZ representation of color and allows to specify whitepoint.

type Lch

type Lch struct {
	L float64 // L represents lightness of the color.
	C float64 // C represents relative saturation (chroma).
	H int     // H represents angle of the hue in the CIELAB color wheel (in degrees).
}

Lch represents a color in Lch colorspace.

func NewLch

func NewLch(l, c float64, h int) *Lch

NewLch returns a new instance of Lch.

func (Lch) CMYK

func (c Lch) CMYK() *CMYK

CMYK returns CMYK representation of color (cyan, magenta, yellow, key).

func (*Lch) Edit

func (c *Lch) Edit(editfn func(c *Lch)) *Lch

Edit allows in-place modification of the Lch color instance using the provided editing function.

The returned value is a pointer to the same instance of Lch, so it should not be used to assign values to other variables. It is intended for method chaining.

func (Lch) HSL

func (c Lch) HSL() *HSL

HSL returns HSL representation of color (hue, saturation, lightness).

func (Lch) HSV

func (c Lch) HSV() *HSV

HSV returns HSV representation of color (hue, saturation, value).

func (Lch) Hex

func (c Lch) Hex() string

Hex returns hexadecimal representation of color.

func (Lch) Lab

func (c Lch) Lab() *Lab

Lab returns Lab representation of color (lightness, red-green, yellow-blue).

func (Lch) Lch

func (c Lch) Lch() *Lch

Lch returns the color unchanged. This method is required to implement the Color interface.

func (Lch) Oklab added in v0.2.0

func (c Lch) Oklab() *Oklab

Oklab returns Oklab representation of color (lightness, red-green, yellow-blue).

func (Lch) Oklch added in v0.2.0

func (c Lch) Oklch() *Oklch

Oklch returns Oklch representation of color (lightness, chroma, hue).

func (Lch) RGB

func (c Lch) RGB() *RGB

RGB returns RGB representation of color (red, green, blue).

func (Lch) String

func (c Lch) String() string

String returns string representation of Lch.

func (Lch) XYZ

func (c Lch) XYZ() *XYZ

XYZ returns XYZ representation of color. D65 is used as whitepoint, use Lab.XYZWithWhitepoint to specify a different whitepoint.

type Oklab added in v0.2.0

type Oklab struct {
	L float64 // L represents lightness of the color.
	A float64 // A represents the green-red component of the color.
	B float64 // B represents the yellow-blue component of the color.
}

Oklab represents a color in Oklab colorspace.

func NewOklab added in v0.2.0

func NewOklab(l, a, b float64) *Oklab

NewOklab returns a new instance of Oklab.

func (Oklab) CMYK added in v0.2.0

func (c Oklab) CMYK() *CMYK

CMYK returns CMYK representation of color (cyan, magenta, yellow, key).

func (*Oklab) Edit added in v0.2.0

func (c *Oklab) Edit(editfn func(*Oklab)) *Oklab

Edit allows in-place modification of the Oklab color instance using the provided editing function.

The returned value is a pointer to the same instance of Oklab, so it should not be used to assign values to other variables. It is intended for method chaining.

func (Oklab) HSL added in v0.2.0

func (c Oklab) HSL() *HSL

HSL returns HSL representation of color (hue, saturation, lightness).

func (Oklab) HSV added in v0.2.0

func (c Oklab) HSV() *HSV

HSV returns HSV representation of color (hue, saturation, value).

func (Oklab) Hex added in v0.2.0

func (c Oklab) Hex() string

Hex returns hexadecimal representation of color.

func (Oklab) Lab added in v0.2.0

func (c Oklab) Lab() *Lab

Lab returns the color unchanged. This method is required to implement the Color interface.

func (Oklab) Lch added in v0.2.0

func (c Oklab) Lch() *Lch

Lch returns Lch representation of color (lightness, chroma, hue).

func (Oklab) Oklab added in v0.2.0

func (c Oklab) Oklab() *Oklab

Oklab returns the color unchanged. This method is required to implement the Color interface.

func (Oklab) Oklch added in v0.2.0

func (c Oklab) Oklch() *Oklch

Oklch returns Oklch representation of color (lightness, chroma, hue).

func (Oklab) RGB added in v0.2.0

func (c Oklab) RGB() *RGB

RGB returns RGB representation of color (red, green, blue).

func (Oklab) String added in v0.2.0

func (c Oklab) String() string

String returns string representation of Oklab

func (Oklab) XYZ added in v0.2.0

func (c Oklab) XYZ() *XYZ

XYZ returns XYZ representation of color (long wavelengths, brightness, short wavelengths).

type Oklch added in v0.2.0

type Oklch struct {
	L float64 // L represents lightness of the color.
	C float64 // C represents relative saturation (chroma).
	H int     // H represents angle of the hue in the Oklab color wheel (in degrees).
}

Oklch represents a color in Oklch colorspace, a cylindrical counterpart of Oklab.

func NewOklch added in v0.2.0

func NewOklch(l, c float64, h int) *Oklch

NewOklch returns a new instance of Oklch.

func (Oklch) CMYK added in v0.2.0

func (c Oklch) CMYK() *CMYK

CMYK returns CMYK representation of color (cyan, magenta, yellow, key).

func (*Oklch) Edit added in v0.2.0

func (c *Oklch) Edit(editfn func(*Oklch)) *Oklch

Edit allows in-place modification of the Oklch color instance using the provided editing function.

The returned value is a pointer to the same instance of Oklch, so it should not be used to assign values to other variables. It is intended for method chaining.

func (Oklch) HSL added in v0.2.0

func (c Oklch) HSL() *HSL

HSL returns HSL representation of color (hue, saturation, lightness).

func (Oklch) HSV added in v0.2.0

func (c Oklch) HSV() *HSV

HSV returns HSV representation of color (hue, saturation, value).

func (Oklch) Hex added in v0.2.0

func (c Oklch) Hex() string

Hex returns hexadecimal representation of color.

func (Oklch) Lab added in v0.2.0

func (c Oklch) Lab() *Lab

Lab returns Lab representation of color (lightness, red-green, yellow-blue).

func (Oklch) Lch added in v0.2.0

func (c Oklch) Lch() *Lch

Lch returns Lch representation of color (lightness, chroma, hue).

func (Oklch) Oklab added in v0.2.0

func (c Oklch) Oklab() *Oklab

Oklab returns Oklab representation of color (lightness, red-green, yellow-blue).

func (Oklch) Oklch added in v0.2.0

func (c Oklch) Oklch() *Oklch

Oklch returns the color unchanged. This method is required to implement the Color interface.

func (Oklch) RGB added in v0.2.0

func (c Oklch) RGB() *RGB

RGB returns RGB representation of color (red, green, blue).

func (Oklch) String added in v0.2.0

func (c Oklch) String() string

String returns string representation of Oklab

func (Oklch) XYZ added in v0.2.0

func (c Oklch) XYZ() *XYZ

type RGB

type RGB struct {
	R uint8 // Red.
	G uint8 // Green.
	B uint8 // Blue.
}

RGB represents a color in RGB color space.

func MixRGB

func MixRGB(colors ...Color) *RGB

MixRGB calculates the average color in RGB colorspace from an arbitrary number of colors.

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	var (
		red   = color.NewRGB(255, 0, 0)
		green = color.NewRGB(0, 255, 0)
		blue  = color.NewRGB(0, 0, 255)
	)

	grey := color.MixRGB(red, green, blue)

	fmt.Printf("rgb(%d, %d, %d)\n", grey.R, grey.G, green.B)

}

func NewRGB

func NewRGB(r, g, b uint8) *RGB

NewRGB returns a new instance of RGB.

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	green := color.NewRGB(0, 255, 0)

	fmt.Println(green.Hex())
}
Output:
#00ff00

func (RGB) CMYK

func (c RGB) CMYK() *CMYK

CMYK returns CMYK representation of color (cyan, magenta, yellow, key).

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	yellow := color.NewRGB(255, 255, 0)
	cmyk := yellow.CMYK()
	fmt.Printf("cmyk(%d%%, %d%%, %d%%, %d%%)", cmyk.C, cmyk.M, cmyk.Y, cmyk.K)
}
Output:
cmyk(0%, 0%, 100%, 0%)

func (*RGB) Edit

func (c *RGB) Edit(editfn func(c *RGB)) *RGB

Edit allows in-place modification of the RGB color instance using the provided editing function.

The returned value is a pointer to the same instance of RGB, so it should not be used to assign values to other variables. It is intended for method chaining.

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	silver := color.NewRGB(191, 191, 191)
	fmt.Println(silver.Edit(func(c *color.RGB) {
		c.R += 10
		c.G += 20
		c.B += 30
	}))
}
Output:
rgb(201, 211, 221)

func (RGB) HSL

func (c RGB) HSL() *HSL

HSL returns HSL representation of color (hue, saturation, lightness).

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	c := color.NewRGB(219, 188, 127)
	fmt.Println(c.HSL())
}
Output:
hsl(40, 56%, 68%)

func (RGB) HSV

func (c RGB) HSV() *HSV

HSV returns HSV representation of color (hue, saturation, value).

func (RGB) Hex

func (c RGB) Hex() string

Hex returns hexadecimal representation of color.

Example
package main

import (
	"fmt"

	"github.com/shelepuginivan/color"
)

func main() {
	black := color.NewRGB(0, 0, 0)
	fmt.Println(black.Hex())
}
Output:
#000000

func (RGB) Lab

func (c RGB) Lab() *Lab

Lab returns Lab representation of color (lightness, red-green, yellow-blue).

D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.

func (RGB) Lch

func (c RGB) Lch() *Lch

Lch returns Lch representation of color (lightness, chroma, hue).

D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.

func (RGB) Oklab added in v0.2.0

func (c RGB) Oklab() *Oklab

Oklab returns Oklab representation of color (lightness, red-green, yellow-blue).

func (RGB) Oklch added in v0.2.0

func (c RGB) Oklch() *Oklch

Oklch returns Oklch representation of color (lightness, chroma, hue).

func (RGB) RGB

func (c RGB) RGB() *RGB

RGB returns the color unchanged. This method is required to implement the Color interface.

func (RGB) String

func (c RGB) String() string

String returns string representation of RGB.

func (RGB) XYZ

func (c RGB) XYZ() *XYZ

XYZ returns XYZ representation of color (long wavelengths, brightness, short wavelengths).

type XYZ

type XYZ struct {
	X float64 // X represents a combination of long wavelengths (red).
	Y float64 // Y corresponds to the luminance or brightness of the color.
	Z float64 // Z captures the short wavelengths (blue).
}

XYZ represents a color in XYZ color space.

func NewXYZ

func NewXYZ(x, y, z float64) *XYZ

NewXYZ returns a new instance of XYZ.

func (XYZ) CMYK

func (c XYZ) CMYK() *CMYK

CMYK returns CMYK representation of color (cyan, magenta, yellow, key).

func (*XYZ) Edit

func (c *XYZ) Edit(editfn func(c *XYZ)) *XYZ

Edit allows in-place modification of the XYZ color instance using the provided editing function.

The returned value is a pointer to the same instance of XYZ, so it should not be used to assign values to other variables. It is intended for method chaining.

func (XYZ) HSL

func (c XYZ) HSL() *HSL

HSL returns HSL representation of color (hue, saturation, lightness).

func (XYZ) HSV

func (c XYZ) HSV() *HSV

HSV returns HSV representation of color (hue, saturation, value).

func (XYZ) Hex

func (c XYZ) Hex() string

Hex returns hexadecimal representation of color.

func (XYZ) Lab

func (c XYZ) Lab() *Lab

Lab returns Lab representation of color (lightness, red-green, yellow-blue).

D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.

func (XYZ) LabWithWhitepoint

func (c XYZ) LabWithWhitepoint(white *XYZ) *Lab

LabWithWhitepoint returns Lab representation of color, allowing to specify reference white color.

func (XYZ) Lch

func (c XYZ) Lch() *Lch

Lch returns Lch representation of color (lightness, chroma, hue).

D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.

func (XYZ) Oklab added in v0.2.0

func (c XYZ) Oklab() *Oklab

Oklab returns Oklab representation of color (lightness, red-green, yellow-blue).

func (XYZ) Oklch added in v0.2.0

func (c XYZ) Oklch() *Oklch

Oklch returns Oklch representation of color (lightness, chroma, hue).

func (XYZ) RGB

func (c XYZ) RGB() *RGB

RGB returns RGB representation of color (red, green, blue).

func (XYZ) String

func (c XYZ) String() string

String returns string representation of XYZ.

func (XYZ) XYZ

func (c XYZ) XYZ() *XYZ

XYZ returns the color unchanged. This method is required to implement the Color interface.

Directories

Path Synopsis
Package gradient provides methods for creating color gradients.
Package gradient provides methods for creating color gradients.
internal
integers
Package integers provides utilities for integer numbers.
Package integers provides utilities for integer numbers.
interpolate
Package interpolate provides helper functions for gradient color interpolation.
Package interpolate provides helper functions for gradient color interpolation.
percents
Package percents provides internal normalization functions for percents.
Package percents provides internal normalization functions for percents.

Jump to

Keyboard shortcuts

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