isValidColor
isValidColor checks if Colore can parse a CSS color string. Use it for a yes-or-no answer in a form, token import or stylesheet tool.
Example
import { isValidColor } from 'colore-js';
isValidColor('#2563eb80'); // true
isValidColor('oklch(0.7 0.18 280)'); // true
isValidColor('hsl(nope)'); // false
Syntax
isValidColor(input: string): boolean
Parameters
Returns
true when Colore can parse the string and false otherwise. currentColor and var() need CSS context, so they return false here.
Throws
None. For an error that explains why parsing failed, call parse.
Validate a user-entered color
Guard user input before you use it to build a style. Colore accepts modern formats such as Display P3 and explicit-space color-mix() expressions, so you can keep wide-gamut input.
import { colore, isValidColor } from 'colore-js';
const input = 'color(display-p3 0 1 0)';
isValidColor(input) ? colore(input).css() : '#2563eb'; // 'color(display-p3 0.00000 1.00000 0.00000)'
isValidColor('var(--brand)'); // false
Use Color.try() if you need the parsed color on success. It returns the object or null in one call.
Related
- detectFormat to identify the input syntax
- parse to read numeric channels
- Color static methods for
Color.try()