apphouse
Version:
Component library for React that uses observable state management and theme-able components.
23 lines (19 loc) • 669 B
text/typescript
/**
* Checks if a string is a valid hex color.
* @param {string} color - The color string to validate.
* @returns {boolean} Returns true if the color string is a valid hex color, otherwise false.
*/
export function isHexColor(color: string): boolean {
// Check if the color starts with a hash (#) character
if (color.charAt(0) !== '#') {
return false;
}
// Remove the hash character from the color string
const hex = color.slice(1);
// Check if the color has 6 or 3 characters
if (hex.length !== 6 && hex.length !== 3) {
return false;
}
// Check if each character is a valid hexadecimal digit
return /^[0-9A-Fa-f]{3,6}$/.test(hex);
}