hct

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package hct is a Go port of Google's HCT Material color utility.

https://github.com/material-foundation/material-color-utilities/tree/main/java/hct

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

View Source
var CAM16RGB_TO_XYZ = [][]float64{
	{1.8620678, -1.0112547, 0.14918678},
	{0.38752654, 0.62144744, -0.00897398},
	{-0.01584150, -0.03412294, 1.0499644},
}

CAM16RGB_TO_XYZ transforms 'cone'/'RGB' responses in CAM16 to XYZ color space coordinates.

View Source
var CRITICAL_PLANES = []float64{}/* 255 elements not displayed */
View Source
var DEFAULT_VIEWING_CONDITIONS = DefaultWithBackgroundLstar(50.0)

sRGB-like viewing conditions.

View Source
var LINRGB_FROM_SCALED_DISCOUNT = [][]float64{
	{1373.2198709594231, -1100.4251190754821, -7.278681089101213},
	{-271.815969077903, 559.6580465940733, -32.46047482791194},
	{1.9622899599665666, -57.173814538844006, 308.7233197812385},
}
View Source
var SCALED_DISCOUNT_FROM_LINRGB = [][]float64{
	{0.001200833568784504, 0.002389694492170889, 0.0002795742885861124},
	{0.0005891086651375999, 0.0029785502573438758, 0.0003270666104008398},
	{0.00010146692491640572, 0.0005364214359186694, 0.0032979401770712076},
}
View Source
var SRGB_TO_XYZ = [][]float64{
	{0.41233895, 0.35762064, 0.18051042},
	{0.2126, 0.7152, 0.0722},
	{0.01932141, 0.11916382, 0.95034478},
}
View Source
var WHITE_POINT_D65 = []float64{95.047, 100.0, 108.883}
View Source
var XYZ_TO_CAM16RGB = [][]float64{
	{0.401288, 0.650173, -0.051461},
	{-0.250268, 1.204414, 0.045854},
	{-0.002079, 0.048952, 0.953127},
}

XYZ_TO_CAM16RGB transforms XYZ color space coordinates to 'cone'/'RGB' responses in CAM16.

View Source
var XYZ_TO_SRGB = [][]float64{
	{3.2413774792388685, -1.5376652402851851, -0.49885366846268053},
	{-0.9691452513005321, 1.8758853451067872, 0.04156585616912061},
	{0.05562093689691305, -0.20395524564742123, 1.0571799111220335},
}
View Source
var Y_FROM_LINRGB = []float64{0.2126, 0.7152, 0.0722}

Functions

func AreInCyclicOrder

func AreInCyclicOrder(a, b, c float64) bool

func ArgbFromLinrgb

func ArgbFromLinrgb(linrgb []float64) int

ArgbFromLinrgb converts a color from linear RGB components to ARGB format.

func ArgbFromLstar

func ArgbFromLstar(lstar float64) int

ArgbFromLstar converts an L* value to an ARGB representation.

func ArgbFromRgb

func ArgbFromRgb(red, green, blue int) int

ArgbFromRgb Converts a color from RGB components to ARGB format.

func ArgbFromXyz

func ArgbFromXyz(x, y, z float64) int

ArgbFromXyz converts a color from ARGB to XYZ.

func BisectToLimit

func BisectToLimit(y float64, targetHue float64) []float64

func BisectToSegment

func BisectToSegment(y float64, targetHue float64) [][]float64

BisectToSegment finds the segment containing the desired color.

func BlueFromArgb

func BlueFromArgb(argb int) int

BlueFromArgb returns the blue component of a color in ARGB format. */

func ChromaticAdaptation

func ChromaticAdaptation(component float64) float64

func ClampDouble

func ClampDouble(min, max, input float64) float64

ClampDouble clamps an floating point number between two floating-point numbers.

func ClampInt

func ClampInt(min, max, input int) int

ClampDouble clamps an integer between two integers.

func CriticalPlaneAbove

func CriticalPlaneAbove(x float64) int

func CriticalPlaneBelow

func CriticalPlaneBelow(x float64) int

func Delinearized

func Delinearized(rgbComponent float64) int

