-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
466 lines (428 loc) · 10.1 KB
/
utils.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
const crypto = require("crypto")
const { createCanvas } = require("canvas")
const seedrandom = require("seedrandom")
// Arrays of adjectives, nouns, and verbs
const adjectives = [
"red",
"green",
"blue",
"orange",
"purple",
"black",
"white",
"yellow",
"light",
"dark",
"clear",
"hard",
"soft",
"smooth",
"rough",
"solid",
"liquid",
"shy",
"confident",
"happy",
"sad",
"angry",
"suprised",
"excited",
"bored",
"tired",
"average",
"modern",
"old",
"secret",
"normal",
"spicy",
"sweet",
"salty",
"sour",
"creamy",
"chewy",
"easy",
"difficult",
"stubborn",
"quiet",
"loud",
"crazy",
"awful",
"many",
"few",
"several",
"married",
"friendly",
"pretty",
"ugly",
"crunchy",
"empty",
"partial",
"colorful",
"bland",
"smart",
"complex",
"simple",
"short",
"long",
"wide",
"thin",
"smelly",
]
const nouns = [
"nights",
"houses",
"bones",
"animals",
"thoughts",
"hoses",
"songs",
"rays",
"puddles",
"pipes",
"bubbles",
"squirrels",
"balls",
"rakes",
"pots",
"grapes",
"papers",
"sticks",
"winds",
"donkeys",
"turkeys",
"tomatoes",
"helmets",
"eyes",
"marbles",
"cars",
"flasks",
"wires",
"rails",
"pens",
"umbrellas",
"sheep",
"boards",
"numbers",
"crackers",
"planets",
"mittens",
"guitars",
"dogs",
"planks",
"coins",
"collars",
"chairs",
"teeth",
"shirts",
"trains",
"candles",
"windows",
"spheres",
"tents",
"pianos",
"keyboards",
"koalas",
"shoes",
"dice",
"plants",
"flutes",
"leaves",
"soaps",
"plates",
"cubs",
"pigs",
"crystals",
"cursors",
]
const verbs = [
"allow",
"jam",
"record",
"grate",
"note",
"tickle",
"answer",
"punch",
"snatch",
"tease",
"wrap",
"rub",
"float",
"mix",
"strap",
"wreck",
"slap",
"support",
"sprinkle",
"return",
"possess",
"decay",
"claim",
"paste",
"see",
"drown",
"mend",
"love",
"leave",
"marry",
"grow",
"guide",
"supply",
"drop",
"explain",
"twist",
"paddle",
"cure",
"fold",
"bless",
"wriggle",
"mock",
"tire",
"order",
"accept",
"strip",
"arrest",
"end",
"attempt",
"trip",
"grease",
"sneeze",
"lighten",
"stamp",
"drag",
"mark",
"shrug",
"flap",
"scorch",
"squash",
"work",
"slide",
"earn",
"hug",
]
const hexString = (s) => {
return crypto.createHash("sha256").update(s).digest("hex")
}
const generateUsername = (userId) => {
const randomString = hexString(userId)
const rng = seedrandom(randomString)
// Function to pick a random word from an array
const getRandomWord = (array) => {
const randomIndex = getRandomInt(0, 63, rng)
return array[randomIndex]
}
return `${getRandomWord(adjectives)}-${getRandomWord(
nouns
)}-${getRandomWord(verbs)}-${getRandomWord(adjectives)}-${getRandomWord(
nouns
)}`
}
// Helper function to generate a random integer in a range
const getRandomInt = (min, max, rng) => {
return Math.floor(rng() * (max - min + 1)) + min
}
// Helper-helper function to convert HSL to RGB
const hslToRgb = (h, s, l) => {
let r, g, b
if (s === 0) {
r = g = b = l // achromatic
} else {
const hue2rgb = (p, q, t) => {
if (t < 0) t += 1
if (t > 1) t -= 1
if (t < 1 / 6) return p + (q - p) * 6 * t
if (t < 1 / 2) return q
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6
return p
}
const q = l < 0.5 ? l * (1 + s) : l + s - l * s
const p = 2 * l - q
r = hue2rgb(p, q, h + 1 / 3)
g = hue2rgb(p, q, h)
b = hue2rgb(p, q, h - 1 / 3)
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)]
}
// Helper function to generate random colors
const generateColor = (rng) => {
const r = getRandomInt(0, 255, rng)
const g = getRandomInt(0, 255, rng)
const b = getRandomInt(0, 255, rng)
// Convert to hex
const hex = `#${((1 << 24) | (r << 16) | (g << 8) | b)
.toString(16)
.slice(1)}`
return hex
}
// Helper function to generate less saturated random colors
const generateBgColor = (rng) => {
const h = getRandomInt(0, 360, rng) // Random hue
const s = getRandomInt(30, 70, rng) // Saturation constrained between 30 and 70
const l = getRandomInt(60, 80, rng) // Lightness constrained between 60 and 80
// Convert HSL to RGB
const [r, g, b] = hslToRgb(h / 360, s / 100, l / 100)
// Convert to hex
const hex = `#${((1 << 24) | (r << 16) | (g << 8) | b)
.toString(16)
.slice(1)}`
return hex
}
// Helper function to interpolate between colors
function interpolateColors(color1, color2, factor) {
// Convert hex to RGB
const r1 = parseInt(color1.slice(1, 3), 16)
const g1 = parseInt(color1.slice(3, 5), 16)
const b1 = parseInt(color1.slice(5, 7), 16)
const r2 = parseInt(color2.slice(1, 3), 16)
const g2 = parseInt(color2.slice(3, 5), 16)
const b2 = parseInt(color2.slice(5, 7), 16)
// Interpolate RGB values
const r = Math.round(r1 + (r2 - r1) * factor)
const g = Math.round(g1 + (g2 - g1) * factor)
const b = Math.round(b1 + (b2 - b1) * factor)
// Convert back to hex
const hex = `#${((1 << 24) | (r << 16) | (g << 8) | b)
.toString(16)
.slice(1)}`
return hex
}
// Function to generate a random toucan avatar based on the userId
const generateAvatar = (userId, width) => {
const randomString = hexString(userId)
const rng = seedrandom(randomString)
// Canvas dimensions
const canvasSize = width
const padding = Math.ceil(width * 0.177)
const squareSize = (canvasSize - padding * 2) / 3
const radius = squareSize
// Colors to use
const bgColor = generateBgColor(rng)
const bodyColor = generateColor(rng)
const innerBodyColor = interpolateColors("#ffffff", bodyColor, 0.3)
const colors = Array.from({ length: 5 }, (_) => generateColor(rng))
const canvas = createCanvas(canvasSize, canvasSize)
const context = canvas.getContext("2d")
// Background color
context.fillStyle = bgColor
context.fillRect(0, 0, canvasSize, canvasSize)
// Draw quarter circles and square
const centerX = canvasSize / 2
const centerY = canvasSize / 2
// Draw head as quarter circles
context.beginPath()
context.fillStyle = bodyColor
context.moveTo(centerX - squareSize / 2, centerY - squareSize / 2)
context.arc(
centerX - squareSize / 2,
centerY - squareSize / 2,
radius,
Math.PI,
Math.PI * 1.5,
false
)
context.fill()
context.beginPath()
context.fillStyle = "#ffffff"
context.moveTo(centerX - squareSize / 2, centerY - squareSize / 2)
context.arc(
centerX - squareSize / 2,
centerY - squareSize / 2,
radius * 0.707,
Math.PI,
Math.PI * 1.5,
false
)
context.fill()
// Draw eye as circle
context.beginPath()
context.fillStyle = "#000000"
context.moveTo(
centerX - squareSize / 2 - radius * 0.3,
centerY - squareSize / 2 - radius * 0.3
)
context.arc(
centerX - squareSize / 2 - radius * 0.3,
centerY - squareSize / 2 - radius * 0.3,
radius * 0.125,
0,
Math.PI * 2.0,
false
)
context.fill()
// Draw colored beak
for (let i = 0; i < 4; i++) {
const x = centerX - squareSize / 2 + i * (squareSize / 4)
const y = centerY - squareSize / 2 - squareSize
context.fillStyle = colors[i]
context.fillRect(x, y, squareSize / 4, squareSize)
}
context.beginPath()
context.fillStyle = colors[4]
context.moveTo(centerX + squareSize / 2, centerY - squareSize / 2)
context.arc(
centerX + squareSize / 2,
centerY - squareSize / 2,
radius,
Math.PI * 1.5,
Math.PI * 2.0,
false
)
context.fill()
// Draw body as quarter circles
context.beginPath()
context.fillStyle = bodyColor
context.moveTo(centerX - squareSize / 2, centerY - squareSize / 2)
context.arc(
centerX - squareSize / 2,
centerY - squareSize / 2,
radius,
Math.PI * 0.5,
Math.PI * 1.0,
false
)
context.fill()
context.beginPath()
context.fillStyle = innerBodyColor
context.moveTo(centerX - squareSize / 2, centerY - squareSize / 2)
context.arc(
centerX - squareSize / 2,
centerY - squareSize / 2,
radius * 0.707,
Math.PI * 0.5,
Math.PI * 1.0,
false
)
context.fill()
context.beginPath()
context.fillStyle = bodyColor
context.moveTo(centerX - squareSize / 2, centerY + squareSize / 2)
context.arc(
centerX - squareSize / 2,
centerY + squareSize / 2,
radius,
Math.PI * 1.5,
Math.PI * 2.0,
false
)
context.fill()
// Draw tail as triangle
context.beginPath()
context.moveTo(centerX + squareSize / 2, centerY + squareSize / 2)
context.lineTo(centerX + squareSize / 2, centerY + (squareSize * 3) / 2)
context.lineTo(centerX - squareSize / 2, centerY + (squareSize * 3) / 2)
context.fillStyle = bodyColor // Set the color of the triangle
context.fill()
// Draw branch as line
context.beginPath()
context.moveTo(0, centerY + squareSize / 2 + squareSize / 8)
context.lineTo(canvasSize, centerY + squareSize / 2 + squareSize / 8)
context.strokeStyle = generateColor(rng)
context.lineWidth = squareSize / 4
context.stroke()
// Save the canvas as a PNG file
return canvas.toBuffer("image/png")
}
module.exports = {
generateUsername,
generateAvatar,
}