hct

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2024 License: BSD-3-Clause Imports: 6 Imported by: 0

README

hct: Hue, Chroma, and Tone

A color system built using CAM16 hue and chroma, and L* (lightness) from the Lab* color space, providing a perceptually accurate color measurement system that can also accurately render what colors will appear as in different lighting environments.

Using L* creates a link between the color system, contrast, and thus accessibility. Contrast ratio depends on relative luminance, or Y in the XYZ color space. L*, or perceptual luminance can be calculated from Y.

Unlike Y, L* is linear to human perception, allowing trivial creation of accurate color tones.

Unlike contrast ratio, measuring contrast in L* is linear, and simple to calculate. A difference of 40 in HCT tone guarantees a contrast ratio >= 3.0, and a difference of 50 guarantees a contrast ratio >= 4.5.

HCT Colorspace

hct colorspace

Documentation

Index

Constants

View Source
const (
	// ContrastAA is the contrast ratio required by WCAG AA for body text
	ContrastAA float32 = 4.5

	// ContrastLargeAA is the contrast ratio required by WCAG AA for large text
	// (at least 120-150% larger than the body text)
	ContrastLargeAA float32 = 3

	// ContrastGraphicsAA is the contrast ratio required by WCAG AA for graphical objects
	// and active user interface components like graphs, icons, and form input borders
	ContrastGraphicsAA float32 = 3

	// ContrastAAA is the contrast ratio required by WCAG AAA for body text
	ContrastAAA float32 = 7

	// ContrastLargeAAA is the contrast ratio required by WCAG AAA for large text
	// (at least 120-150% larger than the body text)
	ContrastLargeAAA float32 = 4.5
)

Variables

View Source
var Model = color.ModelFunc(model)

Model is the standard color.Model that converts colors to HCT.

Functions

func BisectToLimit

func BisectToLimit(y, target_hue float32) math32.Vector3

Finds a color with the given Y and hue on the boundary of the cube. @param y The Y value of the color. @param target_hue The hue of the color. @return The desired color, in linear RGB coordinates.

func BisectToSegment

func BisectToSegment(y, target_hue float32) [2]math32.Vector3

Finds the segment containing the desired color. @param y The Y value of the color. @param target_hue The hue of the color. @return A list of two sets of linear RGB coordinates, each corresponding to an endpoint of the segment containing the desired color.

func Blend

func Blend(pct float32, x, y color.Color) color.RGBA

Blend returns a color that is the given percent blend between the first and second color; 10 = 10% of the first and 90% of the second, etc; blending is done directly on non-premultiplied HCT values, and a correctly premultiplied color is returned.

func ContrastColor

func ContrastColor(c color.Color, ratio float32) color.RGBA

ContrastColor returns the color that will ensure that the given contrast ratio between the given color and the resulting color is met. If the given ratio can not be achieved with the given color, it returns the color that would result in the highest contrast ratio. The ratio must be between 1 and 21. If the tone of the given color is greater than 50, it tries darker tones first, and otherwise it tries lighter tones first.

func ContrastColorTry

func ContrastColorTry(c color.Color, ratio float32) (color.RGBA, bool)

ContrastColorTry returns the color that will ensure that the given contrast ratio between the given color and the resulting color is met. It returns color.RGBA{}, false if the given ratio can not be achieved with the given color. The ratio must be between 1 and 21. If the tone of the given color is greater than 50, it tries darker tones first, and otherwise it tries lighter tones first.

func ContrastRatio

func ContrastRatio(a, b color.Color) float32

ContrastRatio returns the contrast ratio between the given two colors. The contrast ratio will be between 1 and 21.

func ContrastRatioOfYs

func ContrastRatioOfYs(a, b float32) float32

ContrastRatioOfYs returns the contrast ratio of two XYZ Y values.

func ContrastTone

func ContrastTone(tone, ratio float32) float32

ContrastTone returns the tone that will ensure that the given contrast ratio between the given tone and the resulting tone is met. If the given ratio can not be achieved with the given tone, it returns the tone that would result in the highest contrast ratio. The tone must be between 0 and 100 and the ratio must be between 1 and 21. If the given tone is greater than 50, it tries darker tones first, and otherwise it tries lighter tones first.

func ContrastToneDarker

func ContrastToneDarker(tone, ratio float32) float32

ContrastToneDarker returns a tone less than or equal to the given tone that ensures that given contrast ratio between the two tones is met. It returns 0 if the given ratio can not be achieved with the given tone. The tone must be between 0 and 100 and the ratio must be between 1 and 21.

func ContrastToneDarkerTry

func ContrastToneDarkerTry(tone, ratio float32) (float32, bool)

ContrastToneDarkerTry returns a tone less than or equal to the given tone that ensures that given contrast ratio between the two tones is met. It returns -1, false if the given ratio can not be achieved with the given tone. The tone must be between 0 and 100 and the ratio must be between 1 and 21.

func ContrastToneLighter

func ContrastToneLighter(tone, ratio float32) float32

