colorspace

package module
v0.0.0-...-3de7f4f Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2025 License: BSD-3-Clause Imports: 4 Imported by: 0

README

colorspace

go.dev reference Go Report Card codecov Go sourcegraph

colorspace implements different color space logic to allow for conversion from colorspace to colorspace and interpolation within each colorspace.

How to install package with newer versions of Go (+1.16):

go mod download github.com/soypat/colorspace@latest

Linear interpolation example

Shown in each image are 5 different color gradients generated with linear interpolation in each available colorspace. See examples/lerp:

  1. Topmost: sRGB. This is the naive linear interpolation
  2. Linear sRGB.
  3. CIE XYZ
  4. OKLAB
  5. OKLCH. Designed to yield the most perceptively uniform gradient.

greyred to blue lerp red to blue lerp white to blue lerp white to black lerp

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LerpCIEXYZ

func LerpCIEXYZ(c1, c2 color.Color, v float32) color.Color

LerpCIEXYZ interpolates in device-independent CIE XYZ space. Useful for cross-device workflows and conversions, not perceptually uniform.

func LerpLSRGB

func LerpLSRGB(c1, c2 color.Color, v float32) color.Color

LerpLSRGB interpolates in linear-light sRGB (after removing gamma). More physically accurate than plain sRGB (like mixing light). Best for image compositing and blending intensities.

func LerpOKLAB

func LerpOKLAB(c1, c2 color.Color, v float32) color.Color

LerpOKLAB interpolates in OKLab, a perceptually uniform space. Produces smooth, visually even blends. Best for perceptual color mixing and gradients.

func LerpOKLCH

func LerpOKLCH(c1, c2 color.Color, v float32) color.Color

LerpOKLCH interpolates in OKLCH (lightness, chroma, hue). Preserves hue direction and interpolates hue angles correctly. Best for perceptual gradients where hue continuity matters.

func LerpSRGB

func LerpSRGB(c1, c2 color.Color, v float32) color.Color

LerpSRGB interpolates directly in gamma-encoded sRGB. Fast and simple, but not perceptually uniform. Best for quick blends where accuracy is not critical.

Types

type CIELAB

type CIELAB struct {
	// L* (L-star) Perceptual Lightness calcuilated using the cube root of relative luminance with an offset near black.
	// Defines black at 0 and white at 1.
	L float32
	// a* axis (unbounded) Varies greenish appearance.
	A float32
	// b* axis (unbounded) varies red/green/yellow to blue.
	B float32
}

CIELAB or also known as LAB, is a color model defined by the international commission on illumination (CIE) in 1976. It is designed so that a given numerical change always corresponds to a similar preceived change in color. Since a* and b* axes are unbounded a correct CIELAB color may not be representable in sRGB gamut.

func (CIELAB) Array

func (c CIELAB) Array() [3]float32

func (CIELAB) CIELCH

func (c CIELAB) CIELCH() CIELCH

func (CIELAB) CIEXYZ

func (c CIELAB) CIEXYZ() CIEXYZ

func (CIELAB) Lerp

func (from CIELAB) Lerp(to CIELAB, v float32) CIELAB

type CIELCH

type CIELCH struct {
	L float32 // Perceptual luminosity. Same as for [CIELAB].
	C float32 // chroma.
	H float32 // hue.
}

CIELCH is the cylindiracl hue color space representation of CIELAB.

func (CIELCH) Array

func (c CIELCH) Array() [3]float32

func (CIELCH) CIELAB

func (c CIELCH) CIELAB() CIELAB

func (CIELCH) Lerp

func (from CIELCH) Lerp(to CIELCH, v float32) CIELCH

type CIEXYZ

type CIEXYZ struct {
	X, Y, Z float32
}

CIEXYZ refers to the 1931 CIE color space defined such that a mixture between two colors in some proportion lies on the line between those two colors in this space. One disadvantage of this model is that it is not perceptually uniform. The disadvantage is remedied in subsequent color models such as CIELUV and CIELAB. The XYZ values are also called tristiumulants.

