Documentation
¶
Overview ¶
Package color is a standard-library-only Go port of the color primitives that underpin the popular npm packages color-convert, tinycolor2 and chroma-js, which Express/Node web apps reach for when generating themes, badges and UI palettes. It converts between the RGB, HSL and HSV colour models and CSS hex notation, and provides the everyday manipulation helpers — lighten, darken, saturate, desaturate, mix, invert, grayscale, complement and hue rotation — together with WCAG relative-luminance and contrast-ratio calculations for accessibility checks.
Colours are 8-bit sRGB. HexToRGB accepts "#rgb", "#rrggbb", "rgb" and "rrggbb" (the leading '#' and letter case are optional) and returns an error for anything else; RGBToHex always emits lowercase "#rrggbb". The HSL and HSV hue is in degrees [0,360); saturation, lightness and value are fractions in [0,1]. Conversions round to the nearest 8-bit channel and are stable round-trips for the canonical inputs exercised by the tests.
The manipulation helpers take an amount in [0,1]: Lighten(c, 0.2) moves the HSL lightness 20 percentage points toward white, Darken toward black, Saturate/Desaturate adjust HSL saturation, and Mix blends two colours by a weight (0 returns the first colour, 1 the second, 0.5 an even blend). Luminance returns the WCAG relative luminance of a colour and ContrastRatio returns the WCAG 2.1 contrast ratio between two colours (1 to 21), the basis for IsLight/IsDark. Everything is deterministic and depends only on the standard library (math, strings, fmt).
Index ¶
- func Complement(hex string) (string, error)
- func ContrastColor(hex string) (string, error)
- func ContrastRatio(c1, c2 string) (float64, error)
- func Darken(hex string, amount float64) (string, error)
- func Desaturate(hex string, amount float64) (string, error)
- func Grayscale(hex string) (string, error)
- func HSLToHex(c HSL) string
- func Invert(hex string) (string, error)
- func IsDark(hex string) (bool, error)
- func IsLight(hex string) (bool, error)
- func Lighten(hex string, amount float64) (string, error)
- func Luminance(c RGB) float64
- func Mix(c1, c2 string, weight float64) (string, error)
- func RGBToHex(c RGB) string
- func RotateHue(hex string, deg float64) (string, error)
- func Saturate(hex string, amount float64) (string, error)
- func Shade(hex string, amount float64) (string, error)
- func Tint(hex string, amount float64) (string, error)
- type HSL
- type HSV
- type RGB
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Complement ¶
Complement returns the complementary colour (hue rotated 180 degrees).
func ContrastColor ¶
ContrastColor returns "#000000" or "#ffffff", whichever has the higher WCAG contrast ratio against the given background hex colour — the standard way to choose legible foreground text. It returns an error if hex is malformed.
func ContrastRatio ¶
ContrastRatio returns the WCAG 2.1 contrast ratio between two hex colours, a value from 1 (identical) to 21 (black on white).
func Darken ¶
Darken decreases the HSL lightness of a hex colour by amount and returns the new hex colour.
func Desaturate ¶
Desaturate decreases the HSL saturation of a hex colour by amount and returns the new hex colour.
func Grayscale ¶
Grayscale removes all saturation from a hex colour, returning its grey equivalent (preserving lightness).
func IsLight ¶
IsLight reports whether a hex colour has a relative luminance above 0.5, meaning dark text reads well on it.
func Lighten ¶
Lighten increases the HSL lightness of a hex colour by amount (a fraction in [0,1], clamped) and returns the new hex colour.
func Luminance ¶
Luminance returns the WCAG 2.1 relative luminance of an RGB colour, a value in [0,1] where 0 is black and 1 is white.
func Mix ¶
Mix blends two hex colours in sRGB space by weight (0 returns c1, 1 returns c2, 0.5 an even mix) and returns the resulting hex colour.
func RotateHue ¶
RotateHue rotates the HSL hue of a hex colour by deg degrees (wrapping into [0,360)) and returns the new hex colour.
func Saturate ¶
Saturate increases the HSL saturation of a hex colour by amount and returns the new hex colour.
Types ¶
type HSL ¶
HSL is a colour in the hue-saturation-lightness model. H is in degrees [0,360); S and L are fractions in [0,1].
type HSV ¶
HSV is a colour in the hue-saturation-value model. H is in degrees [0,360); S and V are fractions in [0,1].