ContrastToneLighter returns a tone greater than or equal to the given tone that ensures that given contrast ratio between the two tones is met. It returns 100 if the given ratio can not be achieved with the given tone. The tone must be between 0 and 100 and the ratio must be between 1 and 21.

func ContrastToneLighterTry

func ContrastToneLighterTry(tone, ratio float32) (float32, bool)

ContrastToneLighterTry returns a tone greater than or equal to the given tone that ensures that given contrast ratio between the two tones is met. It returns -1, false if the given ratio can not be achieved with the given tone. The tone must be between 0 and 100 and the ratio must be between 1 and 21.

func ContrastToneTry

func ContrastToneTry(tone, ratio float32) (float32, bool)

ContrastToneTry returns the tone that will ensure that the given contrast ratio between the given tone and the resulting tone is met. It returns -1, false if the given ratio can not be achieved with the given tone. The tone must be between 0 and 100 and the ratio must be between 1 and 21. If the given tone is greater than 50, it tries darker tones first, and otherwise it tries lighter tones first.

func CriticalPlaneAbove

func CriticalPlaneAbove(x float32) int

func CriticalPlaneBelow

func CriticalPlaneBelow(x float32) int

func Darken

func Darken(c color.Color, amount float32) color.RGBA

Darken returns a color that is darker by the given absolute HCT tone amount (0-100, ranges enforced)

func Desaturate

func Desaturate(c color.Color, amount float32) color.RGBA

Desaturate returns a color that is less saturated by the given absolute HCT chroma amount (0-max that depends on other params but is around 150, ranges enforced)

func FindResultByJ

func FindResultByJ(hue_rad, chroma, y float32) *math32.Vector3

Finds a color with the given hue, chroma, and Y. @param hue_radians The desired hue in radians. @param chroma The desired chroma. @param y The desired Y. @return The desired color as linear sRGB values.

func GetAxis

func GetAxis(v math32.Vector3, axis int) float32

GetAxis returns value along axis 0,1,2 -- result is divided by 100 so that resulting numbers are in 0-1 range.

func Highlight

func Highlight(c color.Color, amount float32) color.RGBA

Highlight returns a color that is lighter or darker by the given absolute HCT tone amount (0-100, ranges enforced), making the color darker if it is light (tone >= 50) and lighter otherwise. It is the opposite of Samelight.

func HueOf

func HueOf(linrgb math32.Vector3) float32

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

func Intercept

func Intercept(source, mid, target float32) float32

Solves the lerp equation. @param source The starting number. @param mid The number in the middle. @param target The ending number. @return A number t such that lerp(source, target, t) = mid.

func IsBounded

func IsBounded(x float32) bool

func IsDark

func IsDark(c color.Color) bool

IsDark returns whether the given color is dark (has an HCT tone less than 50)

func IsLight

func IsLight(c color.Color) bool

IsLight returns whether the given color is light (has an HCT tone greater than or equal to 50)

func Lighten

func Lighten(c color.Color, amount float32) color.RGBA

Lighten returns a color that is lighter by the given absolute HCT tone amount (0-100, ranges enforced)

func MatMul

func MatMul(v math32.Vector3, mat [3][3]float32) math32.Vector3

func Midpoint

func Midpoint(a, b math32.Vector3) math32.Vector3

func MinHueDistance

func MinHueDistance(a, b float32) float32

MinHueDistance finds the minimum distance between two hues. A positive number means add to a to get to b. A negative number means subtract from a to get to b.

func NthVertex

func NthVertex(y float32, n int) math32.Vector3

Returns the nth possible vertex of the polygonal intersection. @param y The Y value of the plane. @param n The zero-based index of the point. 0 <= n <= 11. @return The nth possible vertex of the polygonal intersection of the y plane and the RGB cube, in linear RGB coordinates, if it exists. If this possible vertex lies outside of the cube,

[-1.0, -1.0, -1.0] is returned.

func Samelight

func Samelight(c color.Color, amount float32) color.RGBA

Samelight returns a color that is lighter or darker by the given absolute HCT tone amount (0-100, ranges enforced), making the color lighter if it is light (tone >= 50) and darker otherwise. It is the opposite of Highlight.

func Saturate

func Saturate(c color.Color, amount float32) color.RGBA

Saturate returns a color that is more saturated by the given absolute HCT chroma amount (0-max that depends on other params but is around 150, ranges enforced)

func SetCoordinate

func SetCoordinate(source, target math32.Vector3, coord float32, axis int) math32.Vector3

*

  • Intersects a segment with a plane. *
  • @param source The coordinates of point A.
  • @param coordinate The R-, G-, or B-coordinate of the plane.
  • @param target The coordinates of point B.
  • @param axis The axis the plane is perpendicular with. (0: R, 1: G, 2: B)
  • @return The intersection point of the segment AB with the plane R=coordinate,
  • G=coordinate, or B=coordinate

func SolveToRGB

func SolveToRGB(hue, chroma, tone float32) (r, g, b float32)