func Illuminant

func Illuminant(ynormal, xchroma, ychroma float32) CIEXYZ

Illuminant returns a standard illuminant given the CIE chromaticity coordinates of a perfectly reflecting (or transmitting) diffuser.

func IlluminantD50

func IlluminantD50(ynormal float32) CIEXYZ

IlluminantD50 returns the standard illuminant representing horizon light (D50). Values are normalized to the y value provided.

func IlluminantD65

func IlluminantD65(ynormal float32) CIEXYZ

IlluminantD65 returns the standard illuminant which represents noon daylight (D65). Values are normalized to the y value provided.

func (CIEXYZ) Array

func (c CIEXYZ) Array() [3]float32

func (CIEXYZ) CIELAB

func (c CIEXYZ) CIELAB() CIELAB

func (CIEXYZ) LSRGB

func (c CIEXYZ) LSRGB() LSRGB

func (CIEXYZ) Lerp

func (from CIEXYZ) Lerp(to CIEXYZ, v float32) CIEXYZ

func (CIEXYZ) OKLAB

func (c CIEXYZ) OKLAB() OKLAB

type HSL

type HSL struct {
	H float32 // Hue. Is the radial component.
	S float32 // Saturation. Also known as chroma. Corresponds to intensity of color.
	L float32 // Lightness.
}

HSL is the Hue–Saturation–Lightness cylindrical-coordinate color space. It is similar to HSV but defines the third axis as lightness rather than value. HSL is widely used in digital art and CSS because its parameters allow for intuitive control over tints, shades, and tones.

func (HSL) Array

func (c HSL) Array() [3]float32

func (HSL) SRGB

func (hsl HSL) SRGB() SRGB

SRGB converts HSL to gamma-encoded sRGB. Inputs: H in degrees, S,L in [0,1].

type HSV

type HSV struct {
	H float32
	S float32
	V float32
}

HSV is the Hue–Saturation–Value cylindrical-coordinate color space. It is often used in user interfaces and color pickers because it aligns more closely with human perception of color attributes than RGB.

Components:

  • H (Hue): The angle of the color on the color wheel, measured in degrees. Range is [0, 360). Red = 0°, Green = 120°, Blue = 240°. Undefined for achromatic colors (S = 0).
  • S (Saturation): The intensity or purity of the hue, in [0, 1]. 0 means grayscale (no color), 1 means fully saturated.
  • V (Value): The brightness of the color, in [0, 1]. 0 corresponds to black, 1 corresponds to the brightest form of the color given its hue and saturation.

Example usage:

HSV{H: 0, S: 1, V: 1}   // pure red
HSV{H: 120, S: 1, V: 1} // pure green
HSV{H: 240, S: 1, V: 1} // pure blue

func (HSV) Array

func (c HSV) Array() [3]float32

func (HSV) SRGB

func (hsv HSV) SRGB() SRGB

SRGB converts HSV to gamma-encoded sRGB. Inputs: H in degrees, S,V in [0,1].

type LSRGB

type LSRGB struct {
	R float32 // Red.
	G float32 // Green.
	B float32 // Blue.
}

LSRGB is linear-light (un-companded) color space.

func (LSRGB) Array

func (c LSRGB) Array() [3]float32

func (LSRGB) CIEXYZ

func (c LSRGB) CIEXYZ() CIEXYZ

func (LSRGB) ClipToGamut

func (c LSRGB) ClipToGamut() LSRGB

ClipToGamut clamps each channel of the linear-light RGB color to [0,1]. Useful after computations that may push values slightly outside the gamut.

func (LSRGB) InGamut

func (c LSRGB) InGamut() bool

InGamut reports whether the linear-light RGB color lies inside the sRGB gamut. Returns true if all channels are in [0,1], false otherwise.

func (LSRGB) Lerp

