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
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.
Related
- isValidColor to check a value
- parse to get numeric channels
- parseToSpace to read the source space