Refactor validation
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
@@ -23,10 +23,6 @@ const isRgbColor = (color: string) => {
|
||||
return /^rgb\((\d{1,3}), ?(\d{1,3}), ?(\d{1,3})\)$/i.test(color);
|
||||
};
|
||||
|
||||
const isValidColor = (color: string) => {
|
||||
return isHexColor(color) || isRgbColor(color);
|
||||
};
|
||||
|
||||
const rgbToHex = (rgb: string) => {
|
||||
const [r, g, b] = rgb
|
||||
.replaceAll(/[^\d,]/g, '')
|
||||
@@ -37,10 +33,22 @@ const rgbToHex = (rgb: string) => {
|
||||
};
|
||||
|
||||
const generateRandomColor = () => {
|
||||
const r = Math.floor(Math.random() * 256).toString(16).padStart(2, '0');
|
||||
const g = Math.floor(Math.random() * 256).toString(16).padStart(2, '0');
|
||||
const b = Math.floor(Math.random() * 256).toString(16).padStart(2, '0');
|
||||
const r = Math.floor(Math.random() * 256)
|
||||
.toString(16)
|
||||
.padStart(2, '0');
|
||||
const g = Math.floor(Math.random() * 256)
|
||||
.toString(16)
|
||||
.padStart(2, '0');
|
||||
const b = Math.floor(Math.random() * 256)
|
||||
.toString(16)
|
||||
.padStart(2, '0');
|
||||
return `#${r}${g}${b}`;
|
||||
};
|
||||
|
||||
export { getContrastColor, isHexColor, isRgbColor, isValidColor, rgbToHex, generateRandomColor };
|
||||
export {
|
||||
getContrastColor,
|
||||
isHexColor,
|
||||
isRgbColor,
|
||||
rgbToHex,
|
||||
generateRandomColor,
|
||||
};
|
||||
|
Reference in New Issue
Block a user