Skip to content

@jsvision/core / encodeStyle

Function: encodeStyle()

encodeStyle(fg, bg, attrs, caps): string

Defined in: color/encode.ts:128

Merge text attributes + foreground + background into ONE escape sequence, downsampled to the terminal's color depth. This is the encoder the renderer uses per cell.

Crash-safe: a malformed color degrades to no-color rather than throwing, so bad cell data can never take down the render loop. At mono depth no color codes are emitted, but attributes (bold, underline, …) still are, to keep text legible.

Parameters

fg

Color

Foreground color.

bg

Color

Background color.

attrs

number

Attribute bitmask (see Attr; combine with |).

caps

CapabilityProfile

A resolved capability profile — only its colorDepth is read.

Returns

string

One merged escape sequence, or '' when nothing needs emitting.

Example

ts
import { encodeStyle, resolveCapabilities, Attr } from '@jsvision/core';

const caps = resolveCapabilities({ override: { colorDepth: 'truecolor' } }).profile;

encodeStyle('#ff0000', '#0000ff', Attr.bold, caps);
// → '\x1b[1;38;2;255;0;0;48;2;0;0;255m'
encodeStyle('default', 'default', Attr.bold | Attr.underline, caps);
// → '\x1b[1;4m'  (attributes only)