func (from LSRGB) Lerp(to LSRGB, v float32) LSRGB

func (LSRGB) SRGB

func (c LSRGB) SRGB() SRGB

type OKLAB

type OKLAB struct {
	// Preceptual lightness. See [LAB]
	L float32
	// A and B for opposite channels of the four unique hues. unbounded but in practice ranging from -0.5 to 0.5.
	// CSS assigns ±100% to ±0.4 for both.
	A float32
	B float32
}

OKLAB is a uniform color space for device independent coloring designed to improve preceptual uniformity, hue and lightness prediction, color blending and usability regarding numerical stability.

func (OKLAB) Array

func (c OKLAB) Array() [3]float32

func (OKLAB) CIEXYZ

func (c OKLAB) CIEXYZ() CIEXYZ

func (OKLAB) DeltaE

func (reference OKLAB) DeltaE(sample OKLAB) float32

func (OKLAB) Lerp

func (from OKLAB) Lerp(to OKLAB, v float32) OKLAB

func (OKLAB) OKLCH

func (c OKLAB) OKLCH() OKLCH

type OKLCH

type OKLCH struct {
	L float32 // Perceptual luminosity. Same as for [OKLAB].
	C float32 // Chroma. Defines intensity of hue.
	H float32 // Hue in degrees.
}

OKLCH is cylindrical representation of OKLAB color space.

func (OKLCH) Array

func (c OKLCH) Array() [3]float32

func (OKLCH) GamutMappedLSRGB

func (c OKLCH) GamutMappedLSRGB() OKLCH

GamutMappedLSRGB maps the OKLCH color into the sRGB gamut.

If the color is already representable in sRGB, it is returned unchanged. Otherwise, chroma is reduced until the color can be expressed in linear sRGB without clipping, while keeping lightness and hue as stable as possible.

Best used after interpolation in OKLab/OKLCH to ensure the result is displayable.

func (OKLCH) Lerp

func (from OKLCH) Lerp(to OKLCH, v float32) OKLCH

func (OKLCH) OKLAB

func (c OKLCH) OKLAB() OKLAB

OKLAB converts the OKLCH cylindrical representation back to OKLab Cartesian form. Hue (H) is interpreted in degrees, and converted into a* (A) and b* (B) axes.

type SRGB

type SRGB struct {
	R float32 // Red.
	G float32 // Green.
	B float32 // Blue.
}

SRGB usually denoted as sRGB (standard Red-Green-Blue) is a color space for use on printers, monitors and the world wide web. This is to say most monitors/printers receive color data as a triplets for each pixel representing redness, greeness and blueness.

func ColorToSRGB

func ColorToSRGB(c color.Color) SRGB

ColorToSRGB converts the color to SRGB discarding the opacity/alpha (A) field.

func (SRGB) Array

func (c SRGB) Array() [3]float32

func (SRGB) ClipToGamut

func (c SRGB) ClipToGamut() SRGB

ClipToGamut clamps each channel of the gamma-encoded sRGB color to [0,1]. Useful to avoid invalid values when converting or interpolating.

func (SRGB) HSL

func (c SRGB) HSL() HSL

HSL converts gamma-encoded sRGB to HSL (all in [0,1] except H in degrees).

func (SRGB) HSV

func (c SRGB) HSV() HSV

HSV converts gamma-encoded sRGB to HSV (all in [0,1] except H in degrees).

func (SRGB) InGamut

func (c SRGB) InGamut() bool

InGamut reports whether the gamma-encoded sRGB color lies inside the sRGB gamut. Returns true if all channels are in [0,1], false otherwise.

func (SRGB) LSRGB

func (c SRGB) LSRGB() LSRGB

func (SRGB) Lerp

func (from SRGB) Lerp(to SRGB, v float32) SRGB

func (SRGB) RGBA

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

Directories

Path Synopsis
examples
lerp command

Jump to

Keyboard shortcuts

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