Delinearized delinearizes an RGB component.

func FindResultByJ

func FindResultByJ(hueRadians float64, chroma float64, y float64) int

FindResultByJ finds a color with the given hue, chroma, and Y.

func GreenFromArgb

func GreenFromArgb(argb int) int

GreenFromArgb returns the green component of a color in ARGB format. */

func HueOf

func HueOf(linrgb []float64) float64

HueOf returns the hue of a linear RGB color in CAM16.

func Intercept

func Intercept(source, mid, target float64) float64

Intercept solves the lerp equation

func InverseChromaticAdaptation

func InverseChromaticAdaptation(adapted float64) float64

func IsBounded

func IsBounded(x float64) bool

func LabF

func LabF(t float64) float64

func LabInvf

func LabInvf(ft float64) float64

func Lerp

func Lerp(start, stop, amount float64) float64

Lerp is the linear interpolation function. It returns start if amount = 0 and stop if amount = 1.

func LerpPoint

func LerpPoint(source []float64, t float64, target []float64) []float64

func Linearized

func Linearized(rgbComponent int) float64

Linearized linearizes an RGB component.

func LstarFromArgb

func LstarFromArgb(argb int) float64

LstarFromArgb computes the L* value of a color in ARGB representation.

func LstarFromY

func LstarFromY(y float64) float64

LstarFromY converts a Y value to an L* value. L* in L*a*b* and Y in XYZ measure the same quantity, luminance. L* measures perceptual luminance, a linear scale. Y in XYZ measures relative luminance, a logarithmic scale.

func MatrixMultiply

func MatrixMultiply(row []float64, matrix [][]float64) []float64

MatrixMultiply multiplies a 1x3 row vector with a 3x3 matrix.

func Midpoint

func Midpoint(a []float64, b []float64) []float64

func NthVertex

func NthVertex(y float64, n int) []float64

NthVertex returns the nth possible vertex of the polygonal intersection.

func RedFromArgb

func RedFromArgb(argb int) int

RedFromArgb returns the red component of a color in ARGB format.

func SanitizeDegreesDouble

func SanitizeDegreesDouble(degrees float64) float64

SanitizeDegreesDouble sanitizes a degree measure as a floating-point number.

func SanitizeRadians

func SanitizeRadians(angle float64) float64

SanitizeRadians sanitizes a small enough angle in radians.

func SetCoordinate

func SetCoordinate(source []float64, coordinate float64, target []float64, axis int) []float64

SetCoordinate intersects a segment with a plane.

func Signum

func Signum(num float64) float64

Signum return 1 if num > 0, -1 if num < 0, and 0 if num = 0

func SolveToInt

func SolveToInt(hueDegrees float64, chroma float64, lstar float64) int

SolveToInt finds an sRGB color with the given hue, chroma, and L*, if possible.

func ToDegrees

func ToDegrees(radians float64) float64

ToDegrees converts an angle measured in radians to an approximately equivalent angle measured in degrees.

func ToRadians

func ToRadians(degrees float64) float64

ToRadians converts an angle measured in degrees to an approximately equivalent angle measured in radians.

func TrueDelinearized

func TrueDelinearized(rgbComponent float64) float64

TrueDelinearized delinearizes an RGB component, returning a floating-point number.

func XyzFromArgb

func XyzFromArgb(argb int) []float64

XyzFromArgb converts a color from XYZ to ARGB

func YFromLstar

func YFromLstar(lstar float64) float64

Converts an L* value to a Y value. L* in L*a*b* and Y in XYZ measure the same quantity, luminance. L* measures perceptual luminance, a linear scale. Y in XYZ measures relative luminance, a logarithmic scale.

Types

type Cam16

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

*

  • Cam16, a color appearance model. Colors are not just defined by their hex code, but rather, a hex
  • code and viewing conditions. *
  • <p>CAM16 instances also have coordinates in the CAM16-UCS space, called J*, a*, b*, or jstar,
  • astar, bstar in code. CAM16-UCS is included in the CAM16 specification, and should be used when
  • measuring distances between colors. *
  • <p>In traditional color spaces, a color can be identified solely by the observer's measurement of
  • the color. Color appearance models such as CAM16 also use information about the environment where
  • the color was observed, known as the viewing conditions. *
  • <p>For example, white under the traditional assumption of a midday sun white point is accurately
  • measured as a slightly chromatic blue by CAM16. (roughly, hue 203, chroma 3, lightness 100)

