-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolors.ts
99 lines (95 loc) · 1.5 KB
/
colors.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// https://github.com/debug-js/debug/blob/4.3.7/src/browser.js
const colors = [
0x0000CC,
0x0000FF,
0x0033CC,
0x0033FF,
0x0066CC,
0x0066FF,
0x0099CC,
0x0099FF,
0x00CC00,
0x00CC33,
0x00CC66,
0x00CC99,
0x00CCCC,
0x00CCFF,
0x3300CC,
0x3300FF,
0x3333CC,
0x3333FF,
0x3366CC,
0x3366FF,
0x3399CC,
0x3399FF,
0x33CC00,
0x33CC33,
0x33CC66,
0x33CC99,
0x33CCCC,
0x33CCFF,
0x6600CC,
0x6600FF,
0x6633CC,
0x6633FF,
0x66CC00,
0x66CC33,
0x9900CC,
0x9900FF,
0x9933CC,
0x9933FF,
0x99CC00,
0x99CC33,
0xCC0000,
0xCC0033,
0xCC0066,
0xCC0099,
0xCC00CC,
0xCC00FF,
0xCC3300,
0xCC3333,
0xCC3366,
0xCC3399,
0xCC33CC,
0xCC33FF,
0xCC6600,
0xCC6633,
0xCC9900,
0xCC9933,
0xCCCC00,
0xCCCC33,
0xFF0000,
0xFF0033,
0xFF0066,
0xFF0099,
0xFF00CC,
0xFF00FF,
0xFF3300,
0xFF3333,
0xFF3366,
0xFF3399,
0xFF33CC,
0xFF33FF,
0xFF6600,
0xFF6633,
0xFF9900,
0xFF9933,
0xFFCC00,
0xFFCC33,
];
// https://github.com/debug-js/debug/blob/4.3.4/src/common.js#L41
export function selectColor(ns: string) {
let hash = 0;
for (let i = 0; i < ns.length; i++) {
hash = ((hash << 5) - hash) + ns.charCodeAt(i);
hash |= 0; // Convert to 32bit integer
}
return colors[Math.abs(hash) % colors.length];
}
export const colorNamespace = (ns: string) => {
const color = selectColor(ns);
const r = color >> 16;
const g = color >> 8 & 0xFF;
const b = color & 0xFF;
return `\x1b[38;2;${r};${g};${b}m${ns}\x1b[1;0m`;
};