color

package
v0.0.4-alpha Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2025 License: Apache-2.0 Imports: 8 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// Threshold for linear vs. nonlinear transition. [Reference]
	//
	// [Reference]: http://www.brucelindbloom.com/index.html?LContinuity.html
	LabFuncE float64 = 216.0 / 24389.0
	// Constant used for linear approximation. [Reference]
	//
	// [Reference]: http://www.brucelindbloom.com/index.html?LContinuity.html
	LabFuncK float64 = 24389.0 / 27.0
)
View Source
const Brightest = uint8(0xFF) // 255

Brightest is the max value of uint8 color

Variables

View Source
var (
	Cat16Matrix = num.NewMatrix3(
		0.401288, 0.650173, -0.051461,
		-0.250268, 1.204414, 0.045854,
		-0.002079, 0.048952, 0.953127,
	)

	Cat16InvMatrix = num.NewMatrix3(
		1.86206786, -1.01125463, 0.14918678,
		0.38752654, 0.62144744, -0.00897399,
		-0.0158415, -0.03412294, 1.04996444,
	)
)
View Source
var (
	SRGB_TO_XYZ = num.NewMatrix3(
		0.41233895, 0.35762064, 0.18051042,
		0.2126, 0.7152, 0.0722,
		0.01932141, 0.11916382, 0.95034478,
	)

	XYZ_TO_SRGB = num.NewMatrix3(
		3.2413774792388685, -1.5376652402851851, -0.49885366846268053,
		-0.9691452513005321, 1.8758853451067872, 0.04156585616912061,
		0.05562093689691305, -0.20395524564742123, 1.0571799111220335,
	)

	// The white most point of XYZ to sRGB color space
	WhitePointD65 = num.NewVector3(95.047, 100.0, 108.883)

	CriticalPlanes = []float64{}/* 255 elements not displayed */

)
View Source
var DefaultEnviroment = NewEnvironment((200/math.Pi)*YFromLstar(50)/100, 50, 2, false)

DefaultEnviroment returns the default sRGB-like viewing conditions.

View Source
var LinrgbFromScaledDiscount = num.NewMatrix3(
	1373.2198709594231, -1100.4251190754821, -7.278681089101213,
	-271.815969077903, 559.6580465940733, -32.46047482791194,
	1.9622899599665666, -57.173814538844006, 308.7233197812385,
)
View Source
var OkLabMatrix1 = num.NewMatrix3(
	0.8189330101, 0.3618667424, -0.1288597137,
	0.0329845436, 0.9293118715, 0.0361456387,
	0.0482003018, 0.2643662691, 0.6338517070,
)
View Source
var OkLabMatrix1Inv = num.NewMatrix3(
	1.2270138511, -0.5577999807, 0.2812561490,
	-0.0405801784, 1.1122568696, -0.0716766787,
	-0.0763812845, -0.4214819784, 1.5861632204,
)
View Source
var OkLabMatrix2 = num.NewMatrix3(
	0.2104542553, 0.7936177850, -0.0040720468,
	1.9779984951, -2.4285922050, 0.4505937099,
	0.0259040371, 0.7827717662, -0.8086757660,
)
View Source
var OkLabMatrix2Inv = num.NewMatrix3(
	0.9999999985, 0.3963377922, 0.2158037581,
	1.0000000089, -0.1055613423, -0.0638541748,
	1.0000000547, -0.0894841821, -1.2914855379,
)
View Source
var ScaledDiscountFromLinRGB = num.NewMatrix3(
	0.001200833568784504, 0.002389694492170889, 0.0002795742885861124,
	0.0005891086651375999, 0.0029785502573438758, 0.0003270666104008398,
	0.00010146692491640572, 0.0005364214359186694, 0.0032979401770712076,
)
View Source
var YFromLinRGB = num.NewVector3(0.2126, 0.7152, 0.0722)

Functions

func Delinearized

func Delinearized(component float64) uint8

