Refactor validation

Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
2023-07-21 16:34:44 +03:00
parent b7dd1c77af
commit 3c303e0304
17 changed files with 256 additions and 151 deletions

View File

@@ -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,
};