Colore JS

Search documentation

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

detectFormat

detectFormat identifies the format a color string appears to use. Use it to label or group colors before you process their channels.

Example

import { detectFormat } from 'colore-js';

detectFormat('#2563eb80'); // 'hex-alpha'
detectFormat('color(display-p3 0.9 0.3 0.2)'); // 'p3'
detectFormat('var(--brand)'); // null

Syntax

detectFormat(input: string): DetectedFormat | null

Parameters

NameTypeDescription
inputstringThe text to classify. The check ignores surrounding whitespace and letter case.

Returns

A name such as hex, hex-alpha, rgb, rgba, hsl, hsla, hwb, lab, lch, oklab, oklch, p3, srgb-color, color-mix, named or transparent. An unrecognized input returns null.

Throws

None. Use isValidColor to check if the recognized syntax parses.

Tell a recognized format from a valid color

A recognized function name can contain invalid channels. Check the value before letting it through a form or token import.

import { detectFormat, isValidColor } from 'colore-js';

detectFormat('rgb(nope)'); // 'rgb'
isValidColor('rgb(nope)'); // false

The parser reports the failing value if you need an error message. Format detection is useful when you already have a verified input and want to choose labels or controls for it.