Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
2481 lines (2281 loc) · 98.9 KB
/
Copy pathindex.html
File metadata and controls
2481 lines (2281 loc) · 98.9 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<!-- If you're reading this, you might like this tool, but it would be NICE if you could credit me somehow.
Demon, Despair, Asmodeus, or Jeff. https://www.github.com/coffee-nerd/
Please leave this line in the code. Preferably we can collaborate and you can just make a pull request. -->
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>MUD Colorizer</title>
<style>
body {
background-color: black;
color: white;
/* Default to Fira Code */
font-family: "Fira Code", monospace;
padding: 20px;
}
#output {
white-space: pre-wrap;
word-wrap: break-word;
overflow-wrap: break-word;
margin-top: 20px;
border: 1px solid #444;
padding: 10px;
background-color: #111;
}
#controls {
margin-bottom: 20px;
}
label {
color: white;
font-size: 1rem;
margin-right: 10px;
}
textarea {
width: 100%;
height: 150px;
background-color: #111;
color: white;
border: 1px solid #444;
padding: 10px;
font-family: inherit;
resize: vertical;
}
select {
background-color: #111;
color: white;
border: 1px solid #444;
padding: 5px;
font-family: inherit;
margin-right: 10px;
}
.preview-box {
display: inline-block;
vertical-align: middle;
width: 100px;
height: 20px;
margin-left: 10px;
border: 1px solid #444;
background: black;
}
.mode-selector {
margin-bottom: 20px;
}
.mode-selector input {
margin-right: 5px;
}
.disabled {
opacity: 0.5;
pointer-events: none;
}
/* Custom Gradient Inputs */
.custom-gradient-input {
background-color: #111;
color: white;
border: 1px solid #444;
padding: 5px;
font-family: inherit;
}
/* Toggle buttons styling */
#toggleCustomGradientButton,
#toggleManageCustomGradients {
background-color: #111;
color: white;
border: 1px solid #444;
padding: 5px 10px;
font-family: inherit;
cursor: pointer;
margin-bottom: 10px;
}
/* Shake animation */
@keyframes shake {
0% { transform: translateX(0); }
25% { transform: translateX(-5px); }
50% { transform: translateX(5px); }
75% { transform: translateX(-5px); }
100% { transform: translateX(0); }
}
.shake {
animation: shake 0.3s;
}
.invalid {
border: 2px solid red !important;
}
/* Manage Custom Gradients section */
#manageCustomGradients {
display: none;
border: 1px solid #444;
padding: 10px;
margin-top: 10px;
}
#manageCustomGradients h3 {
margin-top: 0;
}
/* Inline help text */
.help-text {
font-size: 0.8rem;
color: #888;
}
/* Styling for each editable custom gradient item */
.custom-gradient-item {
display: flex;
align-items: center;
margin-bottom: 5px;
border-bottom: 1px solid #444;
padding-bottom: 5px;
}
.custom-gradient-item input[type="text"] {
margin-right: 10px;
}
.custom-gradient-preview {
width: 80px;
height: 20px;
border: 1px solid #444;
margin-right: 10px;
}
/* Brightness adjustment controls */
.brightness-controls {
display: flex;
align-items: center;
margin-top: 10px;
padding: 10px;
border: 1px solid #444;
background-color: #1a1a1a;
}
.brightness-slider {
width: 200px;
margin: 0 10px;
}
.brightness-value {
width: 50px;
text-align: center;
color: #888;
}
/* Character counter styling */
#charCounter {
float: right;
color: #888;
font-size: 0.9rem;
margin-top: 5px;
}
#charCounter.over-limit {
color: #ff0000;
}
/* Output container to hold both the output and counter */
.output-container {
position: relative;
}
/* Line character count styling */
.line-char-count {
color: #666;
font-size: 0.8rem;
display: inline-block;
margin-left: 10px;
user-select: none;
white-space: nowrap;
position: absolute;
right: 10px;
}
.line-char-count.over-limit {
color: #ff0000;
}
/* Copy button styling */
.copy-container {
position: relative;
display: inline-block;
width: 100%;
}
.copy-button {
position: absolute;
top: 8px;
right: 8px;
background-color: #333;
color: white;
border: 1px solid #555;
padding: 6px 12px;
font-family: inherit;
font-size: 0.85rem;
cursor: pointer;
border-radius: 4px;
z-index: 10;
transition: all 0.2s ease;
}
.copy-button:hover {
background-color: #444;
border-color: #666;
}
.copy-button:active {
background-color: #222;
transform: translateY(1px);
}
.copy-button.copied {
background-color: #0a5d0a;
border-color: #0f8f0f;
}
</style>
</head>
<body>
<h1 style="color: white">MUD Colorizer</h1>
<div id="controls">
<!-- Mode Selection -->
<div class="mode-selector">
<label>Select Mode:</label>
<input type="radio" id="editMode" name="mode" value="edit" checked />
<label for="editMode">Edit Mode</label>
<input
type="radio"
id="viewMode"
name="mode"
value="view"
style="margin-left: 20px"
/>
<label for="viewMode">Viewer Mode</label>
</div>
<!-- Font Selection -->
<div>
<label for="fontSelector">Select Font:</label>
<select id="fontSelector">
<option value="'Fira Code', monospace" selected>Fira Code</option>
<option value="'Courier New', monospace">Courier New</option>
<option value="'Hack', monospace">Hack</option>
<option value="'JetBrains Mono', monospace">JetBrains Mono</option>
<option value="'Roboto Mono', monospace">Roboto Mono</option>
<option value="'Source Code Pro', monospace">Source Code Pro</option>
<option value="'Ubuntu Mono', monospace">Ubuntu Mono</option>
<option value="'Monaco', monospace">Monaco</option>
<option value="'Consolas', monospace">Consolas</option>
<option value="'Anonymous Pro', monospace">Anonymous Pro</option>
</select>
</div>
<br />
<!-- Code Format Selection -->
<div>
<label for="formatSelector">Color Codes:</label>
<select id="formatSelector">
<option value="@x">Xterm: @xNNN</option>
<option value="{x">Xterm: {xNNN</option>
<option value="$x">Xterm: $xNNN</option>
<option value="@[F">Xterm F-colors: @[FNNN]</option>
<option value="^[F">Xterm F-colors: ^[FNNN]</option>
<option value="\t">Xterm Builder F-colors: \t[FNNN]</option>
<option value="&">Xterm: &NNN</option>
<option value="ansi-shell">ANSI Shell: \\033[...m (for echo -e)</option>
<option value="ansi-rust">ANSI Rust: \\x1B[...m</option>
<option value="ansi-lua">ANSI Lua: \\27[...m</option>
<option value="ansi-c">ANSI C/Java: \\033[...m</option>
</select>
</div>
<br />
<!-- Text Input -->
<label for="textInput">Paste Your Text:</label><br />
<textarea id="textInput" placeholder="Paste your text here..."></textarea>
<br /><br />
<!-- Gradient Selection -->
<div id="gradientControls">
<div>
<label for="gradientSelect">Select Gradient:</label>
<select id="gradientSelect"></select>
<span class="preview-box" id="gradientPreview"></span>
<input type="checkbox" id="invertGradient" />
<label for="invertGradient">Invert</label>
</div>
<br />
<!-- Pattern Selection -->
<div>
<label for="patternSelect">Pattern:</label>
<select id="patternSelect">
<option value="original">Original</option>
<option value="distributed">Distributed</option>
<option value="symmetrical">Symmetrical</option>
</select>
<button id="instantLighterButton" style="background-color: #111; color: white; border: 1px solid #444; padding: 5px 10px; font-family: inherit; cursor: pointer;" title="Make colors 5% lighter. Note: For xterm colors, adjustments may not be perfectly reversible due to palette limitations. Truecolor ($X) format preserves full precision.">Lighter</button>
<button id="instantDarkerButton" style="background-color: #111; color: white; border: 1px solid #444; padding: 5px 10px; font-family: inherit; cursor: pointer;" title="Make colors 5% darker. Note: For xterm colors, adjustments may not be perfectly reversible due to palette limitations. Truecolor ($X) format preserves full precision.">Darker</button>
</div>
<br />
<!-- Format Controls -->
<div>
<label for="wrapLimit">Format at character limit:</label>
<input
type="number"
id="wrapLimit"
min="20"
max="200"
value="80"
style="width: 60px; background-color: #111; color: white; border: 1px solid #444; padding: 5px; font-family: inherit;"
/>
<button
id="formatButton"
style="background-color: #111; color: white; border: 1px solid #444; padding: 5px 10px; font-family: inherit; cursor: pointer;"
>
Format
</button>
</div>
<br />
<!-- Toggle button for Custom Gradient -->
<div>
<input type="button" id="toggleCustomGradientButton" value="Create custom gradient" />
<input type="button" id="randomGradientButton" value="Generate random truecolor gradient" style="background-color: #111; color: white; border: 1px solid #444; padding: 5px 10px; font-family: inherit; cursor: pointer;" />
<input type="button" id="saveRandomGradientButton" value="Save generated gradient" style="background-color: #111; color: white; border: 1px solid #444; padding: 5px 10px; font-family: inherit; cursor: pointer; display: none;" />
</div>
<br />
<!-- Add New Gradients (hidden by default) -->
<div id="addGradient" style="display: none;">
<div>
<label>Add New Gradient:</label>
<label for="newGradientName">Name</label>
<input
type="text"
id="newGradientName"
placeholder="My Gradient"
required
pattern="^[\w\s]+$"
title="Enter a name (letters, numbers, and spaces only)"
class="custom-gradient-input"
/>
<small class="help-text">Allowed: letters, numbers, spaces</small>
|
<label for="newGradientCodes">Color Codes</label>
<input
type="text"
id="newGradientCodes"
placeholder="120 16 220 161"
required
pattern="^(?:[0-9]{1,3}(?:\s+[0-9]{1,3})*)$"
title="Enter space-separated numbers between 0 and 255"
class="custom-gradient-input"
/>
<small class="help-text">E.g.: 54 55 56 57</small>
<span class="preview-box" id="customGradientPreview"></span>
<input type="button" id="newGradientProcess" value="Add" />
</div>
</div>
<br />
<!-- Toggle button for managing custom gradients -->
<div>
<input type="button" id="toggleManageCustomGradients" value="Manage custom gradients" />
</div>
<br />
<!-- Manage Custom Gradients Section -->
<div id="manageCustomGradients">
<h3>Custom Gradients</h3>
<div id="customGradientsList"></div>
</div>
<br />
<!-- Pattern Selection - Removed from here -->
</div>
</div>
<div class="output-container">
<div id="output"></div>
<div id="charCounter">Character count per line →</div>
</div>
<div style="margin-top: 20px;">
<label for="copyableOutput">Copyable Text with Color Codes:</label>
<span id="ansiHelp" style="display: none; color: #888; font-size: 0.85rem; margin-left: 10px;">
(Use with echo -e or printf in terminal)
</span>
<br />
<div class="copy-container">
<textarea
id="copyableOutput"
style="width: 100%; height: 150px; background-color: #111; color: white; border: 1px solid #444; padding: 10px; font-family: inherit; resize: vertical;"
readonly
placeholder="Color-coded text will appear here..."
></textarea>
<button id="copyButton" class="copy-button" title="Copy to clipboard">Copy</button>
</div>
</div>
<script>
// Standard 16 color mapping
const standardColorMap = {
k: "#000000", // Black
K: "#555555", // Bright Black (Gray)
r: "#800000", // Dark Red
R: "#ff0000", // Bright Red
g: "#008000", // Dark Green
G: "#00ff00", // Bright Green
y: "#808000", // Dark Yellow
Y: "#ffff00", // Bright Yellow
b: "#000080", // Dark Blue
B: "#0000ff", // Bright Blue
m: "#800080", // Dark Magenta
M: "#ff00ff", // Bright Magenta
c: "#008080", // Dark Cyan
C: "#00ffff", // Bright Cyan
w: "#c0c0c0", // Dark White (Silver)
W: "#ffffff", // Bright White
D: "#333333", // Blackish
};
// Map standard color letters to an xterm index
const standardColorToIndex = {
k: 0,
K: 8,
r: 1,
R: 9,
g: 2,
G: 10,
y: 3,
Y: 11,
b: 4,
B: 12,
m: 5,
M: 13,
c: 6,
C: 14,
w: 7,
W: 15,
D: 8,
};
// Convert an Xterm color index (0-255) into an RGB hex color
function xtermToRgbHex(colorIndex) {
if (colorIndex < 16) {
const standardColors = [
"#000000",
"#800000",
"#008000",
"#808000",
"#000080",
"#800080",
"#008080",
"#c0c0c0",
"#808080",
"#ff0000",
"#00ff00",
"#ffff00",
"#0000ff",
"#ff00ff",
"#00ffff",
"#ffffff",
];
return standardColors[colorIndex];
} else if (colorIndex >= 16 && colorIndex <= 231) {
const base = colorIndex - 16;
const r = (Math.floor(base / 36) % 6) * 51;
const g = (Math.floor(base / 6) % 6) * 51;
const b = (base % 6) * 51;
return `rgb(${r},${g},${b})`;
} else if (colorIndex >= 232 && colorIndex <= 255) {
const gray = Math.round((colorIndex - 232) * 10 + 8);
return `rgb(${gray},${gray},${gray})`;
} else {
return "#ffffff";
}
}
// So what the hell even are these F colors? (Heh)
// "F-colors" are essentially a shorthand notation for specifying colors in the 6×6×6 color cube used by xterm's 256-color palette.
// Instead of using a direct numeric index (like @x123), an F-color (e.g. @[F123] or ^[F123]) breaks the color into three digits—
// each representing a value (0–5) for red, green, and blue respectively. The conversion function then maps these digits into the
// appropriate xterm color index (from 16 to 231).
// In short, while they ultimately refer to colors in the xterm palette, the notation is different. They may appear to differ because:
// Xterm codes typically use a numeric index or direct hex representation.
// F-colors encode the red, green, and blue components as three digits, which are then converted into the xterm index.
// This extra layer of abstraction can make them seem different even though they're designed to represent the same range of colors.
function fToRgbHex(fCode) {
// fCode example: "F012" => r=0, g=1, b=2
const codeStr = fCode.slice(1);
const r = parseInt(codeStr.charAt(0), 10);
const g = parseInt(codeStr.charAt(1), 10);
const b = parseInt(codeStr.charAt(2), 10);
const colorIndex = 16 + 36 * r + 6 * g + b;
return xtermToRgbHex(colorIndex);
}
function renderXterm(text) {
let output = "";
let currentColor = "#ffffff";
let lineCharCount = 0;
let lines = [];
let currentLine = "";
// Extended regex to also match ANSI escape sequences (all formats: \x1b, \u001b, \\033, \\x1B, \\27)
const renderRegex = /((?:\x1b|\u001b|\\033|\\x1B|\\27)\[(?:0|3[0-7]|9[0-7]|38;5;\d{1,3}|38;2;\d{1,3};\d{1,3};\d{1,3})m|\$X[0-9a-fA-F]{6}|\$x\d{3}|@x\d{3}|\{x\d{3}|\^\[[F]\d{3}\]|@\[[F]\d{3}\]|\\t\[[F]\d{3}\]|@n|\$[rRgGyYbBmMcCwWdD](?:\d{3})?|@[rRgGyYbBmMcCwWdD](?:\d{3})?|\&[rRgGyYbBmMcCwWdD](?:\d{3})?|\{[rRgGyYbBmMcCwWdD](?:\d{3})?|\{\d{3}|\&\d{3})|([\s\S])/g;
text.replace(renderRegex, (match, colorCode, textContent) => {
if (colorCode) {
if (colorCode.startsWith("\x1b[") || colorCode.startsWith("\u001b[") || colorCode.includes("\\033[") || colorCode.includes("\\x1B[") || colorCode.includes("\\27[")) {
// Handle ANSI escape codes
if (colorCode === "\x1b[0m" || colorCode === "\u001b[0m" || colorCode === "\\033[0m" || colorCode === "\\x1B[0m" || colorCode === "\\27[0m") {
currentColor = "#ffffff"; // Reset
} else if (colorCode.match(/(?:\x1b|\u001b|\\033|\\x1B|\\27)\[3[0-7]m/)) {
// Standard colors (30-37)
const colorNum = parseInt(colorCode.match(/[0-7]/)[0]);
currentColor = xtermToRgbHex(colorNum);
} else if (colorCode.match(/(?:\x1b|\u001b|\\033|\\x1B|\\27)\[9[0-7]m/)) {
// Bright colors (90-97)
const colorNum = parseInt(colorCode.match(/[0-7]/)[0]) + 8;
currentColor = xtermToRgbHex(colorNum);
} else if (colorCode.match(/(?:\x1b|\u001b|\\033|\\x1B|\\27)\[38;5;(\d+)m/)) {
// 256 color
const matches = colorCode.match(/(?:\x1b|\u001b|\\033|\\x1B|\\27)\[38;5;(\d+)m/);
const colorNum = parseInt(matches[1]);
currentColor = xtermToRgbHex(colorNum);
} else if (colorCode.match(/(?:\x1b|\u001b|\\033|\\x1B|\\27)\[38;2;(\d+);(\d+);(\d+)m/)) {
// True color
const matches = colorCode.match(/(?:\x1b|\u001b|\\033|\\x1B|\\27)\[38;2;(\d+);(\d+);(\d+)m/);
const r = parseInt(matches[1]);
const g = parseInt(matches[2]);
const b = parseInt(matches[3]);
currentColor = `rgb(${r},${g},${b})`;
}
} else if (colorCode.startsWith("$X") && colorCode.length === 8) {
// Handle $X truecolor format
const hex = colorCode.slice(2);
currentColor = `#${hex}`;
} else if (
(colorCode.startsWith("$x") ||
colorCode.startsWith("@x") ||
colorCode.startsWith("{x")) &&
colorCode.length === 5
) {
const idx = parseInt(colorCode.slice(2), 10);
currentColor = xtermToRgbHex(idx);
} else if (colorCode.startsWith("@[") && colorCode.endsWith("]")) {
const inside = colorCode.slice(2, -1);
currentColor = fToRgbHex(inside);
} else if (colorCode.startsWith("^[") && colorCode.endsWith("]")) {
const inside = colorCode.slice(2, -1);
currentColor = fToRgbHex(inside);
} else if (
colorCode.startsWith("\\t[") &&
colorCode.endsWith("]")
) {
const inside = colorCode.slice(3, -1);
currentColor = fToRgbHex(inside);
} else if (colorCode.startsWith("&")) {
const m = colorCode.match(/^&([rRgGyYbBmMcCwWdD])(\d{3})?$/);
if (m) {
if (m[2]) {
const idx = parseInt(m[2], 10);
currentColor = xtermToRgbHex(idx);
} else {
const c = m[1];
currentColor = standardColorMap[c] || "#ffffff";
}
} else if (/^&\d{3}$/.test(colorCode)) {
const idx = parseInt(colorCode.slice(1), 10);
currentColor = xtermToRgbHex(idx);
}
} else if (colorCode.startsWith("{")) {
if (colorCode.startsWith("{x") && colorCode.length === 5) {
const idx = parseInt(colorCode.slice(2), 10);
currentColor = xtermToRgbHex(idx);
} else {
const m = colorCode.match(/^\{([rRgGyYbBmMcCwWdD])(\d{3})?$/);
if (m) {
if (m[2]) {
const idx = parseInt(m[2], 10);
currentColor = xtermToRgbHex(idx);
} else {
const c = m[1];
currentColor = standardColorMap[c] || "#ffffff";
}
} else if (/^\{\d{3}$/.test(colorCode)) {
const idx = parseInt(colorCode.slice(1), 10);
currentColor = xtermToRgbHex(idx);
}
}
} else if (colorCode === "@n") {
currentColor = "#ffffff";
} else if (colorCode.match(/^\$[rRgGyYbBmMcCwWdD](?:\d{3})?$/)) {
const c = colorCode.charAt(1);
currentColor = standardColorMap[c] || "#ffffff";
} else if (colorCode.match(/^@[rRgGyYbBmMcCwWdD](?:\d{3})?$/)) {
const c = colorCode.charAt(1);
currentColor = standardColorMap[c] || "#ffffff";
}
} else if (textContent) {
if (textContent === "\n") {
lines.push({ content: currentLine, count: lineCharCount });
currentLine = "";
lineCharCount = 0;
} else {
if (/\s/.test(textContent)) {
currentLine += escapeHtml(textContent);
} else {
currentLine += `<span style="color: ${currentColor}">${escapeHtml(textContent)}</span>`;
}
lineCharCount++;
}
}
});
// Add the last line if it has content
if (currentLine) {
lines.push({ content: currentLine, count: lineCharCount });
}
// Build the output with line character counts
lines.forEach(line => {
output += `<div style="position: relative; padding-right: 40px;">${line.content}<span class="line-char-count">${line.count}</span></div>`;
});
return output;
}
function escapeHtml(text) {
const map = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
};
return text.replace(/[&<>"']/g, function (m) {
return map[m];
});
}
// Helper functions for random gradient generation
function randomRgbColor() {
return {
r: Math.floor(Math.random() * 256),
g: Math.floor(Math.random() * 256),
b: Math.floor(Math.random() * 256)
};
}
function toHex(n) {
return n.toString(16).padStart(2, '0');
}
// Brightness adjustment functions
function adjustBrightness(rgb, factor, preservePrecision = false) {
// factor is -100 to 100
// For truecolor, we want to avoid rounding errors
if (preservePrecision) {
// Use a reversible linear transformation
// Use a fixed step size for better reversibility (13 units per 5%)
const adjustment = factor > 0 ? 13 : -13;
return {
r: Math.max(0, Math.min(255, rgb.r + adjustment)),
g: Math.max(0, Math.min(255, rgb.g + adjustment)),
b: Math.max(0, Math.min(255, rgb.b + adjustment))
};
} else {
// For xterm colors, use blending approach
if (factor > 0) {
// Lightening: blend with white
const blend = factor / 100;
return {
r: Math.round(rgb.r + (255 - rgb.r) * blend),
g: Math.round(rgb.g + (255 - rgb.g) * blend),
b: Math.round(rgb.b + (255 - rgb.b) * blend)
};
} else {
// Darkening: blend with black
const blend = -factor / 100;
return {
r: Math.round(rgb.r * (1 - blend)),
g: Math.round(rgb.g * (1 - blend)),
b: Math.round(rgb.b * (1 - blend))
};
}
}
}
function xtermToRgb(colorIndex) {
if (colorIndex < 16) {
const standardColors = [
{r: 0, g: 0, b: 0}, // 0: black
{r: 128, g: 0, b: 0}, // 1: dark red
{r: 0, g: 128, b: 0}, // 2: dark green
{r: 128, g: 128, b: 0}, // 3: dark yellow
{r: 0, g: 0, b: 128}, // 4: dark blue
{r: 128, g: 0, b: 128}, // 5: dark magenta
{r: 0, g: 128, b: 128}, // 6: dark cyan
{r: 192, g: 192, b: 192},// 7: silver
{r: 128, g: 128, b: 128},// 8: gray
{r: 255, g: 0, b: 0}, // 9: red
{r: 0, g: 255, b: 0}, // 10: green
{r: 255, g: 255, b: 0}, // 11: yellow
{r: 0, g: 0, b: 255}, // 12: blue
{r: 255, g: 0, b: 255}, // 13: magenta
{r: 0, g: 255, b: 255}, // 14: cyan
{r: 255, g: 255, b: 255} // 15: white
];
return standardColors[colorIndex];
} else if (colorIndex >= 16 && colorIndex <= 231) {
const base = colorIndex - 16;
const r = (Math.floor(base / 36) % 6) * 51;
const g = (Math.floor(base / 6) % 6) * 51;
const b = (base % 6) * 51;
return {r, g, b};
} else if (colorIndex >= 232 && colorIndex <= 255) {
const gray = Math.round((colorIndex - 232) * 10 + 8);
return {r: gray, g: gray, b: gray};
} else {
return {r: 255, g: 255, b: 255};
}
}
function rgbToXterm(rgb) {
// Find closest xterm color
let minDistance = Infinity;
let closestIndex = 0;
// Check standard 16 colors
for (let i = 0; i < 16; i++) {
const xterm = xtermToRgb(i);
const distance = Math.pow(rgb.r - xterm.r, 2) +
Math.pow(rgb.g - xterm.g, 2) +
Math.pow(rgb.b - xterm.b, 2);
if (distance < minDistance) {
minDistance = distance;
closestIndex = i;
}
}
// Check 6x6x6 color cube
const xr = Math.round(rgb.r / 51);
const xg = Math.round(rgb.g / 51);
const xb = Math.round(rgb.b / 51);
const cubeIndex = 16 + 36 * Math.min(5, xr) + 6 * Math.min(5, xg) + Math.min(5, xb);
const cubeColor = xtermToRgb(cubeIndex);
const cubeDistance = Math.pow(rgb.r - cubeColor.r, 2) +
Math.pow(rgb.g - cubeColor.g, 2) +
Math.pow(rgb.b - cubeColor.b, 2);
if (cubeDistance < minDistance) {
closestIndex = cubeIndex;
}
return closestIndex;
}
// Adjust xterm color within its own color space
function adjustXtermColor(colorIndex, lighter) {
// Standard 16 colors (0-15) - use predefined mappings
if (colorIndex < 16) {
const standardLighter = {
0: 8, // black -> gray
1: 9, // dark red -> bright red
2: 10, // dark green -> bright green
3: 11, // dark yellow -> bright yellow
4: 12, // dark blue -> bright blue
5: 13, // dark magenta -> bright magenta
6: 14, // dark cyan -> bright cyan
7: 15, // silver -> white
8: 15, // gray -> white
9: 9, // bright red (stay at max)
10: 10, // bright green (stay at max)
11: 11, // bright yellow (stay at max)
12: 12, // bright blue (stay at max)
13: 13, // bright magenta (stay at max)
14: 14, // bright cyan (stay at max)
15: 15 // white (stay at max)
};
const standardDarker = {
0: 0, // black (stay at min)
1: 1, // dark red (stay at min)
2: 2, // dark green (stay at min)
3: 3, // dark yellow (stay at min)
4: 4, // dark blue (stay at min)
5: 5, // dark magenta (stay at min)
6: 6, // dark cyan (stay at min)
7: 8, // silver -> gray
8: 0, // gray -> black
9: 1, // bright red -> dark red
10: 2, // bright green -> dark green
11: 3, // bright yellow -> dark yellow
12: 4, // bright blue -> dark blue
13: 5, // bright magenta -> dark magenta
14: 6, // bright cyan -> dark cyan
15: 7 // white -> silver
};
return lighter ? standardLighter[colorIndex] : standardDarker[colorIndex];
}
// 6x6x6 color cube (16-231)
if (colorIndex >= 16 && colorIndex <= 231) {
const base = colorIndex - 16;
let r = Math.floor(base / 36) % 6;
let g = Math.floor(base / 6) % 6;
let b = base % 6;
if (lighter) {
// Increase each component by 1 (max 5)
r = Math.min(5, r + 1);
g = Math.min(5, g + 1);
b = Math.min(5, b + 1);
} else {
// Decrease each component by 1 (min 0)
r = Math.max(0, r - 1);
g = Math.max(0, g - 1);
b = Math.max(0, b - 1);
}
return 16 + 36 * r + 6 * g + b;
}
// Grayscale ramp (232-255)
if (colorIndex >= 232 && colorIndex <= 255) {
if (lighter) {
return Math.min(255, colorIndex + 1);
} else {
return Math.max(232, colorIndex - 1);
}
}
return colorIndex; // fallback
}
function generateRandomTruecolorGradient(str) {
let chars = Array.from(str);
// count only non-whitespace characters for gradient steps
let total = chars.filter(c => /\S/.test(c)).length;
// Use two random colors for the gradient ends
let startColor = randomRgbColor();
let endColor = randomRgbColor();
let out = '';
let nonWhitespaceIdx = 0;
let lastAppliedColorHex = null; // Track last applied truecolor hex
for (let char of chars) {
if (/\s/.test(char)) {
out += char; // Add whitespace directly
} else {
// Calculate interpolation factor (t) based on non-whitespace character index
let t = total > 1 ? nonWhitespaceIdx / (total - 1) : 0;
// Interpolate RGB components
let r = Math.round(startColor.r + (endColor.r - startColor.r) * t);
let g = Math.round(startColor.g + (endColor.g - startColor.g) * t);
let b = Math.round(startColor.b + (endColor.b - startColor.b) * t);
// Always use $X format for random gradients (will be converted if needed)
let hex = toHex(r) + toHex(g) + toHex(b);
const colorCode = '$X' + hex;
// Only apply color code if it's different from the last one
if (colorCode !== lastAppliedColorHex) {
out += colorCode;
lastAppliedColorHex = colorCode;
}
out += char;
nonWhitespaceIdx++;
}
}
return out;
}
function wrapColorCode(colorIndex) {
const format = document.getElementById("formatSelector").value;
if (format === "@x") {
return `@x${String(colorIndex).padStart(3, "0")}`;
} else if (format === "{x") {
return `{x${String(colorIndex).padStart(3, "0")}`;
} else if (format === "$x") {
return `$x${String(colorIndex).padStart(3, "0")}`;
} else if (format === "&") {
return `&${String(colorIndex).padStart(3, "0")}`;
} else if (format.startsWith("ansi-")) {
// ANSI escape codes - different formats for different languages
let escapeSeq;
if (format === "ansi-shell") {
escapeSeq = "\\033";
} else if (format === "ansi-rust") {
escapeSeq = "\\x1B";
} else if (format === "ansi-lua") {
escapeSeq = "\\27";
} else if (format === "ansi-c") {
escapeSeq = "\\033";
}
if (colorIndex < 8) {
// Standard colors (30-37)
return `${escapeSeq}[3${colorIndex}m`;
} else if (colorIndex < 16) {
// Bright colors (90-97)
return `${escapeSeq}[9${colorIndex - 8}m`;
} else {
// Extended 256 colors
return `${escapeSeq}[38;5;${colorIndex}m`;
}
} else if (format === "@[F") {
let fCode = "F000";
if (colorIndex >= 16 && colorIndex <= 231) {
const base = colorIndex - 16;
const r = Math.floor(base / 36) % 6;
const g = Math.floor(base / 6) % 6;
const b = base % 6;
fCode = `F${r}${g}${b}`;
} else {
const clamped = Math.min(Math.max(colorIndex, 16), 231);
const base = clamped - 16;
const r = Math.floor(base / 36) % 6;
const g = Math.floor(base / 6) % 6;
const b = base % 6;
fCode = `F${r}${g}${b}`;
}
return `@[${fCode}]`;
} else if (format === "^[F") {
let fCode = "F000";
if (colorIndex >= 16 && colorIndex <= 231) {
const base = colorIndex - 16;
const r = Math.floor(base / 36) % 6;
const g = Math.floor(base / 6) % 6;
const b = base % 6;
fCode = `F${r}${g}${b}`;
} else {
const clamped = Math.min(Math.max(colorIndex, 16), 231);
const base = clamped - 16;
const r = Math.floor(base / 36) % 6;
const g = Math.floor(base / 6) % 6;
const b = base % 6;
fCode = `F${r}${g}${b}`;
}
return `^[${fCode}]`;
} else if (format === "\\t") {
let fCode = "F000";
if (colorIndex >= 16 && colorIndex <= 231) {
const base = colorIndex - 16;
const r = Math.floor(base / 36) % 6;
const g = Math.floor(base / 6) % 6;
const b = base % 6;
fCode = `F${r}${g}${b}`;
} else {
const clamped = Math.min(Math.max(colorIndex, 16), 231);
const base = clamped - 16;
const r = Math.floor(base / 36) % 6;
const g = Math.floor(base / 6) % 6;
const b = base % 6;
fCode = `F${r}${g}${b}`;
}
return `\\t[${fCode}]`;
}
return `@x${String(colorIndex).padStart(3, "0")}`;
}
function convertColorCodesToFormat(text, newFormat) {
let converted = "";
// Add ANSI reset when switching to ANSI format
let needsReset = false;
text.replace(regex, (match, colorCode, textContent) => {
if (colorCode) {
let colorIndex = null;
if (
(colorCode.startsWith("$x") ||
colorCode.startsWith("@x") ||
colorCode.startsWith("{x")) &&
colorCode.length === 5
) {
colorIndex = parseInt(colorCode.slice(2), 10);
} else if (
(colorCode.startsWith("@[F") && colorCode.endsWith("]")) ||
(colorCode.startsWith("\\t[F") && colorCode.endsWith("]")) ||
(colorCode.startsWith("^[F") && colorCode.endsWith("]"))
) {
const inside = colorCode.includes("\\t")
? colorCode.slice(3, -1)
: colorCode.slice(2, -1);
const r = parseInt(inside.charAt(1), 10);
const g = parseInt(inside.charAt(2), 10);
const b = parseInt(inside.charAt(3), 10);
colorIndex = 16 + 36 * r + 6 * g + b;
} else if (colorCode === "@n") {