@pbroom/color-kitConversion

tryParse

Like parse, but returns null instead of throwing when input is not a supported, well-formed CSS color (or not a string). Never throws.

functionimport { tryParse } from '@pbroom/color-kit';parse.ts:326
function tryParse(  input: string,): Color | null;

Parameters

inputstring
CSS color string

Returns

Color | nullThe parsed Color, or null

Example

TypeScript
import { toHex, tryParse } from '@pbroom/color-kit';

const color = tryParse('#3b82f6');
color && toHex(color); // → '#3b82f6'
tryParse('#gggggg'); // → null

See also