@pbroom/color-kitConversion

toHex

Convert a Color to a lowercase hex string: #rrggbb, or #rrggbbaa when alpha is below 1.

functionimport { toHex } from '@pbroom/color-kit';index.ts:136
function toHex(color: Color): string;

Out-of-gamut colors are clipped per channel like toRgb.

Parameters

colorColor
Color to convert

Returns

stringHex string with a leading #

Throws

  • When any channel of color is not finite

Example

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

toHex(parse('oklch(0.7 0.15 30)')); // → '#ed7665'
toHex(parse('rgb(59 130 246 / 0.5)')); // → '#3b82f680'

See also