forked from microsoft/fluentui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.ts
31 lines (25 loc) · 798 Bytes
/
color.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
30
31
import { ColorRGBA64, parseColorHexRGB, parseColorWebRGB } from '@microsoft/fast-colors';
const cache = new Map();
/**
* Converts a color string into a ColorRGBA64 instance.
* Supports #RRGGBB and rgb(r, g, b) formats
*
* @public
*/
export function parseColorString(color: string): ColorRGBA64 {
const cached: ColorRGBA64 | void = cache.get(color);
if (!cached) {
let parsed: ColorRGBA64 | null = parseColorHexRGB(color);
if (parsed === null) {
parsed = parseColorWebRGB(color);
}
if (parsed === null) {
throw new Error(
`${color} cannot be converted to a ColorRGBA64. Color strings must be one of the following formats: "#RGB", "#RRGGBB", or "rgb(r, g, b)"`,
);
}
cache.set(color, parsed);
return parsed;
}
return cached;
}