Validators
isValidColor
Section titled “isValidColor”Check if a string is a valid CSS color. Optionally check for a specific type.
isValidColor(input: string, type?: ColorTypeInput): booleanimport { isValidColor } from 'colorizr';
isValidColor('#ff0044'); // trueisValidColor('rgb(255 0 68)'); // trueisValidColor('hsl(344 100% 50%)'); // trueisValidColor('red'); // trueisValidColor('not-a-color'); // false
// Type-specific validationisValidColor('#ff0044', 'hex'); // trueisValidColor('#ff0044', 'rgb'); // falsePlayground
true
Type Guards
Section titled “Type Guards”These functions check if a value matches a specific color model structure.
isHex(input: unknown): booleanValidates hex strings (#xxx, #xxxx, #xxxxxx, #xxxxxxxx):
import { isHex } from 'colorizr';
isHex('#ff0044'); // trueisHex('#f04'); // trueisHex('#ff004480'); // trueisHex('ff0044'); // false (missing #)isHex('#xyz'); // falsePlayground
true
isHSL(input: unknown): booleanValidates HSL objects (h: 0-360, s: 0-100, l: 0-100):
import { isHSL } from 'colorizr';
isHSL({ h: 344, s: 100, l: 50 }); // trueisHSL({ h: 344, s: 100, l: 50, alpha: 0.5 }); // trueisHSL({ h: 400, s: 100, l: 50 }); // false (h > 360)Playground
true
isRGB(input: unknown): booleanValidates RGB objects (r: 0-255, g: 0-255, b: 0-255):
import { isRGB } from 'colorizr';
isRGB({ r: 255, g: 0, b: 68 }); // trueisRGB({ r: 255, g: 0, b: 68, alpha: 0.5 }); // trueisRGB({ r: 300, g: 0, b: 68 }); // false (r > 255)Playground
true
isLAB(input: unknown): booleanValidates OkLab objects (l: 0-1, a: -0.4 to 0.4, b: -0.4 to 0.4):
import { isLAB } from 'colorizr';
isLAB({ l: 0.63, a: 0.28, b: 0.06 }); // trueisLAB({ l: 2, a: 0, b: 0 }); // false (l > 1)Playground
true
isLCH(input: unknown): booleanValidates OkLCH objects (l: 0-1, c: 0-0.4, h: 0-360):
import { isLCH } from 'colorizr';
isLCH({ l: 0.63, c: 0.29, h: 17 }); // trueisLCH({ l: 0.63, c: 0.5, h: 17 }); // false (c > 0.4)Playground
true