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 Hex6Parameters
color(string): The color string to check.
Return Value
boolean:trueif validHex6,falseotherwise.
Example Usage
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 Hex8Example Usage
isRGB
Checks if a color is in rgb(...) format and components are within $0 \dots 255$.
Function Signature
function isRGB(color: string): color is RGBExample Usage
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 RGBAExample Usage
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 HSLExample Usage
isHSLA
Checks if a color is in hsla(...) format, validating HSL parameters and the alpha channel.
Function Signature
function isHSLA(color: string): color is HSLAExample Usage
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
- Color converters - Convert between HEX, RGB, and HSL formats.
- Color class - Deep OOP-based color manipulation class.
Last updated: Sun, Jun 14, 2026 08:22:19PM (UTC)