Delinearized takes component (float64) that represents linear R/G/B channel. Component should be 0.0 < component < 1.0. Returns uint8 (0 <= n <= 255) representation of color component.

func Delinearized3

func Delinearized3(x, y, z float64) (uint8, uint8, uint8)

Delinearized3 is like Delinearized but takes 3 input and returns 3 output.

func IsBlue

func IsBlue(hue float64) bool

IsBlue determines if a hue is in the blue range.

func IsCyan

func IsCyan(hue float64) bool

IsCyan determines if a hue is in the cyan range.

func IsYellow

func IsYellow(hue float64) bool

IsYellow determines if a hue is in the yellow range.

func LabFunc

func LabFunc(t float64) float64

LabFunc is part of the conversion from XYZ to Lab color space. It applies a nonlinear transformation that approximates human vision perception.

func LabInvFunc

func LabInvFunc(ft float64) float64

LabInvFunc is the inverse of LabFunc, used when converting from Lab to XYZ. It reverses the nonlinear transformation.

func Linearized

func Linearized(component uint8) float64

Linearized takes component (uint8) that represents R/G/B channel. Returns 0.0 <= output <= 1.0, color channel converted to linear RGB space

func Linearized3

func Linearized3(x, y, z uint8) (float64, float64, float64)

Linearized3 is like Linearized but takes 3 input and returns 3 output.

func LstarFromY

func LstarFromY(y float64) float64

LstarFromY converts Y (relative luminance) in the XYZ color space to L* (perceptual luminance) in the CIELAB color space.

Both Y and L* represent luminance, but Y is linear and L* is perceptually uniform.

y is the Y value in the XYZ color space. It returns the corresponding L* value in the CIELAB color space.

func YFromLstar

func YFromLstar(lstar float64) float64

YFromLstar converts an L* (perceptual luminance) value from the CIELAB color space to Y (relative luminance) in the XYZ color space.

Both L* and Y represent luminance, but L* is perceptually uniform and Y is linear.

lstar is the L* value in the CIELAB color space. It returns the corresponding Y value in the XYZ color space.

Types

type ARGB

type ARGB uint32

ARGB is an ARGB color packed into a uint32.

func ARGBFromHex

func ARGBFromHex(hex string) (ARGB, error)

ARGBFromHex parses a hex color string and returns a Color. Supports formats: #RGB, #RGBA, #RRGGBB, #RRGGBBAA

func ARGBFromHexMust

func ARGBFromHexMust(hex string) ARGB

ARGBFromHexMust parses a hex color string and returns a Color. Supports formats: #RGB, #RGBA, #RRGGBB, #RRGGBBAA

func ARGBFromInterface

func ARGBFromInterface(color color.Color) ARGB

ARGBFromInterface

func ARGBFromLinRGB

func ARGBFromLinRGB(r, g, b float64) ARGB

FromARGB creates a ARGB from individual 8-bit red, green, and blue components.

func ARGBFromLstar

func ARGBFromLstar(lstar float64) ARGB

Converts an L* value to an ARGB representation. lstar is L* in L*a*b*. returns ARGB representation of grayscale color with lightness matching L*

func ARGBFromRGB

func ARGBFromRGB(r, g, b uint8) ARGB

FromARGB creates a ARGB from individual 8-bit red, green, and blue components.

func ARGBFromXYZ

func ARGBFromXYZ(x, y, z float64) ARGB

FromARGB creates a ARGB from xyz color space cordinates.

func NewARGB

func NewARGB(a, r, g, b uint8) ARGB

NewARGB creates a ARGB from individual 8-bit alpha, red, green, and blue components.

func (ARGB) Alpha

func (c ARGB) Alpha() uint8

Alpha returns the 8-bit alpha component of the color.

func (ARGB) AnsiBg

func (c ARGB) AnsiBg(text string) string

AnsiBg wraps the given text with the ANSI escape sequence for the background color.

