fix: improve regex to check if string is hex or rgb

Signed-off-by: Chinmay D. Pai <chinmaydpai@gmail.com>
This commit is contained in:
Chinmay D. Pai 2020-03-23 11:29:53 +05:30
parent 90b684daf1
commit 2688662c82
No known key found for this signature in database
GPG Key ID: 75507BE256F40CED

View File

@ -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) => {