Toolbox-XToolbox-X

Color Format Checkers

A collection of type guards to validate color formats like HEX, RGB, RGBA, HSL, and HSLA.

Overview

These functions are TypeScript type guards that check whether a given color string conforms to a specific color format and lies within valid value ranges.


isHex6

Checks if a color is in 6-digit HEX format (e.g., #FF5733).

Function Signature

function isHex6(color: string): color is Hex6

Parameters

  • color (string): The color string to check.

Return Value

  • boolean: true if valid Hex6, false otherwise.

Example Usage

playground.ts

isHex8

Checks if a color is in 8-digit HEX format, which includes the alpha channel (e.g., #FF573380).

Function Signature

function isHex8(color: string): color is Hex8

Example Usage

playground.ts

isRGB

Checks if a color is in rgb(...) format and components are within $0 \dots 255$.

Function Signature

function isRGB(color: string): color is RGB

Example Usage

playground.ts

isRGBA

Checks if a color is in rgba(...) format, including a valid alpha value between $0 \dots 1$.

Function Signature

function isRGBA(color: string): color is RGBA

Example Usage

playground.ts

isHSL

Checks if a color is in hsl(...) format, validating hue ($0 \dots 360$) and saturation/lightness percentages ($0 \dots 100%$).

Function Signature

function isHSL(color: string): color is HSL

Example Usage

playground.ts

isHSLA

Checks if a color is in hsla(...) format, validating HSL parameters and the alpha channel.

Function Signature

function isHSLA(color: string): color is HSLA

Example Usage

playground.ts

Type Guards and Narrowing

Because these functions are type guards, TypeScript will automatically narrow the type in conditional blocks:

import { isHex6, Hex6 } from 'toolbox-x/colors';

const input: string = '#FF5733';

if (isHex6(input)) {
  const hex: Hex6 = input; // No type error
}

See Also

Last updated: Sun, Jun 14, 2026 08:22:19PM (UTC)

On this page