parse('#gggggg') returned { l: NaN, c: NaN, h: NaN } because hexToRgb
never validated its digits, toHex then emitted "#NaNNaNNaN", inputs such
as rgb(1.2.3 0 0) were accepted, and toCss silently fell back to hex for
unknown formats.
- hexToRgb validates length (3/4/6/8) and digits and throws otherwise.
- parse moves to conversion/parse.ts and validates every component token
against the CSS <number> grammar, so malformed numbers throw and
components that overflow to Infinity (1e999) throw too.
- New tryParse(input): Color | null, exported from the root. A nullable
return matches the existing parseColorStringInputValue helper in
@color-kit/driver and keeps the hot path allocation-free; a result
object would add API surface without carrying more than the error
message, which parse already provides.
- toCss takes a CssColorFormat string-literal union and throws a
TypeError for unknown formats at runtime. @color-kit/react's
requestedCss/displayedCss callbacks now take CssColorFormat.
- toHex, toCss and rgbToHex throw a RangeError for non-finite channels
instead of emitting NaN digits.
- Tests include fast-check properties: arbitrary garbage never parses to
a non-finite Color, and tryParse never throws.
Bundle size: parse + toHex + contrastRatio 2.32 kB -> 2.64 kB (limit
2.7 kB, unchanged).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
parse('#gggggg') returned { l: NaN, c: NaN, h: NaN } because hexToRgb
never validated its digits, toHex then emitted "#NaNNaNNaN", inputs such
as rgb(1.2.3 0 0) were accepted, and toCss silently fell back to hex for
unknown formats.
against the CSS grammar, so malformed numbers throw and
components that overflow to Infinity (1e999) throw too.
return matches the existing parseColorStringInputValue helper in
@color-kit/driver and keeps the hot path allocation-free; a result
object would add API surface without carrying more than the error
message, which parse already provides.
TypeError for unknown formats at runtime. @color-kit/react's
requestedCss/displayedCss callbacks now take CssColorFormat.
instead of emitting NaN digits.
a non-finite Color, and tryParse never throws.
Bundle size: parse + toHex + contrastRatio 2.32 kB -> 2.64 kB (limit
2.7 kB, unchanged).
Part of a stacked set of PRs (#204 → #221) from the color-kit assessment. Validated per branch with
pnpm buildandpnpm test; lint, format, typecheck, size, artifact, preprod and agents checks pass at the top of the stack.🤖 Generated with Claude Code