Documentation
¶
Overview ¶
Package color provides 24-bit RGB values, perceptual metrics, and blend operations, independent of any output device.
RGB is a plain 3-byte value with toml field tags, safe for config binding and for embedding in dense cell or pixel buffers. It satisfies image/color.Color, so values pass directly into image, draw, and GUI toolkit pipelines without conversion.
Palette quantization (RGBTo256, RGBTo16, Xterm256To16) and the xterm palette's colors (Xterm256) live here; device concerns — terminal capability detection, SGR emission, framebuffer formats — belong in the consuming package.
Operations ¶
Blend linear alpha compositing SoftLight Perez soft light, gentler than linear alpha Overlay multiply on darks, screen on lights Screen always lightens, avoids the clipping harshness of Add Add saturating additive Max per-channel maximum Scale channel multiply, saturating Grayscale Rec. 601 luma Lerp linear interpolation
Concurrency ¶
All values are immutable and all operations are pure. Package-level palette variables are writable by the language but are read-only by contract.
Index ¶
- Constants
- Variables
- func Cube256(r, g, b uint8) uint8
- func CubeRGB256(index uint8) (r, g, b uint8)
- func Gray256(step uint8) uint8
- func Luma(c RGB) uint8
- func RGBTo16(c RGB) uint8
- func RGBTo256(c RGB) uint8
- func RedmeanDistance(a, b RGB) int
- func WarmXterm256()
- func Xterm256To16(index uint8) uint8
- type RGB
- func Add(dst, src RGB, alpha float64) RGB
- func Blend(dst, src RGB, alpha float64) RGB
- func Desaturate(c RGB, t float64) RGB
- func From(c stdcolor.Color) RGB
- func Grayscale(c RGB) RGB
- func Lerp(a, b RGB, t float64) RGB
- func LerpFixed(a, b RGB, t int64, shift uint) RGB
- func Max(dst, src RGB, alpha float64) RGB
- func MustParseHex(s string) RGB
- func Overlay(dst, src RGB, alpha float64) RGB
- func ParseHex(s string) (RGB, error)
- func Scale(c RGB, factor float64) RGB
- func Screen(dst, src RGB, alpha float64) RGB
- func SoftLight(dst, src RGB, intensity float64) RGB
- func Xterm256(index uint8) RGB
Constants ¶
const ( P256DarkBlue uint8 = 18 // (0,0,2) P256SteelBlue uint8 = 75 // (1,3,5) P256LightBlue uint8 = 81 // (1,4,5) P256DeepTeal uint8 = 23 // (0,1,1) P256Teal uint8 = 44 // (0,4,4) P256Green uint8 = 46 // (0,5,0) P256Cyan uint8 = 51 // (0,5,5) P256LightCyan uint8 = 87 // (1,5,5) P256CobaltBlue uint8 = 33 // (0,2,5) P256DarkPurpleBlue uint8 = 54 // (1,0,2) P256Indigo uint8 = 63 // (1,1,5) P256Purple uint8 = 129 // (3,0,5) P256Violet uint8 = 134 // (3,1,4) P256MediumPurple uint8 = 135 // (3,1,5) P256Orchid uint8 = 176 // (4,2,4) P256YellowGreen uint8 = 154 // (3,5,0) P256Maroon uint8 = 52 // (1,0,0) P256DarkCrimson uint8 = 88 // (2,0,0) P256Crimson uint8 = 160 // (4,0,0) P256Red uint8 = 196 // (5,0,0) P256Rose uint8 = 198 // (5,0,2) P256RedOrange uint8 = 202 // (5,1,0) P256Orange uint8 = 208 // (5,2,0) P256Amber uint8 = 214 // (5,3,0) P256Gold uint8 = 220 // (5,4,0) P256Yellow uint8 = 226 // (5,5,0) P256DarkAmber uint8 = 94 // (2,1,0) P256Gray uint8 = 240 // Grayscale step 8, level ~88 )
const ( ANSIBlack uint8 = iota ANSIRed ANSIGreen ANSIYellow ANSIBlue ANSIMagenta ANSICyan ANSIWhite ANSIBrightBlack ANSIBrightRed ANSIBrightGreen ANSIBrightYellow ANSIBrightBlue ANSIBrightMagenta ANSIBrightCyan ANSIBrightWhite )
ANSI 16 color indices, sent as SGR 30-37/90-97 (40-47/100-107 background). The terminal's theme picks the shade; a text console renders them too.
Variables ¶
var ( // --- Achromatic --- Black = RGB{0, 0, 0} Charcoal = RGB{5, 5, 5} Obsidian = RGB{20, 20, 30} // Blue-black Gunmetal = RGB{26, 27, 38} // Blue-tinted near-black DarkSlate = RGB{35, 36, 48} // Blue-gray near-black DimGray = RGB{55, 55, 55} DarkGray = RGB{60, 60, 60} IronGray = RGB{80, 80, 80} SlateGray = RGB{80, 80, 90} // Cool-tinted Taupe = RGB{100, 95, 85} // Warm gray Gray = RGB{120, 120, 120} MidGray = RGB{128, 128, 128} CoolSilver = RGB{140, 145, 155} // Blue-tinted silver DimSilver = RGB{155, 155, 155} Silver = RGB{180, 180, 180} LightGray = RGB{200, 200, 200} NearWhite = RGB{250, 250, 250} White = RGB{255, 255, 255} // --- Brown / Earth --- Chocolate = RGB{90, 25, 15} SaddleBrown = RGB{101, 67, 33} DarkRust = RGB{140, 35, 25} Sienna = RGB{140, 60, 0} DarkPlum = RGB{60, 30, 40} // Warm dark purple-brown BlueCharcoal = RGB{40, 45, 60} // Cool dark blue-gray // --- Red --- BlackRed = RGB{50, 15, 15} Oxblood = RGB{100, 20, 20} DarkBurgundy = RGB{100, 25, 20} DarkCrimson = RGB{139, 0, 0} Brick = RGB{180, 40, 40} Cinnabar = RGB{200, 60, 50} IndianRed = RGB{180, 60, 60} BurntSienna = RGB{200, 60, 25} Vermilion = RGB{227, 66, 82} Red = RGB{255, 0, 0} BrightRed = RGB{255, 60, 60} Coral = RGB{255, 80, 80} Salmon = RGB{255, 100, 100} LightCoral = RGB{255, 140, 140} LightRose = RGB{255, 150, 150} MistyRose = RGB{255, 200, 200} // --- Orange --- DarkAmber = RGB{60, 40, 0} Rust = RGB{180, 60, 20} Amber = RGB{180, 120, 0} Bronze = RGB{200, 100, 0} BurntOrange = RGB{200, 110, 0} Terracotta = RGB{220, 100, 50} FlameOrange = RGB{240, 100, 30} OrangeRed = RGB{255, 69, 0} RedOrange = RGB{255, 80, 40} Mango = RGB{255, 120, 50} TigerOrange = RGB{255, 140, 0} WarmOrange = RGB{255, 140, 40} Apricot = RGB{255, 160, 60} Orange = RGB{255, 165, 0} // --- Yellow --- DarkGold = RGB{200, 150, 0} OliveYellow = RGB{200, 180, 60} Gold = RGB{255, 215, 0} LemonYellow = RGB{255, 240, 60} Yellow = RGB{255, 255, 0} PaleGold = RGB{255, 200, 100} Buttercream = RGB{255, 250, 150} PaleLemon = RGB{255, 255, 100} Ivory = RGB{255, 255, 220} Cream = RGB{255, 255, 200} // --- Green --- BlackGreen = RGB{0, 40, 0} DarkFern = RGB{30, 80, 25} DeepForest = RGB{25, 80, 35} HunterGreen = RGB{35, 90, 30} DarkGreen = RGB{15, 130, 15} ForestGreen = RGB{34, 139, 34} MediumGreen = RGB{40, 150, 40} FernGreen = RGB{50, 140, 45} LeafGreen = RGB{60, 160, 60} SeaGreen = RGB{60, 180, 80} SageGreen = RGB{70, 170, 100} GrassGreen = RGB{70, 180, 55} EmeraldGreen = RGB{60, 220, 100} MintGreen = RGB{100, 220, 130} BrightGreen = RGB{20, 200, 20} YellowGreen = RGB{100, 220, 80} LimeGreen = RGB{50, 205, 50} NeonGreen = RGB{50, 255, 50} BrightLime = RGB{120, 255, 80} Lime = RGB{0, 255, 0} LightGreen = RGB{144, 238, 144} PaleGreen = RGB{120, 255, 120} PastelGreen = RGB{100, 220, 100} PaleMint = RGB{150, 255, 180} Honeydew = RGB{200, 255, 200} // --- Cyan / Teal --- Teal = RGB{0, 139, 139} DimCyan = RGB{0, 160, 160} VibrantCyan = RGB{0, 200, 200} DarkTurquoise = RGB{0, 206, 209} BrightCyan = RGB{0, 220, 220} Cyan = RGB{0, 255, 255} SkyTeal = RGB{80, 200, 220} PaleCyan = RGB{200, 255, 255} AliceBlue = RGB{230, 245, 255} IceCyan = RGB{240, 255, 255} // --- Blue --- DeepIndigo = RGB{40, 0, 180} CobaltBlue = RGB{50, 80, 200} SteelBlue = RGB{60, 100, 180} MediumBlue = RGB{60, 120, 200} RoyalBlue = RGB{65, 105, 225} CeruleanBlue = RGB{80, 140, 220} Cornflower = RGB{80, 130, 255} DodgerBlue = RGB{40, 180, 255} LightBlue = RGB{120, 170, 255} LightSkyBlue = RGB{135, 206, 250} BabyBlue = RGB{160, 210, 255} Blue = RGB{0, 0, 255} // --- Purple / Violet --- DeepPurple = RGB{60, 20, 80} DarkViolet = RGB{120, 40, 180} MutedPurple = RGB{160, 100, 160} MediumPurple = RGB{170, 100, 210} ElectricViolet = RGB{180, 130, 255} LightOrchid = RGB{200, 130, 210} Orchid = RGB{200, 120, 220} PaleVioletRed = RGB{219, 112, 147} SoftLavender = RGB{220, 150, 230} PaleLavender = RGB{220, 180, 255} // --- Pink / Rose --- RoseRed = RGB{255, 60, 120} HotMagenta = RGB{255, 60, 200} HotPink = RGB{255, 140, 200} PalePink = RGB{255, 145, 220} LightPink = RGB{255, 182, 193} Pink = RGB{255, 192, 203} Magenta = RGB{255, 0, 255} )
Functions ¶
func CubeRGB256 ¶
CubeRGB256 returns the (r, g, b) cube coordinates for a 256-palette color cube index.
func RGBTo16 ¶
RGBTo16 converts 24-bit RGB to the nearest ANSI 16 index (Redmean distance to xterm's default shades), for terminals limited to 16 colors.
func RGBTo256 ¶
RGBTo256 converts 24-bit RGB to the nearest xterm-256 palette index. O(1) via the Redmean LUT; the first call builds it.
func RedmeanDistance ¶
RedmeanDistance returns squared perceptually-weighted distance between a and b. Monotonic in perceived difference; use for nearest-color search, not as an absolute metric. Formula: https://en.wikipedia.org/wiki/Color_difference#sRGB
func WarmXterm256 ¶
func WarmXterm256()
WarmXterm256 forces LUT construction. Idempotent, safe for concurrent use. Optional, prevents latency spike on first RGBTo256 render in the terminal loop.
func Xterm256To16 ¶ added in v0.1.5
Xterm256To16 converts an xterm-256 palette index to the nearest ANSI 16 index, for a text console: the first 16 are themselves, the rest go as RGBTo16 takes their Xterm256 color. O(1).
Types ¶
type RGB ¶
RGB represents a 24-bit color
func Blend ¶
Blend performs linear alpha blend of src over dst alpha <= 0 returns dst, alpha >= 1 returns src
func Desaturate ¶
Desaturate is partial desaturation; t=0 identity, t=1 full grayscale
func From ¶
From converts any image/color.Color to RGB, un-premultiplying by alpha and discarding it. Fully transparent input yields Black.
func LerpFixed ¶
LerpFixed interpolates a→b using fixed-point factor t with the given fractional bit count. t is nominally in [0, 1<<shift]. No float, no alloc. Truncates toward −∞ (parity with the prior integer implementation); this is intentionally distinct from the half-up float Lerp.
func MustParseHex ¶
MustParseHex panics on invalid input; for package-level initializers
func Screen ¶
Screen applies 1-(1-dst)*(1-src), alpha-blended over dst Always lightens; useful for glow accumulation without clipping harshness of Add
func SoftLight ¶
SoftLight applies Perez soft light blend, gentler than linear alpha intensity in [0,1] mixes between dst and the blended result
func Xterm256 ¶ added in v0.1.5
Xterm256 returns the color xterm shows by default for a palette index: the ANSI 16 shades RGBTo16 measures against, the 6x6x6 cube, then the gray ramp.