2.7 KiB
2.7 KiB
colour
(no submodules)
Purpose
colour.ts implements the Colour class — an immutable, chainable colour value object. It wraps an internal RGBA state and exposes methods for manipulation, analysis, conversion, and output formatting. The class is the primary public API surface for the package and is used by the colour-factories helpers.
Features
- Immutable chainable manipulation (lighten, darken, saturate, desaturate, rotate, alpha, fade, opaquer)
- Colour-space conversion methods (hex, RGB, HSL, HSV, LAB, LCH, OKLAB, OKLCH, CMYK)
- Analysis methods (luminance, perceived brightness, contrast ratio, isDark, isLight, best contrast, distance)
- Utility methods (negate, grayscale, mix, clone, equals, toString)
- Pure functional implementation with no mutation of internal state
Usage: Basic Example
import { Colour } from "@suaveplan/colours/colour";
const c = new Colour("#ff9900");
console.log(c.lighten(0.2).toHex()); // "#ffb34d"
console.log(c.isDark()); // false
Usage: Advanced Example
import { Colour } from "@suaveplan/colours/colour";
const brand = new Colour("#3b82f6");
const text = brand.bestContrast("#ffffff", "#1f2937");
console.log(text.toHex()); // "#ffffff"
API Reference
Constructor
new Colour(input: ColourInput)— parses any supported colour input into an immutableColourinstance.
Manipulation
lighten(amount: number): Colourdarken(amount: number): Coloursaturate(amount: number): Colourdesaturate(amount: number): Colourrotate(degrees: number): Colouralpha(a: number): Colourfade(amount: number): Colouropaquer(amount: number): Colournegate(): Colourgrayscale(): Colourmix(other: ColourInput, amount?: number): Colour
Analysis
luminance(): numberperceivedBrightness(): numbercontrast(other: ColourInput): numberisDark(): booleanisLight(): booleanbestContrast(...candidates: (Colour | string)[]): Colourdistance(other: ColourInput, mode?: DistanceMode): numberequals(other: ColourInput): boolean
Output
toHex(): stringtoHex3(): stringtoHex8(): stringtoRgb(): RGBtoRgba(): RGBAtoHsl(): HSLtoHsv(): HSVtoLab(): LABtoLch(): LCHtoOklab(): OKLABtoOklch(): OKLCHtoCmyk(): CMYKtoString(): string
Implementation Notes
The internal _rgba state is never mutated. Every chainable method returns a new Colour instance. Conversion methods delegate to the conversions module. Analysis methods use the standard WCAG 2.1 luminance formula and sRGB gamma. The bestContrast helper uses the APCA-like perceived brightness for candidate ranking.