func FromInt

func FromInt(argb int) *Cam16

FromInt creates a CAM16 color from an ARGB representation of a color, assuming the color was viewed in default viewing conditions.

func FromIntInViewingConditions

func FromIntInViewingConditions(argb int, viewingConditions *ViewingConditions) *Cam16

FromIntInViewingConditions creates a CAM16 color from an ARGB representation of a color, defined viewing conditions.

func FromJch

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

FromJch creates a CAM16 color from CAM16 lighness, chroma and hue, assuming the color was viewed in default viewing conditions.

func FromJchInViewingConditions

func FromJchInViewingConditions(j, c, h float64, viewingConditions *ViewingConditions) *Cam16

FromJchInViewingConditions creates a CAM16 color from CAM16 lighness, chroma and hue, defined viewing conditions.

func FromUcs

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

FromUcs creates a CAM16 color from CAM16-UCS coordinates

func FromUcsInViewingConditions

func FromUcsInViewingConditions(jstar, astar, bstar float64, viewingConditions *ViewingConditions) *Cam16

FromUcsInViewingConditions creates a CAM16 color from CAM16-UCS coordinates in defined viewing conditions.

func FromXyzInViewingConditions

func FromXyzInViewingConditions(x, y, z float64, viewingConditions *ViewingConditions) *Cam16

func SolveToCam

func SolveToCam(hueDegrees, chroma, lstar float64) *Cam16

SolveToCam finds an sRGB color with the given hue, chroma, and L*, if possible.

func (*Cam16) Astar

func (c *Cam16) Astar() float64

Astar is a* coordinate in CAM16-UCS.

func (*Cam16) Bstar

func (c *Cam16) Bstar() float64

Bstar is b* coordinate in CAM16-UCS.

func (*Cam16) Chroma

func (c *Cam16) Chroma() float64

Chroma in CAM16.

func (*Cam16) Distance

func (c *Cam16) Distance(other *Cam16) float64

Distance measures the distance between colors. CAM16 instances also have coordinates in the CAM16-UCS space, called J*, a*, b*, or jstar, astar, bstar in code. CAM16-UCS is included in the CAM16 specification, and is used to measure distances between colors.

func (*Cam16) Hue

func (c *Cam16) Hue() float64

Hue in CAM16.

func (*Cam16) J

func (c *Cam16) J() float64

J is lightness in CAM16.

func (*Cam16) Jstar

func (c *Cam16) Jstar() float64

Jstar is lightness coordinate in CAM16-UCS.

func (*Cam16) M

func (c *Cam16) M() float64

M is colorfulness in CAM16. Prefer chroma, colorfulness is an absolute quantity. For example, a yellow toy car is much more colorful outside than inside, but it has the same chroma in both environments.

func (*Cam16) Q

func (c *Cam16) Q() float64

Q is brightness in CAM16. Prefer lightness, brightness is an absolute quantity. For example, a sheet of white paper is much brighter viewed in sunlight than in indoor light, but it is the lightest object under any lighting.

func (*Cam16) S

func (c *Cam16) S() float64

S is saturation in CAM16. Colorfulness in proportion to brightness. Prefer chroma, saturation measures colorfulness relative to the color's own brightness, where chroma is colorfulness relative to white.

func (*Cam16) ToInt

func (c *Cam16) ToInt() int

ToInt returns the ARGB representation of the color. Assumes the color was viewed in default viewing conditions, which are near-identical to the default viewing conditions for sRGB.

func (*Cam16) Viewed

func (c *Cam16) Viewed(viewingConditions *ViewingConditions) int

func (*Cam16) XyzInViewingConditions

func (c *Cam16) XyzInViewingConditions(viewingConditions *ViewingConditions) []float64

type Hct

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

