parseHex

Commons
stable

Normalise any hex color string — with or without leading `#`, short-form (3/4 digits) or long-form (6/8 digits) — into a canonical 6-character `THex` string, replacing invalid characters with `F`.

API

Signature

Full TypeScript signature sourced from the util file.

typescript

API

Parameters

Input parameters accepted by this util.

hex
required
string
A hex color string in any accepted format: with or without `#`, 3/4/6/8 digit. Invalid characters are silently coerced to `F`.

API

Return value

The value returned by this util.

A normalised 6-character uppercase hex string (e.g. `"3B82F6"`). Invalid inputs are padded/coerced rather than throwing.

Usage

Examples

Ready-to-paste usage snippets.

Normalise hex color strings

typescript