generated from kawarimidoll/deno-dev-template
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.ts
23 lines (18 loc) · 829 Bytes
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const defaultPixelColor = "eee";
const confirmHex = (str: string, defaultColor = defaultPixelColor) =>
/^#?([0-9a-f]{3}){1,2}$/i.test(str) ? str : defaultColor;
const convertToSixChars = (str: string, defaultColor = defaultPixelColor) =>
confirmHex(str, defaultColor).replace(
/^#?(.*)$/,
(_, hex) => (hex.length == 3) ? hex.replace(/./g, "$&$&") : hex,
);
const hexStrToRgbObj = (color: string, defaultColor = defaultPixelColor) =>
Object.fromEntries(
(convertToSixChars(color || defaultColor).match(/../g) ?? []).map((
c,
i,
) => ["rgb".charAt(i), parseInt("0x" + c)]),
);
const hexStrToHexNum = (color: string, defaultColor = defaultPixelColor) =>
parseInt("0x" + convertToSixChars(color || defaultColor));
export { confirmHex, convertToSixChars, hexStrToHexNum, hexStrToRgbObj };