blob: aa939f71cd5d16a0c9b1c5fb79d87372162f7643 (
plain)
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
|
// TODO: Standardise this shit server-side
export function formatHex(hex: string, poundSign = true): string {
if (hex.length === 6) {
return (poundSign ? "#" : "") + hex;
} else if (hex.length === 3) {
return (
(poundSign ? "#" : "") +
hex
.split("")
.map((c) => c + c)
.join("")
);
} else if (hex.length === 7 && hex.startsWith("#")) {
return poundSign ? hex : hex.substring(1);
} else if (hex.length === 4 && hex.startsWith("#")) {
return poundSign
? hex
: hex
.substring(1)
.split("")
.map((c) => c + c)
.join("");
} else {
throw new Error("Invalid hex color format");
}
}
|