color

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: BSD-3-Clause Imports: 5 Imported by: 4 modules, 22 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

color

24-bit RGB values, perceptual metrics, and blend operations. No output device, no dependencies.

Extracted from lixenwraith/terminal so renderers — terminal, GUI, image, framebuffer — share one color type and one set of operations without linking terminal I/O, x/sys, or termios.

Install

go get github.com/lixenwraith/color

Go 1.26+. Standard library only.

Type

type RGB struct {
    R uint8 `toml:"r"`
    G uint8 `toml:"g"`
    B uint8 `toml:"b"`
}

Three bytes, comparable, pointer-free: safe to embed in dense cell or pixel buffers and to bind directly from config.

RGB implements image/color.Color, so values pass into image, draw, and GUI toolkit pipelines unchanged. From converts back, un-premultiplying alpha and discarding it.

img.Set(x, y, color.Amber)          // RGB satisfies image/color.Color
c := color.From(img.At(x, y))       // back to RGB

Where both packages are needed at one site, alias the standard library: import stdcolor "image/color".

Operations

Call Behavior
Blend(dst, src, alpha) Linear alpha compositing
SoftLight(dst, src, intensity) Perez soft light; gentler than linear alpha
Overlay(dst, src, alpha) Multiply on darks, screen on lights
Screen(dst, src, alpha) Always lightens; glow accumulation without Add clipping
Add(dst, src, alpha) Saturating additive
Max(dst, src, alpha) Per-channel maximum
Scale(c, factor) Channel multiply, saturating
Grayscale(c), Luma(c) Rec. 601 luma
c.Lerp(other, t) Linear interpolation
RedmeanDistance(a, b) Squared perceptual distance, for nearest-color search

All operations are pure. alpha and t clamp to [0,1]; channels saturate. Integer paths avoid division; soft light is table-driven, no sqrt per channel.

bg   := color.Obsidian
glow := color.Screen(bg, color.Amber, 0.4)
edge := bg.Lerp(color.Amber, 0.75)
warm := color.SoftLight(edge, color.Terracotta, 0.3)

Palette

~120 named colors, grouped by hue and ordered dark-to-light: Obsidian, Amber, EmeraldGreen, LightSkyBlue, Vermilion, … Standard names (CSS, X11) where the RGB matches; descriptive compounds otherwise.

Package-level vars. Read-only by contract — the language permits assignment, the package does not. Alias them into domain parameter files rather than mutating them.

Hex

c, err := color.ParseHex("#4a90d9")     // also "4a90d9", "#abc", "abc"
s := c.Hex()                            // "#4a90d9"
var Accent = color.MustParseHex("#ff8800")

RGB deliberately does not implement encoding.TextUnmarshaler. TOML and JSON decoders prefer it over struct-field unification, which would silently break table-form config:

accent = { r = 255, g = 136, b = 0 }

Terminal palettes

Quantization for terminals that cannot take 24-bit color:

idx := color.RGBTo256(color.EmeraldGreen)   // xterm-256 index, Redmean LUT
ansi := color.RGBTo16(color.EmeraldGreen)   // ANSI 16 index, for text consoles

ANSIBlack .. ANSIBrightWhite name the 16 indices (SGR 30-37/90-97). The terminal's theme picks their shade, so they suit semantic colors such as log levels. RGBTo16 measures against xterm's default shades.

terminal.Cell carries color.RGB directly. When Cell.Attrs sets AttrFg256 / AttrBg256, Cell.Fg.R / Cell.Bg.R hold a palette index, not a channel. Such values are not colors and must not be passed to this package.

Concurrency

Values are immutable, operations are pure, lookup tables are built at package init. Safe for concurrent use.

License

See LICENSE.

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) lives 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

View Source
const (
	P256DeepNavy  uint8 = 17 // (0,0,1)
	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
)
View Source
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

View Source
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 ---
	DeepNavy     = RGB{15, 25, 50}
	DeepIndigo   = RGB{40, 0, 180}
	NavyBlue     = RGB{30, 60, 120}
	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 Cube256

func Cube256(r, g, b uint8) uint8

Cube256 returns the xterm 256-palette index for an RGB cube coordinate.

func CubeRGB256

func CubeRGB256(index uint8) (r, g, b uint8)

CubeRGB256 returns the (r, g, b) cube coordinates for a 256-palette color cube index.

func Gray256

func Gray256(step uint8) uint8

Gray256 returns the xterm 256-palette index for a grayscale step.

func Luma

func Luma(c RGB) uint8

Luma returns Rec. 601 luminance: R*0.299 + G*0.587 + B*0.114

func RGBTo16

func RGBTo16(c RGB) uint8

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

func RGBTo256(c RGB) uint8

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

func RedmeanDistance(a, b RGB) int

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.

Types

type RGB

type RGB struct {
	R uint8 `toml:"r"`
	G uint8 `toml:"g"`
	B uint8 `toml:"b"`
}

RGB represents a 24-bit color

func Add

func Add(dst, src RGB, alpha float64) RGB

Add performs saturating additive blend, alpha-blended over dst

func Blend

func Blend(dst, src RGB, alpha float64) RGB

Blend performs linear alpha blend of src over dst alpha <= 0 returns dst, alpha >= 1 returns src

func Desaturate

func Desaturate(c RGB, t float64) RGB

Desaturate is partial desaturation; t=0 identity, t=1 full grayscale

func From

func From(c stdcolor.Color) RGB

From converts any image/color.Color to RGB, un-premultiplying by alpha and discarding it. Fully transparent input yields Black.

func Grayscale

func Grayscale(c RGB) RGB

Grayscale converts to grayscale using Rec. 601 luma coefficients

func Lerp

func Lerp(a, b RGB, t float64) RGB

Lerp is free form of RGB.Lerp, uniform with the rest of the blend family

func LerpFixed

func LerpFixed(a, b RGB, t int64, shift uint) RGB

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 Max

func Max(dst, src RGB, alpha float64) RGB

Max returns per-channel maximum, alpha-blended over dst

func MustParseHex

func MustParseHex(s string) RGB

MustParseHex panics on invalid input; for package-level initializers

func Overlay

func Overlay(dst, src RGB, alpha float64) RGB

Overlay combines multiply (darks) and screen (lights), alpha-blended over dst

func ParseHex

func ParseHex(s string) (RGB, error)

ParseHex accepts "#rgb", "#rrggbb", and the same forms without the leading '#'

func Scale

func Scale(c RGB, factor float64) RGB

Scale multiplies all channels by factor, saturating (factor > 1.0 brightens)

func Screen

func Screen(dst, src RGB, alpha float64) RGB

Screen applies 1-(1-dst)*(1-src), alpha-blended over dst Always lightens; useful for glow accumulation without clipping harshness of Add

func SoftLight

func SoftLight(dst, src RGB, intensity float64) RGB

SoftLight applies Perez soft light blend, gentler than linear alpha intensity in [0,1] mixes between dst and the blended result

func (RGB) Hex

func (c RGB) Hex() string

Hex returns the color as "#rrggbb"

func (RGB) Lerp

func (c RGB) Lerp(other RGB, t float64) RGB

Lerp linearly interpolates from c to other by t, clamped to [0,1]

func (RGB) RGBA

func (c RGB) RGBA() (r, g, b, a uint32)

RGBA implements image/color.Color. Alpha is always opaque, so the premultiplied result equals the non-premultiplied one.

Directories

Path Synopsis
cmd
example-256 command
example-tc command

Jump to

Keyboard shortcuts

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