SolveToRGB Finds an sRGB (gamma corrected, 0-1 range) color with the given hue, chroma, and tone, if possible. if not possible to represent the target values, the hue and tone will be sufficiently close, and chroma will be maximized.

func SolveToRGBLin

func SolveToRGBLin(hue, chroma, tone float32) math32.Vector3

SolveToRGBLin Finds an sRGB linear color (represented by math32.Vector3, 0-100 range) with the given hue, chroma, and tone, if possible. if not possible to represent the target values, the hue and tone will be sufficiently close, and chroma will be maximized.

func Spin

func Spin(c color.Color, amount float32) color.RGBA

Spin returns a color that has a different hue by the given absolute HCT hue amount (0-360, ranges enforced)

func ToneContrastRatio

func ToneContrastRatio(a, b float32) float32

ToneContrastRatio returns the contrast ratio between the given two tones. The contrast ratio will be between 1 and 21, and the tones should be between 0 and 100 and will be clamped to such.

func TrueDelinearized

func TrueDelinearized(comp float32) float32

Delinearizes an RGB component, returning a floating-point number. @param rgb_component 0.0 <= rgb_component <= 100.0, represents linear R/G/B channel @return 0.0 <= output <= 255.0, color channel converted to regular RGB space

Types

type HCT

type HCT struct {

	// Hue (h) is the spectral identity of the color
	// (red, green, blue etc) in degrees (0-360)
	Hue float32 `min:"0" max:"360"`

	// Chroma (C) is the colorfulness/saturation of the color.
	// Grayscale colors have no chroma, and fully saturated ones
	// have high chroma. The maximum varies as a function of hue
	// and tone, but 150 is an upper bound.
	Chroma float32 `min:"0" max:"150"`

	// Tone is the L* component from the LAB (L*a*b*) color system,
	// which is linear in human perception of lightness.
	// It ranges from 0 to 100.
	Tone float32 `min:"0" max:"100"`

	// sRGB standard gamma-corrected 0-1 normalized RGB representation
	// of the color. Critically, components are not premultiplied by alpha.
	R, G, B, A float32
}

HCT represents a color as hue, chroma, and tone. HCT is 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. Directly setting the values of the HCT and RGB fields will have no effect on the underlying color; instead, use the Set methods (HCT.SetHue, etc). The A field (transparency) can be set directly.

func FromColor

func FromColor(c color.Color) HCT

FromColor constructs a new HCT color from a standard color.Color

func New

func New(hue, chroma, tone float32) HCT

New returns a new HCT representation for given parameters: hue = 0..360 chroma = 0..? depends on other params tone = 0..100 also computes and sets the sRGB normalized, gamma corrected R,G,B values while keeping the sRGB representation within its gamut, which may cause the chroma to decrease until it is inside the gamut.

func SRGBToHCT

func SRGBToHCT(r, g, b float32) HCT

SRGBToHCT returns an HCT from given SRGB color coordinates, under standard viewing conditions. The RGB value range is 0-1, and RGB values have gamma correction. Alpha is always 1.

func Uint32ToHCT

func Uint32ToHCT(r, g, b, a uint32) HCT

Uint32ToHCT returns an HCT from given SRGBA uint32 color coordinates, which are used for interchange among image.Color types. Uses standard viewing conditions, and RGB values already have gamma correction (i.e., they are SRGB values).

func (HCT) AsRGBA

func (h HCT) AsRGBA() color.RGBA

AsRGBA returns a standard color.RGBA type

func (HCT) RGBA

func (h HCT) RGBA() (r, g, b, a uint32)

RGBA implements the color.Color interface. Performs the premultiplication of the RGB components by alpha at this point.

func (*HCT) SetChroma

func (h *HCT) SetChroma(chroma float32) *HCT

SetChroma sets the chroma of this color (0 to max that depends on other params), while keeping the sRGB representation within its gamut, which may cause the chroma to decrease until it is inside the gamut.

func (*HCT) SetHue

func (h *HCT) SetHue(hue float32) *HCT

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

func (*HCT) SetTone

func (h *HCT) SetTone(tone float32) *HCT

SetTone sets the tone of this color (0 < tone < 100), while keeping the sRGB representation within its gamut, which may cause the chroma to decrease until it is inside the gamut.

func (*HCT) SetUint32

func (h *HCT) SetUint32(r, g, b, a uint32)

SetUint32 sets components from unsigned 32bit integers (alpha-premultiplied)

func (HCT) String

func (h HCT) String() string

func (HCT) WithChroma

func (h HCT) WithChroma(chroma float32) HCT

WithChroma is like [SetChroma] except it returns a new color instead of setting the existing one.

func (HCT) WithHue

func (h HCT) WithHue(hue float32) HCT

WithHue is like [SetHue] except it returns a new color instead of setting the existing one.

func (HCT) WithTone

func (h HCT) WithTone(tone float32) HCT

WithTone is like [SetTone] except it returns a new color instead of setting the existing one.

Jump to

Keyboard shortcuts

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