Skip to content

@jsvision/core / toRgb

Function: toRgb()

toRgb(color): Rgb | null

Defined in: color/color.ts:66

Validate a Color and parse it to RGB components.

'default' returns null (meaning "leave the terminal's own default color"), a named ANSI-16 color returns its reference RGB, and a #rgb/#rrggbb hex string is parsed (the 3-digit form expands each nibble, so #f00 === #ff0000). Anything else throws — the value is never partially parsed.

Parameters

color

Color

The color to parse: 'default', a named ANSI-16 color (e.g. 'red', 'brightBlue'), or a hex string.

Returns

Rgb | null

The { r, g, b } components (each 0–255), or null for 'default'.

Throws

InvalidColorError when color is a malformed hex string or an unknown name.

Example

ts
import { toRgb } from '@jsvision/core';

toRgb('#ff0000');  // → { r: 255, g: 0, b: 0 }
toRgb('#f00');     // → { r: 255, g: 0, b: 0 }  (shorthand expands)
toRgb('default');  // → null  (keep the terminal default)