From 2688662c82acc3d94ffe786a94700b155d097c35 Mon Sep 17 00:00:00 2001 From: "Chinmay D. Pai" Date: Mon, 23 Mar 2020 11:29:53 +0530 Subject: [PATCH] fix: improve regex to check if string is hex or rgb Signed-off-by: Chinmay D. Pai --- src/js/utils/colors.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/js/utils/colors.js b/src/js/utils/colors.js index b78fe5f..8f4275c 100644 --- a/src/js/utils/colors.js +++ b/src/js/utils/colors.js @@ -36,8 +36,10 @@ export function lightenDarkenColor(color, amt) { } export function isValidColor(string) { - // https://stackoverflow.com/a/8027444/6495043 - return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(string); + // https://stackoverflow.com/a/32685393 + let HEX_RE = /^(#)((?:[A-Fa-f0-9]{3}){1,2})$/i + let RGB_RE = /^(rgb|hsl)(a?)[(]\s*([\d.]+\s*%?)\s*,\s*([\d.]+\s*%?)\s*,\s*([\d.]+\s*%?)\s*(?:,\s*([\d.]+)\s*)?[)]$/i + return HEX_RE.test(string) || RGB_RE.test(string); } export const getColor = (color) => {