func (ARGB) AnsiFg

func (c ARGB) AnsiFg(text string) string

AnsiFg wraps the given text with the ANSI escape sequence for the foreground color.

func (ARGB) Blue

func (c ARGB) Blue() uint8

Blue returns the 8-bit blue component of the color.

func (ARGB) Green

func (c ARGB) Green() uint8

Green returns the 8-bit green component of the color.

func (ARGB) HexARGB

func (c ARGB) HexARGB() string

HexARGB return #AARRGGBB represetation of the color

func (ARGB) HexRGB

func (c ARGB) HexRGB() string

HexARGB return #RRGGBB represetation of the color

func (ARGB) HexRGBA

func (c ARGB) HexRGBA() string

HexRGBA return #RRGGBBAA represetation of the color

func (ARGB) LStar

func (c ARGB) LStar() float64

Lstart

func (ARGB) MarshalText

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

TextMarshaler

func (ARGB) RGBA

func (c ARGB) RGBA() (uint32, uint32, uint32, uint32)

RGBA implements the color.Color interface. It returns r, g, b, a values in the 0-65535 range.

func (ARGB) Red

func (c ARGB) Red() uint8

Red returns the 8-bit red component of the color.

func (ARGB) String

func (c ARGB) String() string

func (ARGB) ToARGB

func (c ARGB) ToARGB() ARGB

ToLab convert Color to LabColor

func (ARGB) ToCam

func (c ARGB) ToCam() *Cam16

ToCam16 convert ARGB Color to Cam16

func (ARGB) ToHct

func (c ARGB) ToHct() Hct

ToHct convert ARGB Color to Hct

func (ARGB) ToLab

func (c ARGB) ToLab() Lab

ToLab convert Color to LabColor

func (ARGB) ToXYZ

func (c ARGB) ToXYZ() XYZ

ToXYZ return XYZ color version for c.

func (*ARGB) UnmarshalText

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

TextUnmarshaler

func (ARGB) Values

func (c ARGB) Values() (uint8, uint8, uint8, uint8)

type Cam16

type Cam16 struct {
	// Hue represents the hue of the color
	Hue float64
	// Chroma represents the colorfulness or color intensity, similar to
	// saturation in HSL but more perceptually accurate
	Chroma float64
	// J represents the lightness of the color
	J float64
	// Q represents the brightness, which is the ratio of lightness to
	// the white point's lightness
	Q float64
	// M represents the colorfulness of the color
	M float64
	// S represents the saturation, which is the ratio of chroma to
	// the white point's chroma
	S float64
	// Jstar represents the CAM16-UCS J coordinate
	Jstar float64
	// Astar represents the CAM16-UCS a coordinate
	Astar float64
	// Bstar represents the CAM16-UCS b coordinate
	Bstar float64
}

Cam16 represents the CAM16 color model, which includes various dimensions for color representation. It can be constructed using any combination of three of the following dimensions: j or q, c, m, or s, and hue. It can also be constructed using the CAM16-UCS J, a, and b coordinates.

func Cam16FromJch

func Cam16FromJch(j, c, h float64) *Cam16

Cam16FromJch constructs a Cam16 color from J (lightness), C (chroma), and H (hue angle in degrees), using DefaultViewingConditions viewing conditions.

This is used when synthesizing a CAM16 color from HCT values or performing color space conversions into perceptual models.

func Cam16FromJchInEnv

func Cam16FromJchInEnv(j, c, h float64, env *Environmnet) *Cam16

Cam16FromJchInEnv constructs a Cam16 color from J (lightness), C (chroma), and H (hue angle in degrees), using the given viewing conditions.

This is used when synthesizing a CAM16 color from HCT values or performing color space conversions into perceptual models.

func Cam16FromUcs

func Cam16FromUcs(jstar, astar, bstar float64) *Cam16

func Cam16FromUcsInEnv

