Skip to content

@jsvision/core / nearest256

Function: nearest256()

nearest256(rgb): number

Defined in: color/downsample.ts:52

Find the xterm-256 palette index (0–255) closest to an RGB color.

Scans all 256 reference entries by redmean distance; on a tie the lower index wins, so #000000 → 0 and #ffffff → 15 stay exact instead of drifting into the color cube.

Parameters

rgb

Rgb

The source color (from toRgb).

Returns

number

The nearest palette index, 0–255.

Example

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

nearest256(toRgb('#000000')!);  // → 0
nearest256(toRgb('#ffffff')!);  // → 15
nearest256({ r: 0x80, g: 0x80, b: 0x80 });  // → 244 (a gray-ramp slot)