-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathlibs.js
150 lines (137 loc) · 4.04 KB
/
libs.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import fs from 'fs';
import path from 'path';
import sharp from 'sharp';
import Color from 'color';
import svgRoundCorners from 'svg-round-corners';
const { roundCommands } = svgRoundCorners;
export const getFlagSVGByCountryCode = (countryCode) => {
const chars = countryCode.split('');
const hexEmojiChars = chars.map((char) => (char.charCodeAt(0) + 127397).toString(16));
const fileName = hexEmojiChars.join('-');
const filePath = path.join(process.cwd(), `/assets/flags/${fileName}.svg`);
if (!fs.existsSync(filePath)) {
filePath = path.join(process.cwd(), `/assets/flags/1f1fd-1f1fd.svg`);
}
const flagSVG = fs.readFileSync(filePath, 'utf8');
return flagSVG;
};
export const getPlaymodeSVG = (playmode) => {
const filePath = path.join(process.cwd(), `/assets/modes/${playmode}.svg`);
const playmodeSVG = fs.readFileSync(filePath, 'utf8');
return playmodeSVG;
};
export const getPlaymodeFullName = (playmode) => {
const names = {
std: 'osu!',
catch: 'osu!catch',
mania: 'osu!mania',
taiko: 'osu!taiko'
};
return names[playmode];
};
/**
* @param {number} number
* @param {string} prefix
*/
export const formatNumber = (number, prefix = '') => {
if (number == null) return '-';
return `${prefix}${number.toLocaleString('en-GB')}`;
};
export const formatPlaytime = (playtime) => {
const days = Math.floor(playtime / 86400);
const hours = Math.floor((playtime % 86400) / 3600);
const minutes = Math.floor((playtime % 3600) / 60);
if (days > 0) {
return `${days}d ${hours}h ${minutes}m`;
}
return `${hours}h ${minutes}m`;
};
export const getResizedCoverBase64 = async (img, w, h, blur = 0, flop = false) => {
blur = Math.min(blur, 100);
const image = sharp(img, {failOnError: false}).resize(parseInt(w * 1.5), parseInt(h * 1.5));
if (blur >= 0.5 && blur <= 100) image.blur(blur);
if (flop) image.flop();
return image.toBuffer().then((data) => 'data:image/png;base64,' + data.toString('base64'));
};
export const getHexagonPath = (origX, origY, radius, borderRadius = 2) => {
let outerPathCommands = [];
for (let i = 0; i <= 6; i++) {
const angle = (-120 + i * 60) / 180 * Math.PI;
const x = origX + Math.cos(angle) * radius;
const y = origY + Math.sin(angle) * radius;
outerPathCommands.push({
marker: 'L',
values: { x, y }
});
}
outerPathCommands[0].marker = 'M';
outerPathCommands.push({
marker: 'Z',
values: { x: outerPathCommands[0].values.x, y: outerPathCommands[0].values.x }
});
const path = roundCommands(outerPathCommands, borderRadius);
return path.path;
}
export const getColorBySkillRankName = (skillRankName) => {
const colors = {
Hardy: '#464ac1',
Tenacious: '#ff0066',
Swift: '#fcc013',
Perceptive: '#24d8fe',
Volcanic: '#ef525b',
Furious: '#f8095c',
Sturdy: '#1bad58',
Adventurous: '#79de4f',
Adamant: '#4dceff',
Spirited: '#d0dc05',
Berserk: '#b00106',
Fearless: '#a8157d',
Frantic: '#468c00',
Volatile: '#dc4ad2',
Versatile: '#e9ce14',
Ambitious: '#46d1a7',
Sage: '#1baec0',
Sharpshooter: '#9b1400',
Psychic: '#66d9b7',
Pirate: '#d90606',
Seer: '#1368bd',
Sniper: '#519216',
Daredevil: '#c01900'
}
return colors[skillRankName];
}
export const getSkillNameI18n = (skillName, lang = 'en') => {
skillName = skillName.toLowerCase();
if (lang == 'en') {
return skillName[0].toUpperCase() + skillName.substring(1);
}
const i18n = {
cn: {
"stamina": "耐力",
"tenacity": "韧性",
"agility": "敏捷",
"accuracy": "准度",
"precision": "瞄准",
"reaction": "反应",
"memory": "记忆"
}
}
if (!i18n[lang] || !i18n[lang][skillName]) {
return skillName;
}
return i18n[lang][skillName];
}
export const calcWCAGColorContrast = (color1, color2) => {
const calcLuminance = (color) => {
let [r, g, b] = color.rgb().color.map((c) => c / 255);
[r, g, b] = [r, g, b].map((c) => {
if (c <= 0.03928) {
return c / 12.92;
}
return Math.pow((c + 0.055) / 1.055, 2.4);
});
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
let contrast = (calcLuminance(color1) + 0.05) / (calcLuminance(color2) + 0.05);
return Math.max(contrast, 1 / contrast);
}