|
1 | 1 | /** |
2 | 2 | * Check if is browser env |
3 | 3 | */ |
4 | | -export const isBrowser: boolean = !!( |
5 | | - typeof window != "undefined" && window.document |
6 | | -) |
| 4 | +export function isBrowser(): boolean { |
| 5 | + return typeof window !== "undefined" |
| 6 | +} |
7 | 7 |
|
8 | 8 | /** |
9 | 9 | * Transform string to RGB |
10 | 10 | * @param str |
11 | 11 | */ |
12 | 12 | export function stringToRgb(str: string): [number, number, number] { |
13 | 13 | if (!str) return [128, 128, 128] |
14 | | - |
| 14 | + |
15 | 15 | // Add a salt to make numbers at the end produce different colors |
16 | | - const salt = 'x7f2q9'; |
17 | | - const stringToHash = str + salt; |
18 | | - |
| 16 | + const salt = "x7f2q9" |
| 17 | + const stringToHash = str + salt |
| 18 | + |
19 | 19 | let hash = 0 |
20 | 20 | for (let i = 0; i < stringToHash.length; i++) { |
21 | 21 | let character = stringToHash.charCodeAt(i) |
22 | 22 | hash = (hash << 5) - hash + character |
23 | 23 | hash = Math.abs(hash & hash) |
24 | 24 | } |
25 | | - |
| 25 | + |
26 | 26 | // Create more variance in the RGB values |
27 | 27 | const r = (hash & 0xff0000) >> 16 |
28 | 28 | const g = ((hash >> 3) & 0x00ff00) >> 8 |
29 | | - const b = ((hash >> 6) & 0x0000ff) |
30 | | - |
| 29 | + const b = (hash >> 6) & 0x0000ff |
| 30 | + |
31 | 31 | return [r, g, b] |
32 | 32 | } |
33 | 33 |
|
34 | 34 | /** |
35 | 35 | * ansi RGB |
36 | 36 | */ |
| 37 | +// Wraper for ansi 256 code |
| 38 | +const _wrapAnsi256 = (code) => `\u001B[${38};5;${code}m` |
| 39 | +// Convert RGB color to ansi 256 |
| 40 | +const _rgbToAnsi256 = (red: number, green: number, blue: number): number => { |
| 41 | + if (red === green && green === blue) { |
| 42 | + if (red < 8) return 16 |
| 43 | + if (red > 248) return 231 |
| 44 | + return Math.round(((red - 8) / 247) * 24) + 232 |
| 45 | + } |
| 46 | + return ( |
| 47 | + 16 + |
| 48 | + 36 * Math.round((red / 255) * 5) + |
| 49 | + 6 * Math.round((green / 255) * 5) + |
| 50 | + Math.round((blue / 255) * 5) |
| 51 | + ) |
| 52 | +} |
37 | 53 | export function ansiRgb(r: number, g: number, b: number) { |
38 | 54 | return function (str: string): string { |
39 | 55 | const _close = "\u001B[39m" |
40 | | - |
41 | | - /** |
42 | | - * Wrapper for ansi 256 code |
43 | | - * @param code |
44 | | - */ |
45 | | - const _wrapAnsi256 = (code) => `\u001B[${38};5;${code}m` |
46 | | - |
47 | | - /** |
48 | | - * Convert RGB color to ansi 256 |
49 | | - * @param red |
50 | | - * @param green |
51 | | - * @param blue |
52 | | - */ |
53 | | - const _rgbToAnsi256 = ( |
54 | | - red: number, |
55 | | - green: number, |
56 | | - blue: number |
57 | | - ): number => { |
58 | | - if (red === green && green === blue) { |
59 | | - if (red < 8) return 16 |
60 | | - if (red > 248) return 231 |
61 | | - return Math.round(((red - 8) / 247) * 24) + 232 |
62 | | - } |
63 | | - return ( |
64 | | - 16 + |
65 | | - 36 * Math.round((red / 255) * 5) + |
66 | | - 6 * Math.round((green / 255) * 5) + |
67 | | - Math.round((blue / 255) * 5) |
68 | | - ) |
69 | | - } |
70 | | - |
71 | 56 | return _wrapAnsi256(_rgbToAnsi256(r, g, b)) + str + _close |
72 | 57 | } |
73 | 58 | } |
0 commit comments