MCPcopy Index your code
hub / github.com/ChartGPU/ChartGPU / parseHexColorToRgba01

Function parseHexColorToRgba01

src/utils/colors.ts:16–57  ·  view source on GitHub ↗
(color: string)

Source from the content-addressed store, hash-verified

14};
15
16const parseHexColorToRgba01 = (color: string): Rgba01 | null => {
17 const c = color.trim();
18 if (!c.startsWith('#')) return null;
19
20 const hex = c.slice(1);
21
22 // #rgb
23 if (hex.length === 3) {
24 const r = parseHexNibble(hex[0]);
25 const g = parseHexNibble(hex[1]);
26 const b = parseHexNibble(hex[2]);
27 return [(r * 17) / 255, (g * 17) / 255, (b * 17) / 255, 1];
28 }
29
30 // #rgba
31 if (hex.length === 4) {
32 const r = parseHexNibble(hex[0]);
33 const g = parseHexNibble(hex[1]);
34 const b = parseHexNibble(hex[2]);
35 const a = parseHexNibble(hex[3]);
36 return [(r * 17) / 255, (g * 17) / 255, (b * 17) / 255, (a * 17) / 255];
37 }
38
39 // #rrggbb
40 if (hex.length === 6) {
41 const r = parseHexByte(hex.slice(0, 2));
42 const g = parseHexByte(hex.slice(2, 4));
43 const b = parseHexByte(hex.slice(4, 6));
44 return [r / 255, g / 255, b / 255, 1];
45 }
46
47 // #rrggbbaa
48 if (hex.length === 8) {
49 const r = parseHexByte(hex.slice(0, 2));
50 const g = parseHexByte(hex.slice(2, 4));
51 const b = parseHexByte(hex.slice(4, 6));
52 const a = parseHexByte(hex.slice(6, 8));
53 return [r / 255, g / 255, b / 255, a / 255];
54 }
55
56 return null;
57};
58
59const parseRgbNumberOrPercent = (token: string): number | null => {
60 const t = token.trim();

Callers 1

parseCssColorToRgba01Function · 0.85

Calls 2

parseHexNibbleFunction · 0.85
parseHexByteFunction · 0.85

Tested by

no test coverage detected