Colore JS

Search documentation

Find a function or jump to a section.
v2.0.1

nearestNamed

nearestNamed finds the closest CSS named color to a hex, RGB or OKLCH value. Use the name to label a swatch and keep the distance if you also want to show how close the match is.

Example

import { colore } from 'colore-js';
import { nearestNamed } from 'colore-js/fn';

colore('#7c3aed').nearestNamed().name;   // 'blueviolet'
nearestNamed('#7c3aed').color;          // '#8a2be2'
nearestNamed('#7c3aed').distance.toFixed(4); // '0.0372'

Syntax

colore(target).nearestNamed(
  distanceFn?: (first: InternalColor, second: InternalColor) => number
): NearestNamedResult

nearestNamed(target: ColorInput): { name: string; color: string; distance: number }

Parameters

NameTypeDescription
targetColorInputThe color you want to name.
distanceFn(first: InternalColor, second: InternalColor) => numberOptional class comparator. By default both APIs use OKLab distance.

Returns

NameTypeDescription
namestringThe CSS color name.
colorColor | stringThe named color as a Color from the method or a CSS string from the function.
distancenumberDistance from the input under the chosen measure.

Throws

A ColorError when an input cannot be parsed.

Name a translucent swatch

import { nearestNamed } from 'colore-js/fn';

nearestNamed('#0000');
// { name: 'black', color: '#000000', distance: 0 }

Default matching compares the color channels regardless of opacity. Use the original alpha if it matters to your swatch or displayed CSS.