-
Notifications
You must be signed in to change notification settings - Fork 1
/
Recolors.js
executable file
·1604 lines (1329 loc) · 54 KB
/
Recolors.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { RoaRecolor } from "./Scripts/RoA WebGL Shader.mjs";
import { getJson } from "./Scripts/File System.mjs";
import { getCurrentFormName, getCurrentFormValue, updateFormList } from "./Scripts/Form Selector.mjs";
import { getSkinSelectValue, resetSkinPresetText, updateSkinList } from "./Scripts/Skin Code Selector.mjs";
let char; // this will hold the values from the character database
let rgbSliders; // if active, the page will use rgb sliders instead of hsv ones
let customPortrait, customSprite; // will hold user uploaded images
const alphas = [100, 100, 100, 100, 100, 100, 100, 100, 100];
const codeReg = "^([A-Fa-f0-9]+\-)+([A-Fa-f0-9])+$";
const ograReg = "^([A-Fa-f0-9]{6}\-)+([A-Fa-f0-9]){6}$";
const charList = ["Clairen", "Forsburn", "Zetterburn", "Wrastor", "Absa", "Elliana",
"Sylvanos", "Maypul", "Kragg", "Orcane", "Etalus", "Ranno", "Ori and Sein",
"Shovel Knight", "Mollo", "Hodan", "Pomme", "Olympia"];
const fullCanvas = document.getElementById("fullCanvas");
const animCanvas = document.getElementById("animCanvas");
const sprLCanvas = document.getElementById("spriteL");
const sprRCanvas = document.getElementById("spriteR");
const animDiv = document.getElementById("animDiv");
const spritesDiv = document.getElementById("sContainer");
const charIcon = document.getElementById("selectedIcon");
const codeInput = document.getElementById("codeInput");
const copyToClip = document.getElementById("copyToClip");
const copiedImg = document.getElementById("copyOutImg");
const codeWarning = document.getElementById("row2");
const downImgButton = document.getElementById("downImgButton");
const downMenu = document.getElementById("downMenu");
const defaultFile = document.getElementById("fileupload");
const colorEditor = document.getElementById("colorEditor");
const partList = document.getElementById("partList");
const ogColorList = document.getElementById("ogColorList");
const colorRangeList = document.getElementById("colorRangeList");
const ogButton = document.getElementById("ogButton");
const raButton = document.getElementById("raButton");
const ogInput = document.getElementById("ogInput");
const raInput = document.getElementById("raInput");
const copyOg = document.getElementById("copyOgImg");
const copyRa = document.getElementById("copyRaImg");
const hsvButton = document.getElementById("hsvClick");
const rgbButton = document.getElementById("rgbClick");
const sliderHue = document.getElementById("sliderHue");
const sliderSat = document.getElementById("sliderSat");
const sliderVal = document.getElementById("sliderVal");
const sliderR = document.getElementById("sliderR");
const sliderG = document.getElementById("sliderG");
const sliderB = document.getElementById("sliderB");
const sliderA = document.getElementById("sliderA");
const nowEditingText = document.getElementById("nowEditing");
const editingHex = document.getElementById("editingHex");
const topButtons = document.getElementById("row3");
const loadingDiv = document.getElementById("loadingDiv");
const eaCheck = document.getElementById("EAcheck");
const goldenCheck = document.getElementById("goldenCheck")
const alphaCheck = document.getElementById("alphaCheck");
const numInpsCheck = document.getElementById("numInpsCheck");
const noZoom = document.getElementById("zoomCheck");
const noPixels = document.getElementById("noPixels")
const darkCheck = document.getElementById("darkTheme");
const specialSelect = document.getElementById("specialSelector");
// first things first, initialize the shaders:
const charRenders = [
new RoaRecolor(fullCanvas), // portrait
new RoaRecolor(animCanvas), // idle
new RoaRecolor(sprLCanvas), // left sprite
new RoaRecolor(sprRCanvas), // right sprite
];
//when the page loads, change to a random character
await changeChar(charList[genRnd(0, charList.length - 1)]);
//this will fire everytime we type in the color code input
codeInput.addEventListener("input", codeControl);
function codeControl() {
//look if the code length is correct
if (codeInput.value.length == char.placeholder.length &&
codeInput.value.match(codeReg)) {
//if correct, set everything to normal
codeWarning.innerHTML = "";
codeWarning.style.height = "0px";
// allow download of the image and enable copy button
downImgButton.disabled = false;
copyToClip.disabled = false;
//automatically execute recolor for some QoL
mainRecolor();
createEditor();
} else {
//prevent the user from interacting with the copy button
copyToClip.disabled = true;
if (!codeInput.value) { // if theres no code
// just remove the warning text
codeWarning.innerHTML = "";
codeWarning.style.height = "0px";
// download button will just download base image
downImgButton.disabled = false;
} else { // check if its above or below the limit
if (codeInput.value.length < char.placeholder.length) {
//if its below the limit, warn the user
const dif = char.placeholder.length - codeInput.value.length;
codeWarning.innerHTML = "This color code has "+dif+" less characters than it should.";
codeWarning.style.color = "orange";
} else if (codeInput.value.length > char.placeholder.length) {
//if its above the limit, well thats a big no no
const dif = codeInput.value.length - char.placeholder.length;
codeWarning.innerHTML = "This color code has too many characters ("+dif+").";
codeWarning.style.color = "red";
} else {
// if the regex failed
codeWarning.innerHTML = "Invalid code.";
codeWarning.style.color = "red";
}
//no downloads allowed without a proper code
downImgButton.disabled = true;
// set height to the warning div so it animates
codeWarning.style.height = "18px";
}
}
}
//copy to clipboard button, playing an animation to show feedback
copyToClip.addEventListener("click", () => {
navigator.clipboard.writeText(codeInput.value);
// wait for the animation to finish if any
if (!copiedImg.classList.contains("copiedAnim")) {
copiedImg.classList.add("copiedAnim");
}
});
//automatically enable copy animation once current one finishes
copiedImg.addEventListener('animationend', () => {
copiedImg.classList.remove("copiedAnim");
});
// do the same for the other copy buttons
document.getElementById("copyOg").addEventListener("click", () => {
navigator.clipboard.writeText(ogButton.innerText);
if (!copyOg.classList.contains("copiedAnim")) {
copyOg.classList.add("copiedAnim");
}
});
copyOg.addEventListener('animationend', () => {
copyOg.classList.remove("copiedAnim");
});
document.getElementById("copyRa").addEventListener("click", () => {
navigator.clipboard.writeText(raButton.innerText);
if (!copyRa.classList.contains("copiedAnim")) {
copyRa.classList.add("copiedAnim");
}
});
copyRa.addEventListener('animationend', () => {
copyRa.classList.remove("copiedAnim");
});
//the recolor function!
function mainRecolor(dl) {
let rgb = hexDecode(codeInput.value); // translate the color code
if (rgb != charRenders[0].colorIn) { // if the code is not the default one
rgb.splice(rgb.length - 4); //remove the checksum at the end of the code
} else { // if default code, we'll modify it later
rgb = null;
}
if (char.name == "Orcane") { // orcane has green and yellow hidden parts
// copy either given array or og colors
rgb = rgb ? [...rgb] : [...char.colorData[getCurrentFormValue()].ogColor];
for (let i = 0; i < 8; i++) {
// orcane is a very special boi
if (goldenCheck.checked) {
rgb[i+8] = 255;
} else {
// add the 1st colors as the 3rd colors, 2nd to 4th
rgb[i+8] = rgb[i];
}
}
}
if (!rgb) {
rgb = charRenders[0].colorIn;
}
// add in custom transparency in case the user modified it
for (let i = 0; i < rgb.length; i++) {
if ((i+1)%4 == 0) {
rgb[i] = alphas[((i+1) / 4) - 1] / 100;
}
}
// golden skins have a predefined color for black pixels
if (goldenCheck.checked) {
rgb = [...rgb, 76, 53, 0, 1]
}
// now recolor the images
if (dl) {
charRenders[dl[0]].render(rgb, dl[1])
} else {
for (let i = 0; i < charRenders.length; i++) {
charRenders[i].render(rgb);
}
}
}
// for those characters that have different colors for their default skins
function manualRecolor(rgb) {
codeInput.value = rgb;
mainRecolor();
createEditor();
codeControl();
}
// TODO mOdUlAtE
document.getElementById("formSelector").addEventListener("change", () => {changeChar(char.name, true)})
document.getElementById("codePresetSelector").addEventListener("change", () => {applySkinSelectCode()})
specialSelect.addEventListener("change", applySpecial)
//whenever the character changes
async function changeChar(charName, formChange) {
// calculate the height of the divs so we dont get jumps when switching to the loading div
loadingDiv.style.height = (fullCanvas.clientHeight + spritesDiv.clientHeight) + 7 + "px";
// hide the character images, show the loading div
loadingDiv.style.display = "flex";
fullCanvas.style.display = "none";
spritesDiv.style.display = "none";
// reset the alphas in case they were modified
for (let i = 0; i < alphas.length; i++) {
alphas[i] = 100;
}
//look at the database to see whats up
char = await getJson("Characters/" + charName + "/Info");
// update the form selector, unless this is a form change
if (!formChange) {
updateFormList(char.portraitList);
updateSkinList(char.skinList);
}
// create new character images with this info
// save all the new images as promises so we know when they are fully loaded
const imgPromises = [
charRenders[0].addImage(`Characters/${char.name}/${getCurrentFormName()}/Portrait.png`)
]
// only add sprites if the form has those
if (!formHasSprites(getCurrentFormName())) {
imgPromises.push(
charRenders[1].addImage(`Characters/${char.name}/${getCurrentFormName()}/Idle.png`),
charRenders[2].addImage(`Characters/${char.name}/${getCurrentFormName()}/SpriteL.png`),
charRenders[3].addImage(`Characters/${char.name}/${getCurrentFormName()}/SpriteR.png`)
)
} else {
// to avoid them showing up in the download list
charRenders[1].setAvailable(false);
charRenders[2].setAvailable(false);
charRenders[3].setAvailable(false);
}
// when the images finish loading
Promise.all(imgPromises).then( () => {
// update color data for all renders
for (let i = 0; i < charRenders.length; i++) {
charRenders[i].updateData(
char.name,
char.colorData[getCurrentFormValue()].ogColor,
char.colorData[getCurrentFormValue()].colorRange,
eaCheck.checked,
specialSelect.value
)
}
//set a new placeholder text
codeInput.placeholder = char.placeholder;
//then clear the current color code
codeInput.value = null;
codeControl(); //to reset the warning message if any
// change the character icon
// if the form has a custom icon, lets use it!
if (formHasIcon(getCurrentFormName())) {
charIcon.setAttribute("src", "Characters/"+char.name+"/"+getCurrentFormName()+"/Icon.png")
} else {
charIcon.setAttribute("src", "Characters/"+char.name+"/1.png");
}
// do a first paint (some characters have different defaults)
if (char.name == "Ori and Sein") {
manualRecolor("F8F5-FCF8-F5FC-0000-005D-CBF1-FFC8-21A4");
} else if (char.name == "Olympia") {
manualRecolor("EC8D-CAEC-8DCA-B880-53E4-8574-F7F3-F9FF-F9F9-367B-8D4A");
} else {
mainRecolor();
}
//adjust the code input width
resizeInput();
// create a new color editor
createEditor();
// hide the color sliders, show top buttons
colorEditor.style.display = "none";
topButtons.style.display = "flex";
// hide the loading div, show the characters
loadingDiv.style.display = "none";
fullCanvas.style.display = "inherit";
// only show sprites if the portrait has those
if (!formHasSprites(getCurrentFormName())) {
spritesDiv.style.display = "inherit";
}
})
if (!formHasSprites(getCurrentFormName())) {
// all of this is for the animated idle sprite
const sprite = new Image();
sprite.src = `Characters/${char.name}/${getCurrentFormValue()}/Idle.png`;
sprite.decode().then( () => { // when the image finishes loading
//change the width of the sprite animation, depending on the character
animDiv.style.width = (sprite.width / char.colorData[getCurrentFormValue()].idleFC) + "px"; //gets the w of 1 frame
animDiv.style.height = sprite.height + "px"; // div will have slightly wrong height otherwise
//now change the variables for the sprite animation
const r = document.querySelector(':root');
r.style.setProperty("--spriteMove", -sprite.width + "px"); //end position of the animation
r.style.setProperty("--spriteCount", char.colorData[getCurrentFormValue()].idleFC); // frame count
// formula for this one is: 1000 is a second, then divided by 60 gets us an
// in-game frame, then we multiply by 7 because thats the average frame
// wait between sprite changes, and then we multiply by the character frame
// count to know how long the animation is going to take, finally, we divide
// by 1000 to get the value in seconds for the css variable
r.style.setProperty("--spriteTime", 1000/60*7*char.colorData[getCurrentFormValue()].idleFC/1000 + "s");
})
charRenders[1].setAvailable(true);
charRenders[2].setAvailable(true);
charRenders[3].setAvailable(true);
}
}
// create the color editor, called whenever we switch character
function createEditor() {
//clear the part list
partList.innerHTML = null;
let count = 0; // keep track of how many parts we have created
const aCheck = alphaCheck.checked; // so its shorter
const rgb = hexDecode(codeInput.value); // get the rgb values of the current code
// check if the current character has hidden parts
const theLength = char.actualParts ? char.actualParts * 4 : charRenders[0].colorIn.length;
// now for each (editable) part:
for (let i = 0; i < theLength; i += 4) {
// this is where everything will be stored
const part = document.createElement("button");
part.className = "part";
part.setAttribute("pNum", count); // just a way to know what we will click
part.addEventListener("click", showSliders);
// get the part name and colorize its background
const partName = document.createElement("div");
partName.innerHTML = char.partNames[count];
partName.classList.add("partName");
partName.style.backgroundColor = "rgb(" + rgb[i] + ", " + rgb[i+1] + ", " + rgb[i+2] + ")";
// lets also add the hex code for each part
const hexDiv = document.createElement("div");
hexDiv.classList.add("hexDiv");
hexDiv.innerHTML = "#" + rgbToHex(rgb[i], rgb[i+1], rgb[i+2]);
// add the rgb values with a cool colored icon next to them
const red = genColorDiv(rgb[i], rgb[i] + ", 0, 0");
const green = genColorDiv(rgb[i+1], "0," + rgb[i+1] + ", 0");
const blue = genColorDiv(rgb[i+2], "0, 0," + rgb[i+2]);
// add the alpha value
const alpha = document.createElement("div");
alpha.classList.add("alphaText");
alpha.innerHTML = alphas[count] + "%";
// now add everything we just created to the part div and then to the actual html
part.appendChild(partName);
if (!aCheck) part.appendChild(hexDiv);
part.appendChild(red);
part.appendChild(green);
part.appendChild(blue);
if (aCheck) part.appendChild(alpha);
partList.appendChild(part);
count++;
}
}
function genColorDiv(text, bgCol) {
const colorDiv = document.createElement("div");
colorDiv.classList.add("colorDiv");
const rgbColor = document.createElement("div");
rgbColor.classList.add("rgbColor");
rgbColor.style.backgroundColor = "rgb(" + bgCol + ")";
colorDiv.appendChild(rgbColor);
const colorText = document.createElement("div");
colorText.innerHTML = text;
colorDiv.appendChild(colorText);
return colorDiv;
}
//create the character dropdown menu
charSwitcher();
function charSwitcher() {
const drop = document.getElementById("charDropdown");
const row1 = document.getElementById("charRow1");
const row2 = document.getElementById("charRow2");
const row3 = document.getElementById("charRow3");
for (let i = 0; i < charList.length; i++) {
//create a new div that will have the char info
const newDiv = document.createElement('div');
newDiv.className = "charEntry";
newDiv.setAttribute("data-tooltip", charList[i]);
newDiv.setAttribute("data-tooltip-delay", "none");
//depending on the char num, set elemental background
if (i < 3) { //fire
newDiv.style.backgroundColor = "#ba6464"
} else if (i < 6) { //air
newDiv.style.backgroundColor = "#bd8ac1"
} else if (i < 9) { //earth
newDiv.style.backgroundColor = "#6ca577"
} else if (i < 12) { //water
newDiv.style.backgroundColor = "#6673ad"
} else if (i < 14) { //but everything changed when the indie characters attacked
newDiv.style.backgroundColor = "#c9b4d3"
} else { // but nobody expected the workshop inquisition
newDiv.style.backgroundColor = "#a1a1a1"
}
//if the div gets clicked, update the character to be recolored
newDiv.addEventListener("click", () => {
changeChar(charList[i]);
drop.parentElement.blur();
});
//create the character image, randomized icon just because we can
const newImg = document.createElement('img');
newImg.setAttribute("src",
`Characters/${charList[i]}/${genRnd(2, 3)}.png`
);
newImg.className = "iconImage";
//add it to the div we created before
newDiv.appendChild(newImg);
//now add it to the actual interface
if (i <= 5) {
row1.appendChild(newDiv);
} else if (i <= 11) {
row2.appendChild(newDiv);
} else {
row3.appendChild(newDiv);
}
}
}
//adjust the input width depending on code length
function resizeInput() {
switch (char.placeholder.length) {
case 19: codeInput.style.width = "170px"; break;
case 24: codeInput.style.width = "215px"; break;
case 34: codeInput.style.width = "300px"; break;
case 39: codeInput.style.width = "345px"; break;
case 49: codeInput.style.width = "430px"; break;
case 54: codeInput.style.width = "475px"; break;
default: codeInput.style.width = "555px"; break;
}
}
/**
* TODO This should be on Skin Code Selector.mjs
*/
function applySkinSelectCode() {
if (getSkinSelectValue() != "default") {
for (let i = 0; i < char.skinList.length; i++) {
if (char.skinList[i].name == getSkinSelectValue()) {
codeInput.value = char.skinList[i].hex;
// check the code, this is just to force recoloring the image
codeControl();
resetSkinPresetText();
if (char.skinList[i].note) {
// if skin has a note
codeWarning.innerHTML = char.skinList[i].note;
codeWarning.style.color = "orange";
codeWarning.style.height = "18px";
}
}
}
}
}
function applySpecial() {
for (let i = 0; i < charRenders.length; i++) {
charRenders[i].updateSpecial(this.value);
}
mainRecolor();
}
/**
* TODO this should be on i dont even know man but not here
* @param {String} name
* @returns {Object}
*/
function formHasSprites(name) {
for (let i = 0; i < char.portraitList.length; i++) {
if (char.portraitList[i].name == name) {
return char.portraitList[i].noSprite;
}
}
return null;
}
/**
* TODO this should be on i dont even know man but not here
* @param {String} name
* @returns {Object}
*/
function formHasIcon(name) {
for (let i = 0; i < char.portraitList.length; i++) {
if (char.portraitList[i].name == name) {
return char.portraitList[i].icon;
}
}
return null;
}
//download image button
downImgButton.addEventListener('click', () => {
// show the download popout
showInfo("downInfo");
// clear the download menu
downMenu.innerHTML = null;
const downloadNames = ["Portrait", "Idle Spritesheet", "Sprite Left", "Sprite Right"];
// for each loaded image
for (let i = 0; i < charRenders.length; i++) {
if (charRenders[i].available) {
// create a link (necessary to trigger download)
const newA = document.createElement("a");
newA.id = downloadNames[i];
// downloaded filename
newA.setAttribute("download", `${char.name} ${downloadNames[i]} Recolor`);
// create a button
const newBut = document.createElement("button");
newBut.classList.add("buttons", "downButt");
newBut.innerHTML = downloadNames[i]; // set its name
newBut.addEventListener("click", () => {
mainRecolor([i, downloadNames[i]]); // we need to repaint the image to be able to download it
})
// add it to the menu
newA.append(newBut);
downMenu.appendChild(newA);
}
}
});
//randomize button, generates a random valid code based on character parts count
document.getElementById("randomize").addEventListener("click", () => {
if (char.actualParts) { // for orcane
randomize(char.actualParts)
} else {
randomize(charRenders[0].colorIn.length / 4)
}
});
function randomize(colorNum) {
//code from https://github.com/ErrorThreeOThree/ROAColorCodeBot
//randomize the rgb values
const r = [...Array(colorNum)].map(() => Math.floor(Math.random() * 256));
const g = [...Array(colorNum)].map(() => Math.floor(Math.random() * 256));
const b = [...Array(colorNum)].map(() => Math.floor(Math.random() * 256));
//generate a valid checksum (this is only for the game, the webpage doesn't need this)
let checksum = 0;
for (let i = 0; i < colorNum; i++)
{
checksum += (i + 101) * r[i];
checksum += (i + 102) * g[i];
checksum += (i + 103) * b[i];
}
checksum = checksum % 256;
//convert the rgb values to hex, add those and the checksum to a single string
let code = "";
let i = 0;
for (i; i < colorNum; i++)
{
code += r[i].toString(16).toUpperCase().padStart(2, '0');
code += g[i].toString(16).toUpperCase().padStart(2, '0');
code += b[i].toString(16).toUpperCase().padStart(2, '0');
}
code += checksum.toString(16).toUpperCase().padStart(2, '0');
if (i % 2 == 0)
{
code += "00";
}
//put the code in the code input, separating the full color code with "-" every 4 characters
codeInput.value = code.match(/.{1,4}/g).join("-");
//check the code, this is just to force recoloring the image
codeControl();
}
//when clicking on the fake upload image button, click on the hidden upload file button
document.getElementById("uplImg").addEventListener("click", () => {
defaultFile.click();
});
//if we got a file
defaultFile.addEventListener("change", () => {
const newImg = defaultFile.files[0]; //store the selected file
//read file with some magic i dont really understand
const fileReader = new FileReader();
fileReader.addEventListener("load", function () {
// save the image in case we need it
customPortrait = this.result;
//add the custom image to the portrait canvas
charRenders[0].addImage(customPortrait).then( () => { // wait for the img to load
mainRecolor(); // then show it
// hide the sprites
spritesDiv.style.display = "none";
})
});
fileReader.readAsDataURL(newImg); //imma be honest idk what this is but it doesnt work without it
});
// Early Access check
eaCheck.addEventListener("click", () => {
for (let i = 0; i < charRenders.length; i++) {
charRenders[i].updateBlend(eaCheck.checked);
}
mainRecolor();
})
// Golden borders check
goldenCheck.addEventListener("click", () => {
mainRecolor();
})
// Alpha editing
alphaCheck.addEventListener("click", alphaEdit);
function alphaEdit() {
if (alphaCheck.checked) {
sliderA.style.display = "inherit";
} else {
sliderA.style.display = "none";
for (let i = 0; i < alphas.length; i++) {
alphas[i] = 100;
}
}
createEditor();
}
/* alphaEdit(); */
// Numerical Inputs
numInpsCheck.addEventListener("click", numInpsSwap);
function numInpsSwap() {
const sliders = document.getElementById("sliderDiv").children;
for (let i = 0; i < sliders.length; i++) {
if (numInpsCheck.checked) {
sliders[i].type = "number";
sliders[i].classList.remove("slider");
sliders[i].classList.add("numInput");
} else {
sliders[i].type = "range";
sliders[i].classList.remove("numInput");
sliders[i].classList.add("slider");
}
}
}
numInpsSwap();
// No point scaling
noPixels.addEventListener("click", noPixelsCheck)
function noPixelsCheck() {
if (noPixels.checked) {
fullCanvas.style.imageRendering = "auto";
document.getElementById("sContainer").style.imageRendering = "auto";
} else {
fullCanvas.style.imageRendering = "pixelated";
fullCanvas.style.imageRendering = "crisp-edges";
document.getElementById("sContainer").style.imageRendering = "pixelated";
document.getElementById("sContainer").style.imageRendering = "crisp-edges";
}
}
noPixelsCheck();
// No scale down
noZoom.addEventListener("click", noZoomCheck);
function noZoomCheck() {
if (noZoom.checked) {
fullCanvas.style.maxWidth = "none";
fullCanvas.style.maxHeight = "none";
} else {
fullCanvas.style.maxWidth = "100%";
fullCanvas.style.maxHeight = "600px";
}
}
noZoomCheck();
// Dark Theme
darkCheck.addEventListener("click", darkMode);
function darkMode() {
const r = document.querySelector(':root');
if (darkCheck.checked) {
r.style.setProperty("--mainBG", "#202020");
r.style.setProperty("--bg", "#686868");
r.style.setProperty("--bgH", "#818181");
r.style.setProperty("--bgD", "#303030");
} else {
r.style.setProperty("--mainBG", "#514188");
r.style.setProperty("--bg", "#7c6bb4");
r.style.setProperty("--bgH", "#9484c9");
r.style.setProperty("--bgD", "#4a368a");
}
}
darkMode(); // to activate at startup
// called when the user clicks on a part
function showSliders() {
// show the color sliders, hide buttons
colorEditor.style.display = "block";
topButtons.style.display = "none";
// this tells us what part has been clicked
const partNum = this.getAttribute("pNum");
// change the text of the active part
nowEditingText.innerHTML = char.partNames[partNum];
// tell the sliders what to change
sliderR.setAttribute("num", partNum * 4);
sliderG.setAttribute("num", partNum * 4 + 1);
sliderB.setAttribute("num", partNum * 4 + 2);
sliderHue.setAttribute("num", partNum * 4);
sliderSat.setAttribute("num", partNum * 4 + 1);
sliderVal.setAttribute("num", partNum * 4 + 2);
sliderA.setAttribute("num", partNum);
const rgb = hexDecode(codeInput.value);
// update the slider values to the current part's
if (rgbSliders) {
sliderR.value = rgb[partNum * 4];
sliderG.value = rgb[partNum * 4 + 1];
sliderB.value = rgb[partNum * 4 + 2];
} else {
const hsv = rgb2hsv(rgb[partNum * 4], rgb[partNum * 4 + 1], rgb[partNum * 4 + 2])
sliderHue.value = hsv[0];
sliderSat.value = hsv[1];
sliderVal.value = hsv[2];
// update the slider color
const cssRgb = hsv2rgb(hsv[0] / 360, 1, 1);
cssRgb[0] = cssRgb[0] * 255;
cssRgb[1] = cssRgb[1] * 255;
cssRgb[2] = cssRgb[2] * 255;
sliderSat.style.background = "linear-gradient(to right, white, rgb(" + cssRgb[0] + ", " + cssRgb[1] + ", " + cssRgb[2] + ")";
sliderVal.style.background = "linear-gradient(to right, black, rgb(" + cssRgb[0] + ", " + cssRgb[1] + ", " + cssRgb[2] + ")";
}
sliderA.value = alphas[partNum];
// change the color of the "now editing" indicator
editingHex.style.backgroundColor = "rgb(" + rgb[partNum*4] + ", " + rgb[partNum*4+1] + ", " + rgb[partNum*4+2] + ")";
}
document.getElementById("hideEditor").addEventListener("click", hideSliders);
function hideSliders() {
colorEditor.style.display = "none";
topButtons.style.display = "flex";
}
sliderHue.oninput = sliderMoved;
sliderSat.oninput = sliderMoved;
sliderVal.oninput = sliderMoved;
sliderR.oninput = sliderMoved;
sliderG.oninput = sliderMoved;
sliderB.oninput = sliderMoved;
sliderA.oninput = alphaMoved;
function sliderMoved() {
// get the current code
const rgb = hexDecode(codeInput.value);
const newRgb = [];
const num = Number(this.getAttribute("num"));
const num2 = num-(num%4);
if (rgbSliders) { // if rgb mode
// modify the rgb values with the new ones from the sliders
for (let i = 0; i < rgb.length; i++) {
if (i == num) {
newRgb[i] = Number(this.value);
} else {
newRgb.push(rgb[i]);
}
}
} else { // hsv mode
const rgbFromHsv = hsv2rgb(sliderHue.value / 360, sliderSat.value / 100, sliderVal.value / 100);
rgbFromHsv[0] = Math.round(rgbFromHsv[0] * 255);
rgbFromHsv[1] = Math.round(rgbFromHsv[1] * 255);
rgbFromHsv[2] = Math.round(rgbFromHsv[2] * 255);
// modify the rgb values with the new ones from the sliders
for (let i = 0; i < rgb.length; i++) {
if (i == num2 || i == num2 + 1 || i == num2 + 2) {
newRgb[i] = rgbFromHsv[i%4];
} else {
newRgb.push(rgb[i]);
}
}
// if changing the hue, update the colors of the other sliders
if (this === sliderHue) {
const cssRgb = hsv2rgb(this.value / 360, 1, 1);
cssRgb[0] = cssRgb[0] * 255;
cssRgb[1] = cssRgb[1] * 255;
cssRgb[2] = cssRgb[2] * 255;
sliderSat.style.background = "linear-gradient(to right, white, rgb(" + cssRgb[0] + ", " + cssRgb[1] + ", " + cssRgb[2] + ")";
sliderVal.style.background = "linear-gradient(to right, black, rgb(" + cssRgb[0] + ", " + cssRgb[1] + ", " + cssRgb[2] + ")";
}
}
// display a new code
genCodeManual(newRgb)
// update the color on the "now editing" indicator
editingHex.style.backgroundColor = "rgb(" + newRgb[num2] + ", " + newRgb[num2+1] + ", " + newRgb[num2+2] + ")";
}
function alphaMoved() {
const num = Number(this.getAttribute("num"));
alphas[num] = this.value;
createEditor();
mainRecolor();
}
hsvButton.addEventListener("click", () => {
rgbSliders = false;
changeSliders();
})
rgbButton.addEventListener("click", () => {
rgbSliders = true;
changeSliders();
})
function changeSliders() {
// show and hide the sliders
const slidersHSV = document.getElementsByClassName("sliderHSV");
const slidersRGB = document.getElementsByClassName("sliderRGB");
if (rgbSliders) {
for (let i = 0; i < slidersHSV.length; i++) {
slidersHSV[i].style.display = "none";
slidersRGB[i].style.display = "inherit";
}
hsvButton.style.backgroundColor = "var(--mainBG)";
rgbButton.style.backgroundColor = "var(--bg)";
} else {
for (let i = 0; i < slidersHSV.length; i++) {
slidersHSV[i].style.display = "inherit";
slidersRGB[i].style.display = "none";
}
hsvButton.style.backgroundColor = "var(--bg)";
rgbButton.style.backgroundColor = "var(--mainBG)";
}
// change the slider values
const rgb = hexDecode(codeInput.value);
if (rgbSliders) {
sliderR.value = rgb[sliderR.getAttribute("num")];
sliderG.value = rgb[sliderG.getAttribute("num")];
sliderB.value = rgb[sliderB.getAttribute("num")];
} else {
const hsv = rgb2hsv(rgb[sliderHue.getAttribute("num")], rgb[sliderSat.getAttribute("num")], rgb[sliderVal.getAttribute("num")])
sliderHue.value = hsv[0];
sliderSat.value = hsv[1];
sliderVal.value = hsv[2];
// update the slider color
const cssRgb = hsv2rgb(hsv[0] / 360, 1, 1);
cssRgb[0] = cssRgb[0] * 255;
cssRgb[1] = cssRgb[1] * 255;
cssRgb[2] = cssRgb[2] * 255;
sliderSat.style.background = "linear-gradient(to right, white, rgb(" + cssRgb[0] + ", " + cssRgb[1] + ", " + cssRgb[2] + ")";
sliderVal.style.background = "linear-gradient(to right, black, rgb(" + cssRgb[0] + ", " + cssRgb[1] + ", " + cssRgb[2] + ")";
}
}
function genCodeManual(rgb) {
// this is mostly the randomize code
// get the number of parts we will recolor
const colorNum = char.actualParts ? char.actualParts : charRenders[0].colorIn.length / 4;
// add in the rgb values in hex form
let finalCode = "";
for (let i = 0; i < colorNum * 4; i += 4) {
finalCode += rgbToHex(rgb[i], rgb[i+1], rgb[i+2])
}
//generate a valid checksum (this is only for the game, the webpage doesn't need this)
let checksum = 0;
let count = 0;
for (let i = 0; i < colorNum*4; i += 4)
{
checksum += (count + 101) * rgb[i];
checksum += (count + 102) * rgb[i+1];
checksum += (count + 103) * rgb[i+2];
count++;
}
checksum = checksum % 256;
finalCode += checksum.toString(16).toUpperCase().padStart(2, '0');
if (colorNum % 2 == 0)
{
finalCode += "00";
}
//put the code in the code input, separating the full color code with "-" every 4 characters
codeInput.value = finalCode.match(/.{1,4}/g).join("-").toUpperCase();
//check the code, this is just to force recoloring the image
codeControl();
}
// lets see that edit character mode