func Cam16FromUcsInEnv(jstar, astar, bstar float64, env *Environmnet) *Cam16

func Cam16FromXyzInEnv

func Cam16FromXyzInEnv(xyz XYZ, env *Environmnet) *Cam16

Cam16FromColorInEnv create a Cam16 color In specific ViewingConditions

func NewCam16

func NewCam16(hue, chroma, j, q, m, s, jstar, astar, bstar float64) *Cam16

func (Cam16) Distance

func (c Cam16) Distance(other Cam16) float64

Distance returns distance between to Cam16 color

func (*Cam16) RGBA

func (c *Cam16) RGBA() (uint32, uint32, uint32, uint32)

func (*Cam16) ToARGB

func (c *Cam16) ToARGB() ARGB

func (*Cam16) ToCam

func (c *Cam16) ToCam() *Cam16

func (*Cam16) ToHct

func (c *Cam16) ToHct() Hct

func (*Cam16) ToLab

func (c *Cam16) ToLab() Lab

func (*Cam16) ToXYZ

func (c *Cam16) ToXYZ() XYZ

func (*Cam16) Viewed

func (c *Cam16) Viewed(vc *Environmnet) XYZ

Viewed converts a CAM16 color to an ARGB integer based on the given viewing conditions

type Environmnet

type Environmnet struct {
	// N is the relative luminance of the background relative to the reference white.
	N float64
	// Aw is the achromatic response to the white point.
	Aw float64
	// Nbb is the brightness induction factor (background).
	Nbb float64
	// Ncb is the chromatic induction factor (background).
	Ncb float64
	// C is the surround exponential non-linearity factor.
	C float64
	// Nc is the chromatic induction factor (surround).
	Nc float64
	// RgbD is the degree of adaptation for each RGB channel after discounting the illuminant.
	RgbD num.Vector3
	// Fl is the luminance-level adaptation factor (nonlinear response).
	Fl float64
	// FlRoot is the fourth root of Fl, used in CAM16 computations.
	FlRoot float64
	// Z is a base exponential factor used in the CAM16 J calculation.
	Z float64
}

Environmnet encapsulates all constants needed for CAM16 color conversions. These are intermediate values derived from the viewing environment and are used throughout the CAM16 model to compute perceptual color attributes.

func NewEnvironment

func NewEnvironment(
	adaptingLuminance float64,
	backgroundLstar float64,
	surround float64,
	discountingIlluminant bool,
) Environmnet

NewEnvironment creates a ViewingConditions instance with the specified parameters.

type Hct

type Hct struct {
	Hue    float64
	Chroma float64
	Tone   float64
}

Hct represents a color in the HCT color space (Hue, Chroma, Tone). HCT provides a perceptually accurate color measurement system that can also accurately render what colors will appear as in different lighting environments.

func NewHct

func NewHct(hue, chroma, tone float64) Hct

From creates an HCT color from the provided hue, chroma, and tone values.

hue: 0 <= hue < 360; invalid values are corrected. chroma: 0 <= chroma < ?; Chroma has a different maximum for any given hue and tone. tone: 0 <= tone <= 100; invalid values are corrected.

func (Hct) Hash

func (h Hct) Hash() uint64

Hash returns a uint64 hash representation of the HCT color. This is much more efficient than string-based hashing.

func (*Hct) InViewingConditions

func (h *Hct) InViewingConditions(env *Environmnet) Hct

InViewingConditions translates a color into different ViewingConditions.

Colors change appearance. They look different with lights on versus off, the same color, as in hex code, on white looks different when on black. This is called color relativity, most famously explicated by Josef Albers in Interaction of Color.

In color science, color appearance models can account for this and calculate the appearance of a color in different settings. HCT is based on CAM16, a color appearance model, and uses it to make these calculations.

See ViewingConditions.Make for parameters affecting color appearance.

func (Hct) IsBlue

func (h Hct) IsBlue() bool

IsBlue determines if a hue is in the blue range.

