apphouse
Version:
Component library for React that uses observable state management and theme-able components.
33 lines (27 loc) • 885 B
text/typescript
/**
* Checks if a string is a valid RGBA color.
* @param {string} color - The color string to validate.
* @returns {boolean} Returns true if the color string is a valid RGBA color, otherwise false.
*/
export function isRgbaColor(color: string): boolean {
// Check if the color starts with 'rgba(' and ends with ')'
if (!color.startsWith('rgba(') || !color.endsWith(')')) {
return false;
}
// Remove 'rgba(' and ')' from the color string
const rgba = color.slice(5, -1);
// Split the color string by commas
const values = rgba.split(',');
// Check if the color has exactly 4 values
if (values.length !== 4) {
return false;
}
// Check if each value is a valid number between 0 and 255
for (const value of values) {
const num = Number(value.trim());
if (isNaN(num) || num < 0 || num > 255) {
return false;
}
}
return true;
}