forked from unanimated/luaegisub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
colorize.lua
1043 lines (932 loc) · 36.3 KB
/
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="Colorize"
script_description="Does things with colours"
script_author="unanimated"
script_version="3.9"
--[[
Bottom dropdown menu chooses mode:
Colorize letter by letter:
Alternates between 2-5 colours character by character, like 121212, 123123123, or 123412341234.
Works for primary/border/shadow/secondary (only one of those).
Nukes all comments and inline tags. Only first block of tags is kept.
Shift:
Shift can be used on an already colorized line to shift the colours by one letter.
You have to set the right number of colours for it to work correctly!
If shift base is "line", then it takes the colour for the first character from the last character.
"Don't join with other tags" will keep {initial tags}{colour} separated (ie won't nuke the "}{").
This helps some other scripts to keep the colour as part of the "text" without initial tags.
"Continuous shift line by line" - If you select a bunch of the same colorized lines, this shifts the colours line by line.
This kind of requires that no additional weird crap is done to the lines, otherwise malfunctioning can be expected.
"Colorize by word"
Colorizes by word instead of by letter.
"Set colours across whole line"
This is like a preparation for gradient-by-character. Select number of colours.
For 3 colours, it will place one at the start, one in the middle, and one before the last character.
Works for 2-10 colours and sets them evenly across the line. (Then you can run grad-by-char.)
Gradient:
Creates a gradient by character. (Uses Colorize button.)
There are two modes: RGB and HSB. RGB is the standard, like lyger's GBC; HSB interpolates Hue, Saturation, and Brightness separately.
Use the \c, \3c, \4c, \2c checkboxes on the right to choose which colour to gradient.
"Shortest hue" makes sure that hue is interpolated in the shorter direction. Unchecking it will give you a different gradient in 50% cases.
"Double HSB gradient" will make an extra round through Hue. Note that neither of these 2 options applies to RGB.
"Use asterisks" places asterisks like GBC so that you can ungradient the line with lyger's script.
There are several differences from lyger's GBC:
- RGB / HSB option
- You can choose which types of colour you want to gradient
- Other tags don't interfere with the colour gradients
Match/switch/invert \c, \3c, 4c:
This should be obvious from the names and should apply to all new colour tags in the line.
Adjust RGB / HSB
Adjusting Red/Green/Blue or Hue/Saturation/Brightness
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.
"Randomize" - if you set Brightness (any RGB/HSB) to 20, the resulting colour will have anywhere between -20 and +20 of the original brightness.
"Remember last"
Remembers last settings of checkboxes and dropdowns.
"Save config"
Saves a config file in your Application Data folder with current settings.
--]]
re=require'aegisub.re'
-- Colorize --
function colors(subs,sel)
for x, i in ipairs(sel) do
progress(string.format("Colorizing line %d/%d",x,#sel))
line=subs[i]
text=line.text
col1=res.c1:gsub("#(%x%x)(%x%x)(%x%x)","&H%3%2%1&")
col2=res.c2:gsub("#(%x%x)(%x%x)(%x%x)","&H%3%2%1&")
col3=res.c3:gsub("#(%x%x)(%x%x)(%x%x)","&H%3%2%1&")
col4=res.c4:gsub("#(%x%x)(%x%x)(%x%x)","&H%3%2%1&")
col5=res.c5:gsub("#(%x%x)(%x%x)(%x%x)","&H%3%2%1&")
if res.kol=="primary" then k="\\c" text=text:gsub("\\1?c&H%x+&","") end
if res.kol=="border" then k="\\3c" text=text:gsub("\\3c&H%x+&","") end
if res.kol=="shadow" then k="\\4c" text=text:gsub("\\4c&H%x+&","") end
if res.kol=="secondary" then k="\\2c" text=text:gsub("\\2c&H%x+&","") end
k1=k..col1
k2=k..col2
k3=k..col3
k4=k..col4
k5=k..col5
tags=""
if text:match("^{\\[^}]*}") then tags=text:match("^({\\[^}]*})") end
orig=text:gsub("^({\\[^}]*})","")
text=text:gsub("{[^}]*}","")
text=text:gsub("%s*$","")
if res.clrs=="2" then
if res.word then
text=text.." * "
text=re.sub(text,"([\\w[:punct:]]+) ([\\w[:punct:]]+) ","{\\"..k1.."}\\1 {\\"..k2.."}\\2 ")
else
text=text:gsub("%s"," ") text=text.."*"
text=re.sub(text,"([\\w[:punct:]\\s])([\\w[:punct:]\\s])","{\\"..k1.."}\\1{\\"..k2.."}\\2")
text=text:gsub("{\\[1234]?c&H%x+&}%s{\\[1234]?c&H%x+&}%s"," ")
end
end
if res.clrs=="3" then
if res.word then
text=text.." * * "
text=re.sub(text,"([\\w[:punct:]]+) ([\\w[:punct:]]+) ([\\w[:punct:]]+) ","{\\"..k1.."}\\1 {\\"..k2.."}\\2 {\\"..k3.."}\\3 ")
else
text=text:gsub("%s"," ") text=text:gsub("\\N","\\N~") text=text.."**"
text=re.sub(text,"([\\w[:punct:]\\s])([\\w[:punct:]\\s])([\\w[:punct:]\\s])","{\\"..k1.."}\\1{\\"..k2.."}\\2{\\"..k3.."}\\3")
text=text:gsub("{\\[1234]?c&H%x+&}%s{\\[1234]?c&H%x+&}%s{\\[1234]?c&H%x+&}%s"," ")
text=text:gsub("{\\[1234]?c&H%x+&}~","")
end
end
if res.clrs=="4" then
if res.word then
text=text.." * * * "
text=re.sub(text,"([\\w[:punct:]]+) ([\\w[:punct:]]+) ([\\w[:punct:]]+) ([\\w[:punct:]]+) ","{\\"..k1.."}\\1 {\\"..k2.."}\\2 {\\"..k3.."}\\3 {\\"..k4.."}\\4 ")
else
text=text:gsub("%s"," ") text=text:gsub("\\N","\\N\\N") text=text.."***"
text=re.sub(text,"([\\w[:punct:]\\s])([\\w[:punct:]\\s])([\\w[:punct:]\\s])([\\w[:punct:]\\s])","{\\"..k1.."}\\1{\\"..k2.."}\\2{\\"..k3.."}\\3{\\"..k4.."}\\4")
text=text:gsub("{\\[1234]?c&H%x+&}%s{\\[1234]?c&H%x+&}%s{\\[1234]?c&H%x+&}%s{\\[1234]?c&H%x+&}%s"," ")
end
end
if res.clrs=="5" then
if res.word then
text=text.." * * * * "
text=re.sub(text,"([\\w[:punct:]]+) ([\\w[:punct:]]+) ([\\w[:punct:]]+) ([\\w[:punct:]]+) ([\\w[:punct:]]+) ","{\\"..k1.."}\\1 {\\"..k2.."}\\2 {\\"..k3.."}\\3 {\\"..k4.."}\\4 {\\"..k5.."}\\5 ")
else
text=text:gsub("%s"," ") text=text:gsub("\\N","\\N\\N~") text=text.."****"
text=re.sub(text,"([\\w[:punct:]\\s])([\\w[:punct:]\\s])([\\w[:punct:]\\s])([\\w[:punct:]\\s])([\\w[:punct:]\\s])","{\\"..k1.."}\\1{\\"..k2.."}\\2{\\"..k3.."}\\3{\\"..k4.."}\\4{\\"..k5.."}\\5")
text=text:gsub("{\\[1234]?c&H%x+&}%s{\\[1234]?c&H%x+&}%s{\\[1234]?c&H%x+&}%s{\\[1234]?c&H%x+&}%s{\\[1234]?c&H%x+&}%s"," ")
text=text:gsub("{\\[1234]?c&H%x+&}~","")
end
end
text=text:gsub("{\\[1234]?c&H%x+&}%*","")
text=text:gsub("[%s%*]+$","")
text=text:gsub(" $","")
text=text:gsub("{\\[1234]?c&H%x+&}\\{\\[1234]?c&H%x+&}N","\\N")
text=text:gsub("\\N\\N","\\N")
text=tags..text
if res.join==false then text=text:gsub("}{","") end
if orig:match("{%*?\\") then text=textmod(orig) end
line.text=text
subs[i]=line
end
end
function textmod(orig)
tk={}
tg={}
text=text:gsub("{\\\\k0}","")
repeat text=text:gsub("{(\\[^}]-)}{(\\[^}]-)}","{%1%2}")
until not text:match("{(\\[^}]-)}{(\\[^}]-)}")
vis=text:gsub("{[^}]-}","")
ltrmatches=re.find(vis,".")
for l=1,#ltrmatches do
table.insert(tk,ltrmatches[l].str)
end
stags=text:match("^{(\\[^}]-)}")
if stags==nil then stags="" end
text=text:gsub("^{\\[^}]-}","") :gsub("{[^\\}]-}","")
count=0
for seq in orig:gmatch("[^{]-{%*?\\[^}]-}") do
chars,as,tak=seq:match("([^{]-){(%*?)(\\[^}]-)}")
pos=re.find(chars,".")
if pos==nil then ps=0+count else ps=#pos+count end
tgl={p=ps,t=tak,a=as}
table.insert(tg,tgl)
count=ps
end
count=0
for seq in text:gmatch("[^{]-{%*?\\[^}]-}") do
chars,as,tak=seq:match("([^{]-){(%*?)(\\[^}]-)}")
pos=re.find(chars,".")
if pos==nil then ps=0+count else ps=#pos+count end
tgl={p=ps,t=tak,a=as}
table.insert(tg,tgl)
count=ps
end
newline=""
for i=1,#tk do
newline=newline..tk[i]
newt=""
for n, t in ipairs(tg) do
if t.p==i then newt=newt..t.a..t.t end
end
if newt~="" then newline=newline.."{"..newt.."}" end
end
newtext="{"..stags.."}"..newline
text=newtext
return text
end
-- Colours across line --
function gcolors(subs,sel)
cn=tonumber(res.gclrs)
fn=cn-1
-- factors table
fakt={0}
for f=1,fn do
fk=f/fn
table.insert(fakt,fk)
end
-- GUI
gc_config={{x=0,y=0,width=1,height=1,class="dropdown",name="gctype",items={"\\c","\\3c","\\4c","\\2c"},value="\\c"}}
for c=1,cn do
cte={x=c,y=0,width=1,height=1,class="color",name="gc"..c}
table.insert(gc_config,cte)
end
button={"This is a rather big button","Click this, and something might happen","What is this, I don't even","The do-not-cancel button","I accept the terms of this scam","Is this really the right button?","What do I do with all these colours?!","I sure hope nothing will break if I do this","Yeah, okay. Fine. I'm gonna click on this.","Is this button safe to click?","Anyone else feels like this is a bit random?","We interrupt your typesetting to bring you a button!","I assure you this script actually works (maybe)","No, but seriously, click me!"}
ex=math.random(1,#button)
if not res.rept then press,rez=aegisub.dialog.display(gc_config,{button[ex],"Cancel"},{close='Cancel'}) end
if press=="Cancel" then aegisub.cancel() end
kt=rez.gctype
-- colours table
kolors={}
for c=1,cn do
gcol=rez["gc"..c]:gsub("#(%x%x)(%x%x)(%x%x)","&H%3%2%1&")
gcol=kt..gcol
table.insert(kolors,gcol)
end
for x, i in ipairs(sel) do
progress(string.format("Colorizing line %d/%d",x,#sel))
line=subs[i]
text=line.text
text=text:gsub("\\1c","\\c") :gsub(kt.."&H%x+&","")
if not text:match("^{\\") then text=text:gsub("^","{\\clrs}") end
clean=text:gsub("{[^}]-}","") :gsub("%s?\\[Nn]%s?"," ")
text=text:gsub("%*","_ast_")
for n=cn,1,-1 do
lngth=math.floor(clean:len()*fakt[n])
text="*"..text
text=text:gsub("%*({\\[^}]-})","%1*")
tags=kolors[n]
m=0
if lngth>0 then
repeat text=text:gsub("%*({[^}]-})","%1*") :gsub("%*(.)","%1*") :gsub("%*(%s?\\[Nn]%s?)","%1*") m=m+1
until m==lngth
end
if n==cn then text=text:gsub("([^}])%*$","*%1") :gsub("([^}])%*({[^\\}]-})$","*%1%2") end
text=text:gsub("%*","{"..tags.."}") :gsub("({"..tags.."})({[^}]-})","%2%1")
:gsub("{(\\[^}]-)}{(\\[^}]-)}","{%1%2}") :gsub("("..kt.."&H%x+&)"..kt.."&H%x+&","%1")
end
text=text:gsub("\\clrs","") :gsub("_ast_","*") :gsub("{}","")
line.text=text
subs[i]=line
end
end
-- Shift colours --
function shift(subs,sel)
klrs=tonumber(res.clrs) -- how many colours we're dealing with
count=1 -- start line counter
if res.shit=="line" then sline=true else sline=false end
for x, i in ipairs(sel) do
progress(string.format("Colorizing line %d/%d",x,#sel))
line=subs[i]
text=line.text
-- check if line looks colorized
if not text:match("{(\\[1234]?c)&H%x+&}[%w%p]") then aegisub.dialog.display({{class="label",
label="Line "..x.." does not \nappear to be colorized",x=0,y=0,width=1,height=2}},{"OK"}) aegisub.cancel()
end
-- determine which colour has been used to colorize - 1c, 2c, 3c, 4c
if sline then
matches=re.find(text,"\\{\\*?\\\\[1234]?c&H[A-Fa-f0-9]+&\\}[^\\{]*$")
cms=matches[1].str
ctype,shc=cms:match("{%*?(\\[1234]?c)(&H%x+&)}[^{]*$")
first="{"..ctype..shc.."}"
else
matches=re.find(text,"\\{\\\\[1234]?c&H[A-Fa-f0-9]+&\\}[\\w[:punct:]]")
cms=matches[1].str
ctype=cms:match("\\[1234]?c")
-- get colours 2, 3, 4, 5, and create sequences for shifting
matches=re.match(text,"([\\w[:punct:]]\\s?)(\\{\\"..ctype.."&H[A-Fa-f0-9]+&\\})([\\w[:punct:]]\\s?)(\\{\\"..ctype.."&H[A-Fa-f0-9]+&\\})([\\w[:punct:]]\\s?)(\\{\\"..ctype.."&H[A-Fa-f0-9]+&\\})([\\w[:punct:]]\\s?)(\\{\\"..ctype.."&H[A-Fa-f0-9]+&\\})")
if matches==nil then
matches=re.match(text,"([\\w[:punct:]]\\s?)(\\{\\"..ctype.."&H[A-Fa-f0-9]+&\\})([\\w[:punct:]]\\s?)(\\{\\"..ctype.."&H[A-Fa-f0-9]+&\\})")
c2=matches[3].str c3=matches[5].str
else
c2=matches[3].str c3=matches[5].str c4=matches[7].str c5=matches[9].str
end
if klrs==2 then first=c2 end
if klrs==3 then first=c3 second=c2 end
if klrs==4 then first=c4 second=c3 third=c2 end
if klrs==5 then first=c5 second=c4 third=c3 fourth=c2 end
end
-- don't run for 1st lines in sequences
if count>1 or not res.cont then
-- separate first colour tag from other tags, save initial tags
tags=""
if text:match("^{[^}]*"..ctype.."&") then text=text:gsub("^({[^}]*)("..ctype.."&H%x+&)([^}]*})","%1%3{%2}") end
if not text:match("^{\\[1234]?c&H%x+&}") then tags=text:match("^({\\[^}]*})") text=text:gsub("^{\\[^}]*}","") end
-- shifting colours happens here
switch=1
repeat
text=re.sub(text, "(\\{\\*?\\\\[1234]?c&H[A-Fa-f0-9]+&\\})([\\w[:punct:]])", "\\2\\1")
text=re.sub(text, "(\\{\\*?\\\\[1234]?c&H[A-Fa-f0-9]+&\\})(\\s)", "\\2\\1")
text=re.sub(text, "(\\{\\*?\\\\[1234]?c&H[A-Fa-f0-9]+&\\})(\\\\N)", "\\2\\1")
text=re.sub(text, "(\\{\\*?\\\\[1234]?c&H[A-Fa-f0-9]+&\\})$", "")
text=first..text
switch=switch+1
if not sline then
if switch==2 then first=second end
if switch==3 then first=third end
if switch==4 then first=fourth end
else
matches=re.find(text,"\\{\\\\[1234]?c&H[A-Fa-f0-9]+&\\}[^\\{]*$")
ctype,shc=cms:match("{%*?(\\[1234]?c)(&H%x+&)}[^{]*$")
first="{"..ctype..shc.."}"
end
for cl1,cl2,cl3 in text:gmatch("({\\[1234]?c&H%x+&})([%w%p%s])({\\[1234]?c&H%x+&})") do
if cl1==cl3 then
text=text:gsub(cl1..cl2..cl3,cl1..cl2)
end
end
until switch>=count
if tags~=nil then text=tags..text end
if res.join==false then text=text:gsub("}{","") end
end
-- line counter
if res.cont then count=count+1 end
if not sline and count>klrs then count=1 end
line.text=text
subs[i]=line
end
end
function stylecolours()
stylecol={}
primary=styleref.color1:gsub("H%x%x","H") sc1=primary table.insert(stylecol,sc1)
pri=text:match("^{[^}]-\\c(&H%x+&)") if pri~=nil then primary=pri end
secondary=styleref.color2:gsub("H%x%x","H") sc2=secondary table.insert(stylecol,sc2)
sec=text:match("^{[^}]-\\3c(&H%x+&)") if sec~=nil then secondary=sec end
outline=styleref.color3:gsub("H%x%x","H") sc3=outline table.insert(stylecol,sc3)
out=text:match("^{[^}]-\\3c(&H%x+&)") if out~=nil then outline=out end
shadow=styleref.color4:gsub("H%x%x","H") sc4=shadow table.insert(stylecol,sc4)
sha=text:match("^{[^}]-\\c(&H%x+&)") if sha~=nil then shadow=sha end
return stylecol
end
-- Match colours --
function matchcolors(subs,sel)
for x, i in ipairs(sel) do
progress(string.format("Colorizing line %d/%d",x,#sel))
line=subs[i]
text=line.text
if defaref~=nil and line.style=="Default" then styleref=defaref
elseif lastref~=nil and laststyle==line.style then styleref=lastref
else styleref=stylechk(line.style) end
lastref=styleref laststyle=line.style
stylecol=stylecolours()
if res.kol=="primary" then k="\\c" end
if res.kol=="border" then k="\\3c" end
if res.kol=="shadow" then k="\\4c" end
if res.kol=="secondary" then k="\\2c" end
text=text:gsub("\\1c","\\c")
if not text:match("^{\\") then text=text:gsub("^","{\\}") end
-- 1-->3 match outline to primary
if pressed=="Match Colours" and res.match13 then
for ctags in text:gmatch("({\\[^}]-})") do
ctags2=nil
if ctags:match("\\3c") and not ctags:match("\\1?c") then ctags2=ctags:gsub("\\3c&H%w+&","\\3c"..primary) end
if ctags:match("\\1?c") and ctags:match("\\3c") then
tempc=ctags:match("\\1?c(&H%w+&)") ctags2=ctags:gsub("\\3c&H%w+&","\\3c"..tempc) end
if ctags:match("\\1?c") and not ctags:match("\\3c") then
ctags2=ctags:gsub("\\1?c(&H%w+&)","\\c%1\\3c%1") end
if ctags==text:match("^({\\[^}]-})") and not ctags:match("\\3c") and not ctags:match("\\1?c") then
ctags2=ctags:gsub("^({\\[^}]-)}","%1\\3c"..primary.."}") end
if ctags2~=nil then ctags=esc(ctags) text=text:gsub(ctags,ctags2) end
end
end
-- 3-->1 match primary to outline
if pressed=="Match Colours" and res.match31 then
for ctags in text:gmatch("({\\[^}]-})") do
ctags2=nil
if ctags:match("\\1?c") and not ctags:match("\\3c") then ctags2=ctags:gsub("\\1?c&H%w+&","\\c"..outline) end
if ctags:match("\\1?c") and ctags:match("\\3c") then
tempc=ctags:match("\\3c(&H%w+&)") ctags2=ctags:gsub("\\1?c&H%w+&","\\c"..tempc) end
if ctags:match("\\3c") and not ctags:match("\\1?c") then
ctags2=ctags:gsub("\\3c(&H%w+&)","\\c%1\\3c%1") end
if ctags==text:match("^({\\[^}]-})") and not ctags:match("\\1?c") and not ctags:match("\\3c") then
ctags2=ctags:gsub("^({\\[^}]-)}","%1\\c"..outline.."}") end
if ctags2~=nil then ctags=esc(ctags) text=text:gsub(ctags,ctags2) end
end
end
-- 1-->4 match shadow to primary
if pressed=="Match Colours" and res.match14 then
for ctags in text:gmatch("({\\[^}]-})") do
ctags2=nil
if ctags:match("\\4c") and not ctags:match("\\c") then ctags2=ctags:gsub("\\4c&H%w+&","\\4c"..primary) end
if ctags:match("\\4c") and ctags:match("\\c") then
tempc=ctags:match("\\c(&H%w+&)") ctags2=ctags:gsub("\\4c&H%w+&","\\4c"..tempc) end
if ctags:match("\\c") and not ctags:match("\\4c") then
ctags2=ctags:gsub("\\c(&H%w+&)","\\c%1\\4c%1") end
if ctags==text:match("^({\\[^}]-})") and not ctags:match("\\4c") and not ctags:match("\\c") then
ctags2=ctags:gsub("^({\\[^}]-)}","%1\\4c"..primary.."}") end
if ctags2~=nil then ctags=esc(ctags) text=text:gsub(ctags,ctags2) end
end
end
-- 3-->4 match shadow to outline
if pressed=="Match Colours" and res.match34 then
for ctags in text:gmatch("({\\[^}]-})") do
ctags2=nil
if ctags:match("\\4c") and not ctags:match("\\3c") then ctags2=ctags:gsub("\\4c&H%w+&","\\4c"..outline) end
if ctags:match("\\4c") and ctags:match("\\3c") then
tempc=ctags:match("\\3c(&H%w+&)") ctags2=ctags:gsub("\\4c&H%w+&","\\4c"..tempc) end
if ctags:match("\\3c") and not ctags:match("\\4c") then
ctags2=ctags:gsub("\\3c(&H%w+&)","\\3c%1\\4c%1") end
if ctags==text:match("^({\\[^}]-})") and not ctags:match("\\4c") and not ctags:match("\\3c") then
ctags2=ctags:gsub("^({\\[^}]-)}","%1\\4c"..outline.."}") end
if ctags2~=nil then ctags=esc(ctags) text=text:gsub(ctags,ctags2) end
end
end
-- 1<-->3 switch primary and border
if pressed=="Match Colours" and res.match131 then
if text:match("^{\\") then
tags=text:match("^({\\[^}]-})")
if tags:match("\\1?c") then tags=tags:gsub("\\1?c","\\tempc")
else tags=tags:gsub("({\\[^}]-)}","%1\\tempc"..primary.."}") end
if tags:match("\\3c") then tags=tags:gsub("\\3c","\\c")
else tags=tags:gsub("({\\[^}]-)}","%1\\c"..outline.."}") end
tags=tags:gsub("\\tempc","\\3c")
after=text:match("^{\\[^}]-}(.*)")
after=after:gsub("\\1?c","\\tempc")
after=after:gsub("\\3c","\\c")
after=after:gsub("\\tempc","\\3c")
text=tags..after
else
tags="{\\c"..outline.."\\3c"..primary.."}"
after=text
after=after:gsub("\\1?c","\\tempc")
after=after:gsub("\\3c","\\c")
after=after:gsub("\\tempc","\\3c")
text=tags..after
end
end
-- Invert All Colours
if pressed=="Match Colours" and res.invert then
if not text:match("^{\\") then text="{\\what}"..text end
tags=text:match("^({\\[^}]-})")
for n=1,4 do
ctg="\\"..n.."c"
ctg=ctg:gsub("1","")
if not tags:match(ctg) and n~=2 then text=text:gsub("^({\\[^}]-)}","%1"..ctg..stylecol[n].."}") end
end
for color in text:gmatch("\\[1234]?c&H(%x%x%x%x%x%x)&") do
icolor=""
for kol in color:gmatch("(%x%x)") do
dkol=tonumber(kol,16)
idkol=255-dkol
ikol=tohex(idkol)
icolor=icolor..ikol
end
text=text:gsub("&H"..color.."&","&H"..icolor.."&")
end
text=text:gsub("\\what","")
end
-- RGB / HSB
if pressed=="RGB" or pressed=="HSB" then
lvlr=res.R lvlg=res.G lvlb=res.B
hue=res.huehue
sat=res.satur
brite=res.bright
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
tagz=text:match("^({\\[^}]-})")
for i=1,#corols do
n=tonumber(corols[i])
local kl="\\"..n.."c"
kl=kl:gsub("\\1c","\\c")
if res.mktag and not tagz:match(kl) then
text=text:gsub("^({\\[^}]-)}","%1"..kl..stylecol[n].."}")
end
-- R G B --
if pressed=="RGB" then
for kol1,kol2,kol3 in text:gmatch(kl.."&H(%x%x)(%x%x)(%x%x)&") do
kol1n=brightness(kol1,lvlb)
kol2n=brightness(kol2,lvlg)
kol3n=brightness(kol3,lvlr)
text=text:gsub(kol1..kol2..kol3,kol1n..kol2n..kol3n)
end
end
-- H S B --
if pressed=="HSB" then
for kol1,kol2,kol3 in text:gmatch(kl.."&H(%x%x)(%x%x)(%x%x)&") do
H1,S1,L1=RGB_to_HSL(kol3,kol2,kol1)
H=H1+hue/255
S=S1+sat/255
L=L1+brite/255
H,S,L=HSLround(H,S,L)
if randomize then
H2=H1-hue/255
S2=S1-sat/255
L2=L1-brite/255
H2,S2,L2=HSLround(H2,S2,L2)
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
kol3n,kol2n,kol1n=HSL_to_RGB(H,S,L)
kol3n=tohex(round(kol3n))
kol2n=tohex(round(kol2n))
kol1n=tohex(round(kol1n))
text=text:gsub(kol1..kol2..kol3,kol1n..kol2n..kol3n)
end
end
end
end
text=text:gsub("\\\\","\\") :gsub("\\}","}") :gsub("{}","")
line.text=text
subs[i]=line
end
end
function RGB_to_HSL(Red,Green,Blue)
R=(tonumber(Red,16)/255)
G=(tonumber(Green,16)/255)
B=(tonumber(Blue,16)/255)
Min=math.min(R,G,B)
Max=math.max(R,G,B)
del_Max=Max-Min
L=(Max+Min)/2
if del_Max==0 then H=0 S=0
else
if L<0.5 then S=del_Max/(Max+Min)
else S=del_Max/(2-Max-Min)
end
del_R=(((Max-R)/6)+(del_Max/2))/del_Max
del_G=(((Max-G)/6)+(del_Max/2))/del_Max
del_B=(((Max-B)/6)+(del_Max/2))/del_Max
if R==Max then H=del_B-del_G
elseif G==Max then H=(1/3)+del_R-del_B
elseif B==Max then H=(2/3)+del_G-del_R
end
if H<0 then H=H+1 end
if H>1 then H=H-1 end
end
return H,S,L
end
function HSL_to_RGB(H,S,L)
if S==0 then
R=L*255
G=L*255
B=L*255
else
if L<0.5 then var_2=L*(1+S)
else var_2=(L+S)-(S*L)
end
var_1=2*L-var_2
R=255*Hue_to_RGB(var_1,var_2,H+(1/3))
G=255*Hue_to_RGB(var_1,var_2,H)
B=255*Hue_to_RGB(var_1,var_2,H-(1/3))
end
return R,G,B
end
function Hue_to_RGB(v1,v2,vH)
if vH<0 then vH=vH+1 end
if vH>1 then vH=vH-1 end
if (6*vH)<1 then return(v1+(v2-v1)*6*vH) end
if (2*vH)<1 then return(v2) end
if (3*vH)<2 then return(v1+(v2-v1)*((2/3)-vH)*6) end
return(v1)
end
function HSLround(H,S,L)
if H>1 then H=H-1 end
if H<0 then H=H+1 end
if S>1 then S=1 end
if S<0 then S=0 end
if L>1 then L=1 end
if L<0 then L=0 end
return H,S,L
end
function brightness(klr,lvl)
klr=tonumber(klr,16)
if randomize then
rAn=math.random(klr-lvl,klr+lvl)
klr=round(rAn)
else
klr=klr+lvl
end
if klr<0 then klr=0 end
if klr<10 then klr="0"..klr else klr=tohex(klr) end
return klr
end
function tohex(num)
n1=math.floor(num/16)
n2=num%16
num=tohex1(n1)..tohex1(n2)
return num
end
function tohex1(num)
if num<1 then num="0"
elseif num>14 then num="F"
elseif num==10 then num="A"
elseif num==11 then num="B"
elseif num==12 then num="C"
elseif num==13 then num="D"
elseif num==14 then num="E" end
return num
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 x, i in ipairs(sel) do
progress(string.format("Colorizing line %d/%d",x,#sel))
line=subs[i]
text=line.text
text=text:gsub("\\c&","\\1c&")
after=text:gsub("^{\\[^}]-}","")
if text:match("{\\[^}]-}$") then text=text.."wtfwhywouldyoudothis" end
-- colours from style
styleref=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]
-- backup original text + save tags
orig=text
tags=text:match("^{\\[^}]-}") if tags==nil then tags="" end
-- leave only releavant colour tags, nuke all other ones, add colour from style if missing at the start
ctext=text:gsub("\\[^1234][^c][^\\}]+","") :gsub("{%*?}","") :gsub("\\[^"..ac.."]c[^\\}]+","") :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
nR1=(tonumber(R1,16)) nR2=(tonumber(R2,16))
nG1=(tonumber(G1,16)) nG2=(tonumber(G2,16))
nB1=(tonumber(B1,16)) nB2=(tonumber(B2,16))
Rdiff=(nR2-nR1)/posi[c+1] R=nR1+Rdiff*l
Gdiff=(nG2-nG1)/posi[c+1] G=nG1+Gdiff*l
Bdiff=(nB2-nB1)/posi[c+1] B=nB1+Bdiff*l
else -- HSL
Hdiff=(H2-H1)/posi[c+1] H=H1+Hdiff*l
Sdiff=(S2-S1)/posi[c+1] S=S1+Sdiff*l
Ldiff=(L2-L1)/posi[c+1] L=L1+Ldiff*l
R,G,B=HSL_to_RGB(H,S,L)
end
R=tohex(round(R))
G=tohex(round(G))
B=tohex(round(B))
-- colour + letter
ncol="{"..ast.."\\"..ac.."c&H"..B..G..R.."&}"
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)
end
text=text:gsub("({%*?\\[^}]-})",function(tg) return colkill(tg) end)
:gsub("wtfwhywouldyoudothis","")
:gsub("([^{])%*\\","%1\\")
line.text=text
subs[i]=line
end
end
function colkill(tagz)
tagz=tagz:gsub("\\1c&","\\c&")
tags2={"c","2c","3c","4c"}
for i=1,#tags2 do
tag=tags2[i]
tagz=tagz:gsub("\\"..tag.."&H%x+&([^}]-)(\\"..tag.."&H%x+&)","%2%1")
end
return tagz
end
function styleget(subs)
styles={}
for i=1, #subs do
if subs[i].class=="style" then
table.insert(styles,subs[i])
end
if subs[i].class=="dialogue" then break end
end
end
function stylechk(stylename)
for i=1,#styles do
if stylename==styles[i].name then
styleref=styles[i]
if styles[i].name=="Default" then defaref=styles[i] end
break
end
end
return styleref
end
function esc(str)
str=str
:gsub("%%","%%%%")
:gsub("%(","%%%(")
:gsub("%)","%%%)")
:gsub("%[","%%%[")
:gsub("%]","%%%]")
:gsub("%.","%%%.")
:gsub("%*","%%%*")
:gsub("%-","%%%-")
:gsub("%+","%%%+")
:gsub("%?","%%%?")
return str
end
function round(num) num=math.floor(num+0.5) return num end
function progress(msg)
cancelled=aegisub.progress.is_cancelled()
if cancelled then aegisub.cancel() end
aegisub.progress.title(msg)
end
function repetition()
if res.rept then
res.clrs=lastclrs
res.shit=lastshit
res.kol=lastkol
res.c1=lastc1
res.c2=lastc2
res.c3=lastc3
res.c4=lastc4
res.c5=lastc5
res.join=lastjoin
res.cont=lastcont
res.word=lastword
res.R=lastR
res.G=lastG
res.B=lastB
res.bright=lastbright
res.k1=lastk1
res.k2=lastk2
res.k3=lastk3
res.k4=lastk4
res.mktag=lastmktag
res.gclrs=lastgclrs
res.gcl=lastgcl
res.match13=lastmatch13
res.match31=lastmatch31
res.match14=lastmatch14
res.match34=lastmatch34
res.match131=lastmatch131
res.invert=lastinvert
res.grad=lastgrad
res.grtype=lastgrtype
res.hueshort=lasthueshort
res.double=lastdouble
end
end
function tf(val)
if val==true then ret="true" else ret="false" end
return ret
end
function detf(txt)
if txt=="true" then ret=true
elseif txt=="false" then ret=false
else ret=txt end
return ret
end
function saveconfig()
colconf=
"Colorize Configutation\n"..
"\nclrs:"..res.clrs..
"\nshit:"..res.shit..
"\nkol:"..res.kol..
"\njoin:"..tf(res.join)..
"\ncont:"..tf(res.cont)..
"\nword:"..tf(res.word)..
"\ngcl:"..tf(res.gcl)..
"\ngclrs:"..res.gclrs..
"\ngrad:"..tf(res.grad)..
"\nhueshort:"..tf(res.hueshort)..
"\ngrtype:"..res.grtype..
"\ndouble:"..tf(res.double)..
"\nast:"..tf(res.ast)..
"\nk1:"..tf(res.k1)..
"\nk2:"..tf(res.k2)..
"\nk3:"..tf(res.k3)..
"\nk4:"..tf(res.k4)..
"\nmatch13:"..tf(res.match13)..
"\nmatch31:"..tf(res.match31)..
"\nmatch14:"..tf(res.match14)..
"\nmatch34:"..tf(res.match34)..
"\nmatch131:"..tf(res.match131)..
"\nmktag:"..tf(res.mktag)..
"\ninvert:"..tf(res.invert)..
"\nrem:"..tf(res.rem).."\n"
colorconfig=aegisub.decode_path("?user").."\\colorize.conf"
file=io.open(colorconfig,"w")
file:write(colconf)
file:close()
aegisub.dialog.display({{class="label",label="Config Saved to:\n"..colorconfig}},{"OK"},{close='OK'})
end
function loadconfig()
colorconfig=aegisub.decode_path("?user").."\\colorize.conf"
file=io.open(colorconfig)
if file~=nil then
konf=file:read("*all")
io.close(file)
for key,val in ipairs(colorfiguration) do
if val.class=="checkbox" or val.class=="dropdown" then
if konf:match(val.name) then val.value=detf(konf:match(val.name..":(.-)\n")) end
end
end
end
end
function colorize(subs,sel)
colorfiguration=
{
{x=0,y=0,width=1,height=1,class="label",label="Colours"},
{x=1,y=0,width=2,height=1,class="dropdown",name="clrs",items={"2","3","4","5"},value="2"},
{x=0,y=1,width=1,height=1,class="label",label="Shift base:"},
{x=1,y=1,width=2,height=1,class="dropdown",name="shit",items={"# of colours","line"},value="# of colours"},
{x=0,y=2,width=1,height=1,class="label",label="Apply to: "},
{x=1,y=2,width=2,height=1,class="dropdown",name="kol",items={"primary","border","shadow","secondary"},value="primary"},
{x=4,y=0,width=1,height=1,class="label",label=" 1 "},
{x=4,y=1,width=1,height=1,class="label",label=" 2 "},
{x=4,y=2,width=1,height=1,class="label",label=" 3 "},
{x=4,y=3,width=1,height=1,class="label",label=" 4 "},
{x=4,y=4,width=1,height=1,class="label",label=" 5 "},
{x=5,y=0,width=1,height=1,class="color",name="c1" },
{x=5,y=1,width=1,height=1,class="color",name="c2" },
{x=5,y=2,width=1,height=1,class="color",name="c3" },
{x=5,y=3,width=1,height=1,class="color",name="c4" },
{x=5,y=4,width=1,height=1,class="color",name="c5" },
{x=0,y=3,width=3,height=1,class="checkbox",name="join",label="Don't join with other tags",value=false },
{x=0,y=4,width=4,height=1,class="checkbox",name="cont",label="Continuous shift line by line",value=false },
{x=0,y=5,width=5,height=1,class="checkbox",name="gcl",label="Set colours across whole line:",value=false },
{x=5,y=5,width=1,height=1,class="dropdown",name="gclrs",items={"2","3","4","5","6","7","8","9","10"},value="3"},
{x=0,y=6,width=3,height=1,class="checkbox",name="word",label="Colorize by word",value=false },
{x=0,y=7,width=3,height=1,class="checkbox",name="rept",label="Repeat with last settings",value=false },
{x=6,y=0,width=1,height=1,class="label",label=" "},
{x=7,y=2,width=1,height=1,class="label",label="Red: "},
{x=8,y=2,width=3,height=1,class="intedit",name="R",value=0,min=-255,max=255},
{x=7,y=3,width=1,height=1,class="label",label="Green: "},
{x=8,y=3,width=3,height=1,class="intedit",name="G",value=0,min=-255,max=255},
{x=7,y=4,width=1,height=1,class="label",label="Blue: "},
{x=8,y=4,width=3,height=1,class="intedit",name="B",value=0,min=-255,max=255},
{x=7,y=5,width=1,height=1,class="label",label="Hue:"},
{x=8,y=5,width=3,height=1,class="intedit",name="huehue",value=0,min=-255,max=255},
{x=7,y=6,width=1,height=1,class="label",label="Saturation:"},
{x=8,y=6,width=3,height=1,class="intedit",name="satur",value=0,min=-255,max=255},
{x=7,y=7,width=1,height=1,class="label",label="Brightness:"},
{x=8,y=7,width=3,height=1,class="intedit",name="bright",value=0,min=-255,max=255},
{x=7,y=8,width=1,height=1,class="checkbox",name="k1",label="\\c ",value=true },
{x=8,y=8,width=1,height=1,class="checkbox",name="k3",label="\\3c ",value=false },
{x=9,y=8,width=1,height=1,class="checkbox",name="k4",label="\\4c ",value=false },
{x=10,y=8,width=1,height=1,class="checkbox",name="k2",label="\\2c",value=false },
{x=7,y=9,width=2,height=1,class="checkbox",name="mktag",label="Apply to missing",hint="Apply even to colours without tags in line",value=false },
{x=9,y=9,width=2,height=1,class="checkbox",name="randoom",label="Randomize",hint="",value=false },
{x=7,y=0,width=1,height=1,class="label",label="Match col.:"},
{x=8,y=0,width=1,height=1,class="checkbox",name="match13",label="c->3c ",value=false,hint="copy primary to outline"},
{x=9,y=0,width=1,height=1,class="checkbox",name="match31",label="3c->c",value=false,hint="copy outline to primary"},
{x=7,y=1,width=1,height=1,class="checkbox",name="match14",label="c->4c",value=false,hint="copy primary to shadow"},
{x=8,y=1,width=1,height=1,class="checkbox",name="match34",label="3c->4c",value=false,hint="copy outline to shadow"},
{x=9,y=1,width=1,height=1,class="checkbox",name="match131",label="c<->3c",value=false,hint="switch primary and outline"},
{x=10,y=1,width=1,height=1,class="checkbox",name="invert",label="Invert",value=false,hint="invert colours"},
{x=10,y=0,width=1,height=1,class="label",label="[ver. "..script_version.."]"},
{x=0,y=8,width=2,height=1,class="checkbox",name="grad",label="Gradient ",value=false},
{x=2,y=8,width=3,height=1,class="checkbox",name="hueshort",label="Shortest hue",value=true},
{x=5,y=8,width=1,height=1,class="dropdown",name="grtype",items={"RGB","HSB"},value="HSB"},
{x=0,y=9,width=3,height=1,class="checkbox",name="double",label="Double HSB gradient",value=false},
{x=3,y=9,width=3,height=1,class="checkbox",name="ast",label="Use asterisks",value=false},
{x=3,y=6,width=3,height=1,class="checkbox",name="conf",label="Save config",value=false,hint="Saves current configuration\n(for most things)"},
{x=3,y=7,width=3,height=1,class="checkbox",name="rem",label="Remember last",value=false,hint="remember last settings"},