Hct is hue, chroma, and tone. A color system that provides a perceptually accurate color measurement system that can also accurately render what colors will appear as in different lighting environments.

func From

func From(hue, chroma, tone float64) *Hct

func FromInteger

func FromInteger(argb int) *Hct

FromInteger creates an HCT color from a color.

func (*Hct) Chroma

func (hct *Hct) Chroma() float64

Chroma returns the chroma component of the color. 0 <= chroma < ?

func (*Hct) Hue

func (hct *Hct) Hue() float64

Hue returns the hue component of the color. 0 <= hue < 360

func (*Hct) InViewingConditions

func (hct *Hct) InViewingConditions(vc *ViewingConditions) *Hct

Translate 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) SetChroma

func (hct *Hct) SetChroma(newChroma float64)

SetChroma sets the chroma of this color. Chroma may decrease because chroma has a different maximum for any given hue and tone. 0 <= newChroma < ?

func (*Hct) SetHue

func (hct *Hct) SetHue(newHue float64)

SetHue sets the hue of this color. Chroma may decrease because chroma has a different maximum for any given hue and tone. 0 <= newHue < 360

func (*Hct) SetTone

func (hct *Hct) SetTone(newTone float64)

SetTone sets the tone of this color. Chroma may decrease because chroma has a different maximum for any given hue and tone. 0 <= newTone <= 100

func (*Hct) ToInt

func (hct *Hct) ToInt() int

ToInt returns the ARGB representation of the color.

func (*Hct) Tone

func (hct *Hct) Tone() float64

Tone returns the tone component of the color. 0 <= tone <= 100

type ViewingConditions

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

In traditional color spaces, a color can be identified solely by the observer's measurement of the color. Color appearance models such as CAM16 also use information about the environment where the color was observed, known as the viewing conditions.

For example, white under the traditional assumption of a midday sun white point is accurately measured as a slightly chromatic blue by CAM16. (roughly, hue 203, chroma 3, lightness 100)

This class caches intermediate values of the CAM16 conversion process that depend only on viewing conditions, enabling speed ups.

func DefaultWithBackgroundLstar

func DefaultWithBackgroundLstar(lstar float64) *ViewingConditions

Create sRGB-like viewing conditions with a custom background lstar. Default viewing conditions have a lstar of 50, midgray.

func Make

func Make(whitePoint []float64, adaptingLuminance, backgroundLstar, surround float64, discountingIlluminant bool) *ViewingConditions

Create ViewingConditions from a simple, physically relevant, set of parameters.

White point, measured in the XYZ color space. default = D65, or sunny day afternoon.

The luminance of the adapting field. Informally, how bright it is in the room where the color is viewed. Can be calculated from lux by multiplying lux by 0.0586. default = 11.72, or 200 lux.

The lightness of the area surrounding the color. measured by L* in L*a*b*. default = 50.0

A general description of the lighting surrounding the color. 0 is pitch dark, like watching a movie in a theater. 1.0 is a dimly light room, like watching TV at home at night. 2.0 means there is no difference between the lighting on the color and around it. default = 2.0

Whether the eye accounts for the tint of the ambient lighting, such as knowing an apple is still red in green light. default = false, the eye does not perform this process on self-luminous objects like displays.

func (*ViewingConditions) Aw

func (vc *ViewingConditions) Aw() float64

func (*ViewingConditions) C

func (vc *ViewingConditions) C() float64

func (*ViewingConditions) Fl

func (vc *ViewingConditions) Fl() float64

func (*ViewingConditions) FlRoot

func (vc *ViewingConditions) FlRoot() float64

func (*ViewingConditions) N

func (vc *ViewingConditions) N() float64

func (*ViewingConditions) Nbb

func (vc *ViewingConditions) Nbb() float64

func (*ViewingConditions) Nc

func (vc *ViewingConditions) Nc() float64

func (*ViewingConditions) Ncb

func (vc *ViewingConditions) Ncb() float64

func (*ViewingConditions) RgbD

func (vc *ViewingConditions) RgbD() []float64

func (*ViewingConditions) Z

func (vc *ViewingConditions) Z() float64

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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