func (Hct) IsCyan

func (h Hct) IsCyan() bool

IsCyan determines if a hue is in the cyan range.

func (Hct) IsYellow

func (h Hct) IsYellow() bool

IsYellow determines if a hue is in the yellow range.

func (Hct) RGBA

func (h Hct) RGBA() (uint32, uint32, uint32, uint32)

ToInt returns the ARGB representation of this color.

func (Hct) String

func (h Hct) String() string

String returns a string representation of the HCT color.

func (Hct) ToARGB

func (h Hct) ToARGB() ARGB

ToInt returns the ARGB representation of this color.

func (Hct) ToCam

func (h Hct) ToCam() *Cam16

HctFromColor creates an HCT color from the provided ARGB integer.

func (Hct) ToHct

func (h Hct) ToHct() Hct

HctFromColor creates an HCT color from the provided ARGB integer.

func (Hct) ToLab

func (h Hct) ToLab() Lab

ToInt returns the ARGB representation of this color.

func (Hct) ToXYZ

func (h Hct) ToXYZ() XYZ

ToInt returns the ARGB representation of this color.

type Lab

type Lab struct {
	L, A, B float64
}

func NewLab

func NewLab(l, a, b float64) Lab

func (Lab) DistanceSquared

func (a Lab) DistanceSquared(b Lab) float64

DistanceSquared returns square of distance between two color

func (Lab) LStar

func (c Lab) LStar() float64

LStar returns the L* value of L*a*b* (LabColor)

func (Lab) LuminanceY

func (c Lab) LuminanceY() float64

LStar returns the Y value for XYZColor

func (Lab) RGBA

func (c Lab) RGBA() (uint32, uint32, uint32, uint32)

func (Lab) ToARGB

func (c Lab) ToARGB() ARGB

ToARGB returns Color (ARGB) from LabColor

func (Lab) ToCam

func (c Lab) ToCam() *Cam16

func (Lab) ToHct

func (c Lab) ToHct() Hct

func (Lab) ToLab

func (c Lab) ToLab() Lab

func (Lab) ToXYZ

func (c Lab) ToXYZ() XYZ

ToXYZ return XYZColor from LabColor

func (Lab) Values

func (c Lab) Values() (float64, float64, float64)

Values returns L, a, b values of LABColor color

type OkLab

type OkLab struct {
	L, A, B float64
}

func NewOkLab

func NewOkLab(l, a, b float64) OkLab

func OkLabFromXYZ

func OkLabFromXYZ(x, y, z float64) OkLab

func (OkLab) ToXYZ

func (ok OkLab) ToXYZ() XYZ

func (OkLab) Values

func (c OkLab) Values() (float64, float64, float64)

Values returns L, a, b values of OkLab Model

type XYZ

type XYZ struct {
	X, Y, Z float64
}

XYZ is color in XYZ color space

func NewXYZ

func NewXYZ(x, y, z float64) XYZ

func (XYZ) LStar

func (c XYZ) LStar() float64

LStar returns the L* value of L*a*b* (LabColor)

func (XYZ) Luminance

func (c XYZ) Luminance() float64

Luminance returns the Y value of XYZColor

func (XYZ) RGBA

func (c XYZ) RGBA() (uint32, uint32, uint32, uint32)

RGBA implements the color.Color interface. It returns r, g, b, a values in the 0-65535 range.

func (XYZ) ToARGB

func (c XYZ) ToARGB() ARGB

func (XYZ) ToCam

func (c XYZ) ToCam() *Cam16

func (XYZ) ToHct

func (c XYZ) ToHct() Hct

func (XYZ) ToLab

func (c XYZ) ToLab() Lab

func (XYZ) ToXYZ

func (c XYZ) ToXYZ() XYZ

func (XYZ) Values

func (c XYZ) Values() (float64, float64, float64)

Values returns x, y, z values of XYZColor

Jump to

Keyboard shortcuts

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