Skip to content

serializeColor

serializeColor(props): string

Defined in: packages/core/src/fields/color/convert.ts:290

Serialises a colour into one of the supported CSS notations.

Alpha is emitted only when the colour is not fully opaque, so an opaque colour produces #e8622a rather than #e8622aff and rgb(...) rather than rgba(..., 1).

Input props.

ColorValue

The colour to write.

"hex" | "rgb" | "hsl" | "oklch"

The notation to write it in.

string

A CSS colour string in format.

An Error if an unrecognised format is given. Reaching this is a compile error: the default arm binds the exhausted union to never.

const ember = { r: 232, g: 98, b: 42, a: 1 };
serializeColor({ color: ember, format: "hex" }) // "#e8622a"
serializeColor({ color: ember, format: "oklch" }) // "oklch(65.7% 0.1793 40.9)"