forked from birdflop/web
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
413 lines (384 loc) · 13.1 KB
/
Copy pathscript.js
File metadata and controls
413 lines (384 loc) · 13.1 KB
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
// elements for obtaining vals
const nickName = document.getElementById('nickname');
const coloredNick = document.getElementById('coloredNick');
const savedColors = ['FBCD2C', 'F8372B', getRandomHexColor(), getRandomHexColor(), getRandomHexColor(), getRandomHexColor(), getRandomHexColor(), getRandomHexColor(), getRandomHexColor(), getRandomHexColor()];
const presets = {
1: {
colors: ["FF0000", "FF7F00", "FFFF00", "00FF00", "0000FF", "4B0082", "9400D3"],
},
2: {
colors: ["#F53053", "#F54379", "#F56A83", "#F580A3"],
},
3: {
colors: ["#F9CEEE", "#E0CDFF", "#C1F0FB", "#DCF9A8", "#FFEBAF"],
},
4: {
colors: ["#FF4B4B", "#FF9534", "#FFF84E", "#B2FF5A", "#25CBFF", "#863CFF"],
},
5: {
colors: ["#FFB9C5", "#FFD5D2", "#D6E792", "#B9E284", "#89C356"],
},
6: {
colors: ["#F9881F", "#F99A42", "#B9638C", "#8958AF", "#6C42B1"],
},
7: {
colors: ["#58D7FF", "#A4E4FF", "#D9F1FF", "#EEF9FF"],
},
8: {
colors: ["#FF5555", "#FFAEAE", "#FFEAE9", "#FFFFFF"],
},
9: {
colors: ["#82D439", "#D4F8B4"],
},
10: {
colors: ["#0ea3e5", "#C4F2FA"],
},
11: {
colors: ["#C177FB", "#D0C3F7"],
},
12: {
colors: ["#eeb915", "#F7F2A9"],
},
}
const formats = {
0: {
outputPrefix: '',
template: '&#$1$2$3$4$5$6$f$c',
formatChar: '&',
maxLength: 256
},
1: {
outputPrefix: '',
template: '<#$1$2$3$4$5$6>$f$c',
formatChar: '&',
maxLength: 256
},
2: {
outputPrefix: '',
template: '&x&$1&$2&$3&$4&$5&$6$f$c',
formatChar: '&',
maxLength: 256
},
3: {
outputPrefix: '/nick ',
template: '&#$1$2$3$4$5$6$f$c',
formatChar: '&',
maxLength: 256
},
4: {
outputPrefix: '/nick ',
template: '<#$1$2$3$4$5$6>$f$c',
formatChar: '&',
maxLength: 256
},
5: {
outputPrefix: '/nick ',
template: '&x&$1&$2&$3&$4&$5&$6$f$c',
formatChar: '&',
maxLength: 256
},
6: {
outputPrefix: '',
template: '§x§$1§$2§$3§$4§$5§$6$f$c',
formatChar: '§',
maxLength: null
},
7: {
outputPrefix: '',
template: '[COLOR=#$1$2$3$4$5$6]$c[/COLOR]',
formatChar: null,
maxLength: null
},
8: {
outputPrefix: '',
template: '\\u00A7x\\u00A7$1\\u00A7$2\\u00A7$3\\u00A7$4\\u00A7$5\\u00A7$6$c',
formatChar: null,
maxLength: null
},
9: {
outputPrefix: '',
template: '#$1$2$3$4$5$6$f$c',
formatChar: '&',
maxLength: 256
},
};
function darkMode() {
if (document.getElementById('darkmode').checked == true) {
document.body.classList.add('dark');
document.getElementById('output-format').classList.add("dark");
document.getElementById('color-preset').classList.add("dark");
document.getElementById('numOfColors').classList.add("dark");
document.getElementById('graylabel1').classList.replace("gray", "darkgray");
document.getElementById('graylabel2').classList.replace("gray", "darkgray");
document.getElementById('outputText').classList.replace("gray", "darkgray");
document.getElementById('outputText').classList.replace("gray", "darkgray");
document.getElementById('error').classList.replace("errortext", "darkerrortext");
document.getElementById('numOfColors').classList.add("darktextboxes");
document.getElementById('nickname').classList.add("darktextboxes");
document.getElementById('outputText').classList.add("darktextboxes");
Array.from(document.getElementsByClassName("hexColor")).forEach(e => {
document.getElementById(e.id).classList.add("darktextboxes");
})
} else {
document.body.classList.remove('dark');
document.getElementById('output-format').classList.remove("dark");
document.getElementById('color-preset').classList.remove("dark");
document.getElementById('numOfColors').classList.remove("dark");
document.getElementById('graylabel1').classList.replace("darkgray", "gray");
document.getElementById('graylabel2').classList.replace("darkgray", "gray");
document.getElementById('outputText').classList.replace("darkgray", "gray");
document.getElementById('error').classList.replace("darkerrortext", "errortext");
document.getElementById('numOfColors').classList.remove("darktextboxes");
document.getElementById('nickname').classList.remove("darktextboxes");
document.getElementById('outputText').classList.remove("darktextboxes");
Array.from(document.getElementsByClassName("hexColor")).forEach(e => {
document.getElementById(e.id).classList.remove("darktextboxes");
})
}
}
/* Get a random HEX color */
function getRandomHexColor() {
return Math.floor(Math.random()*16777215).toString(16).toUpperCase();
}
/*
Copies contents to clipboard
function copyTextToClipboard(text) {
let textArea = document.createElement('textarea');
textArea.value = text;
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
document.execCommand('copy');
alert('Copied output!');
document.body.removeChild(textArea);
}
*/
function showError(show) {
if (show) {
document.getElementById('error').style.display = 'block';
document.getElementById('outputText').style.height = '70px';
document.getElementById('outputText').style.marginBottom = '5px';
} else {
document.getElementById('error').style.display = 'none';
document.getElementById('outputText').style.height = '95px';
document.getElementById('outputText').style.marginBottom = '10px';
}
}
function hex(c) {
let s = '0123456789abcdef';
let i = parseInt(c);
if (i == 0 || isNaN(c))
return '00';
i = Math.round(Math.min(Math.max(0, i), 255));
return s.charAt((i - i % 16) / 16) + s.charAt(i % 16);
}
/* Convert an RGB triplet to a hex string */
function convertToHex(rgb) {
return hex(rgb[0]) + hex(rgb[1]) + hex(rgb[2]);
}
/* Remove '#' in color hex string */
function trim(s) {
return (s.charAt(0) == '#') ? s.substring(1, 7) : s
}
/* Convert a hex string to an RGB triplet */
function convertToRGB(hex) {
let color = [];
color[0] = parseInt((trim(hex)).substring(0, 2), 16);
color[1] = parseInt((trim(hex)).substring(2, 4), 16);
color[2] = parseInt((trim(hex)).substring(4, 6), 16);
return color;
}
/**
* JavaScript implementation of HexUtils Gradients from RoseGarden.
* https://github.com/Rosewood-Development/RoseGarden/blob/master/src/main/java/dev/rosewood/rosegarden/utils/HexUtils.java#L358
*/
class Gradient {
constructor(colors, numSteps) {
this.colors = colors;
this.gradients = [];
this.steps = numSteps - 1;
this.step = 0;
const increment = this.steps / (colors.length - 1);
for (let i = 0; i < colors.length - 1; i++)
this.gradients.push(new TwoStopGradient(colors[i], colors[i + 1], increment * i, increment * (i + 1)));
}
/* Gets the next color in the gradient sequence as an array of 3 numbers: [r, g, b] */
next() {
if (this.steps <= 1)
return this.colors[0];
const adjustedStep = Math.round(Math.abs(((2 * Math.asin(Math.sin(this.step * (Math.PI / (2 * this.steps))))) / Math.PI) * this.steps));
let color;
if (this.gradients.length < 2) {
color = this.gradients[0].colorAt(adjustedStep);
} else {
const segment = this.steps / this.gradients.length;
const index = Math.min(Math.floor(adjustedStep / segment), this.gradients.length - 1);
color = this.gradients[index].colorAt(adjustedStep);
}
this.step++;
return color;
}
}
class TwoStopGradient {
constructor(startColor, endColor, lowerRange, upperRange) {
this.startColor = startColor;
this.endColor = endColor;
this.lowerRange = lowerRange;
this.upperRange = upperRange;
}
colorAt(step) {
return [
this.calculateHexPiece(step, this.startColor[0], this.endColor[0]),
this.calculateHexPiece(step, this.startColor[1], this.endColor[1]),
this.calculateHexPiece(step, this.startColor[2], this.endColor[2])
];
}
calculateHexPiece(step, channelStart, channelEnd) {
const range = this.upperRange - this.lowerRange;
const interval = (channelEnd - channelStart) / range;
return Math.round(interval * (step - this.lowerRange) + channelStart);
}
}
/* Toggles the number of gradient colors between 2 and 10 based on user input */
function toggleColors(colors) {
let clamped = Math.min(10, Math.max(2, colors));
if (colors == 1 || colors == '') {
colors = getColors().length;
} else if (colors != clamped) {
$('#numOfColors').val(clamped);
colors = clamped;
}
const container = $('#hexColors');
const hexColors = container.find('.hexColor');
const number = hexColors.size();
if (number > colors) {
// Need to remove some colors
hexColors.each((index, element) => {
if (index + 1 > colors) {
savedColors[index] = $(element).val();
$(element).parent().remove();
}
});
} else if (number < colors) {
// Need to add some colors
let template = $('#hexColorTemplate').html();
for (let i = number + 1; i <= colors; i++) {
let html = template.replace(/\$NUM/g, i).replace(/\$VAL/g, savedColors[i - 1]);
container.append(html);
}
jscolor.install(); // Refresh all jscolor elements
}
}
/* Gets all colored entered by the user */
function getColors() {
const hexColors = $('#hexColors').find('.hexColor');
const colors = [];
hexColors.each((index, element) => {
const value = $(element).val();
savedColors[index] = value;
colors[index] = convertToRGB(value);
});
return colors;
}
function updateOutputText(event) {
let format = formats[document.getElementById('output-format').value];
if (format.outputPrefix) {
nickName.value = nickName.value.replace(/ /g, '');
if (nickName.value) {
let letters = /^[0-9a-zA-Z_]+$/;
if (!nickName.value.match(letters)) nickName.value = nickName.value.replace(event.data, '');
if (!nickName.value.match(letters)) nickName.value = 'birdflop.com';
}
}
let newNick = nickName.value
if (!newNick) {
newNick = 'Type something!'
}
const bold = document.getElementById('bold').checked;
const italic = document.getElementById('italics').checked;
const underline = document.getElementById('underline').checked;
const strike = document.getElementById('strike').checked;
let outputText = document.getElementById('outputText');
let gradient = new Gradient(getColors(), newNick.replace(/ /g, '').length);
let charColors = [];
let output = format.outputPrefix;
for (let i = 0; i < newNick.length; i++) {
let char = newNick.charAt(i);
if (char == ' ') {
output += char;
charColors.push(null);
continue;
}
let hex = convertToHex(gradient.next());
charColors.push(hex);
let hexOutput = format.template;
for (let n = 1; n <= 6; n++)
hexOutput = hexOutput.replace(`$${n}`, hex.charAt(n - 1));
let formatCodes = '';
if (format.formatChar != null) {
if (bold) formatCodes += format.formatChar + 'l';
if (italic) formatCodes += format.formatChar + 'o';
if (underline) formatCodes += format.formatChar + 'n';
if (strike) formatCodes += format.formatChar + 'm';
}
hexOutput = hexOutput.replace('$f', formatCodes);
hexOutput = hexOutput.replace('$c', char);
output += hexOutput;
}
outputText.innerText = output;
showError(format.maxLength != null && format.maxLength < output.length);
displayColoredName(newNick, charColors);
}
/**
* padding function:
* cba to roll my own, thanks Pointy!
* ==================================
* source: http://stackoverflow.com/questions/10073699/pad-a-number-with-leading-zeros-in-javascript
*/
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
function displayColoredName(nickName, colors) {
coloredNick.classList.remove('minecraftbold', 'minecraftibold', 'minecraftitalic');
if (document.getElementById('bold').checked) {
if (document.getElementById('italics').checked) {
coloredNick.classList.add('minecraftibold');
} else {
coloredNick.classList.add('minecraftbold');
}
} else if (document.getElementById('italics').checked) {
coloredNick.classList.add('minecraftitalic');
}
coloredNick.innerHTML = '';
for (let i = 0; i < nickName.length; i++) {
const coloredNickSpan = document.createElement('span');
if (document.getElementById('underline').checked) {
if (document.getElementById('strike').checked) {
coloredNickSpan.classList.add('minecraftustrike');
} else coloredNickSpan.classList.add('minecraftunderline');
} else if (document.getElementById('strike').checked) {
coloredNickSpan.classList.add('minecraftstrike');
}
coloredNickSpan.style.color = colors[i];
coloredNickSpan.textContent = nickName[i];
coloredNick.append(coloredNickSpan);
}
}
function preset(n) {
const colors = presets[n].colors
const container = $('#hexColors');
container.empty();
// Need to add some colors
let template = $('#hexColorTemplate').html();
for (let i = 0 + 1; i <= colors.length; i++) {
let html = template.replace(/\$NUM/g, i).replace(/\$VAL/g, colors[i - 1]);
container.append(html);
}
jscolor.install(); // Refresh all jscolor elements
}
toggleColors(2);
updateOutputText();
document.getElementById('darkmode').checked = true
darkMode()