/
parseColor.ts
29 lines (27 loc) · 1.32 KB
/
parseColor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const HEX3_REGEX = /^#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])$/;
const HEX6_REGEX = /^#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})$/;
const RGB_REGEX = /^rgb\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*\)$/;
const RGBA_REGEX = /^rgba\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*\)$/;
/**
* Parse color string to r/g/b value.
* If the given color is not in a recognized format, return null
*/
export default function parseColor(color: string): [number, number, number] | null {
color = (color || '').trim();
let match: RegExpMatchArray | null;
if ((match = color.match(HEX3_REGEX))) {
return [
parseInt(match[1] + match[1], 16),
parseInt(match[2] + match[2], 16),
parseInt(match[3] + match[3], 16),
];
} else if ((match = color.match(HEX6_REGEX))) {
return [parseInt(match[1], 16), parseInt(match[2], 16), parseInt(match[3], 16)];
} else if ((match = color.match(RGB_REGEX) || color.match(RGBA_REGEX))) {
return [parseInt(match[1]), parseInt(match[2]), parseInt(match[3])];
} else {
// CSS color names such as red, green is not included for now.
// If need, we can add those colors from https://www.w3.org/wiki/CSS/Properties/color/keywords
return null;
}
}