Skip to content

Color

Color utility with multiple output formats and ANSI helpers.

Diagram

100%Ctrl + wheel to zoom

Constructor

ts
new Color(input: ColorInput, protectionLevel?: ProtectionLevel)
ArgumentTypeDescription
inputColorInputColor input.
protectionLevelProtectionLevelValidation strictness.

ColorInput can be:

ts
string
| { r: number; g: number; b: number; a?: number }
| { h: number; s: number; l: number; a?: number }

Properties

This class exposes no public instance properties.

Static Properties

PropertyTypeDescription
RESETstringANSI reset sequence.
BOLDstringANSI bold sequence.
UNDERLINEstringANSI underline sequence.

Methods

.hex()

Returns the color as a hex string.

.rgb()

Returns the color as an rgb(...) string.

.rgba()

Returns the color as an rgba(...) string.

.hsl()

Returns the color as an hsl(...) string.

.hsla()

Returns the color as an hsla(...) string.

.css()

Returns a CSS-ready string for this color.

.ansiTruecolor()

Returns a 24-bit ANSI sequence for the foreground color.

.ansiTruecolorBg()

Returns a 24-bit ANSI sequence for the background color.

.ansi256()

Returns a 256-color ANSI sequence for the foreground color.

.ansi256Bg()

Returns a 256-color ANSI sequence for the background color.

.wrapAnsi(text: string, opts?: { background?: boolean; use256?: boolean; bold?: boolean; underline?: boolean })

Wraps the given text with ANSI styling based on the color.

ArgumentTypeDescription
textstringText to wrap.
optsobjectFormatting options.

opts fields:

  • background (boolean): Use background color.
  • use256 (boolean): Use 256-color mode.
  • bold (boolean): Apply bold.
  • underline (boolean): Apply underline.

Example

ts
const color = new Color({ r: 255, g: 0, b: 0 }, "boundary");
console.log(color.hex()); // "#ff0000"
console.log(color.rgb()); // "rgb(255, 0, 0)"
console.log(color.ansiTruecolor()); // ANSI sequence for red foreground
console.log(color.wrapAnsi("Hello", { bold: true })); // "Hello" wrapped in red and bold ANSI sequences