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
Returns
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.
Related
- nearest to match against a private palette
- deltaEOK for the default distance
- OKLCH in JavaScript to convert a color before labeling it