-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
library.lua
3370 lines (2854 loc) · 94 KB
/
library.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
-- SCROLL TO BOTTOM ----------------------------------------------------
Set = {}
Set.__index = Set
function Set.new()
local self = setmetatable({}, Set)
self.set = {}
return self
end
function Set:add(key)
self.set[key] = true
end
function Set:remove(key)
self.set[key] = nil
end
function Set:sorted()
local elements = {}
for element in pairs(self.set) do
table.insert(elements, element)
end
table.sort(elements)
return elements
end
Tree = {}
Tree.__index = Tree
function Tree.new(value)
local self = setmetatable({}, Tree)
self.val = value
self.parent = nil
self.children = {}
return self
end
function Tree:__call(...)
local nodes = { ... }
for _, node in ipairs(nodes) do
self:add_child(node)
end
end
function Tree:__tostring()
local function tostring_r(node, depth, childNumber)
local indent = (depth > 0) and string.rep(" ", depth - 1) .. " |- " or ""
local prefix = (depth > 0) and ("Child" .. childNumber .. ": ") or "Root: "
local str = indent .. prefix .. "val = " .. tostring(node.val)
for i, child in ipairs(node.children) do
str = str .. "\n" .. tostring_r(child, depth + 1, i)
end
return str
end
return tostring_r(self, 0, 0)
end
function Tree:add_child(node)
node.parent = self
table.insert(self.children, node)
return self
end
-- TODO Should this return self or the child or both?
function Tree:remove_child(node)
local index
for i, v in ipairs(self.children) do
if v == node then
index = i
break
end
end
if index == nil then return end
table.remove(self.children, index)
return self
end
function Tree:dfs_pre(t)
if not t then t = {} end
table.insert(t, self)
for _, node in ipairs(self.children) do
node:dfs_pre(t)
end
return t
end
function Tree:dfs_post(t)
if not t then t = {} end
for _, node in ipairs(self.children) do
node:dfs_post(t)
end
table.insert(t, self)
return t
end
function Tree:bfs()
local function dequeue(t)
return table.remove(t, 1)
end
local visited = {}
local to_visit = {}
table.insert(to_visit, self)
while #to_visit ~= 0 do
local node = dequeue(to_visit)
table.insert(visited, node)
if node.children then
for _, child in ipairs(node.children) do
table.insert(to_visit, child)
end
end
end
return visited
end
function Tree:add_structure(tree_structure)
for _, v in ipairs(tree_structure) do
local node = Tree.new(v.val)
self:add_child(node)
if v.children then
node:add_structure(v.children)
end
end
return self
end
Gradient = {}
G = Gradient
Gradient.__index = Gradient
Gradient.id = 1
function Gradient.new(vec2a, vec2b, color1, color2)
local self = setmetatable({}, Gradient)
self.element_id = Element.id
Element.id = Element.id + 1
self.class_id = Gradient.id
Gradient.id = Gradient.id + 1
self.vec2a = vec2a or Vec2.new()
self.vec2b = vec2b or Vec2.new()
self.color1 = color1 or Color.new()
self.color2 = color2 or Color.new()
return self
end
function Gradient.__add(self, other)
return self:clone():add(other)
end
function Gradient.__sub(self, other)
return self:clone():sub(other)
end
function Gradient.__mul(self, other)
return self:clone():mult(other)
end
function Gradient.__div(self, other)
return self:clone():div(other)
end
function Gradient.__unm(self)
return self:clone():invert()
end
function Gradient:add(gradient)
local color1 = self.color1:add(gradient.color1)
local color2 = self.color2:add(gradient.color2)
return Gradient.new(self.vec2a, self.vec2b, color1, color2)
end
function Gradient:Add(gradient)
self.color1:Add(gradient.color1)
self.color2:Add(gradient.color2)
return self
end
function Gradient:sub(gradient)
local color1 = self.color1:sub(gradient.color1)
local color2 = self.color2:sub(gradient.color2)
return Gradient.new(self.vec2a, self.vec2b, color1, color2)
end
function Gradient:Sub(gradient)
self.color1:Sub(gradient.color1)
self.color2:Sub(gradient.color2)
return self
end
function Gradient:mult(gradient)
local color1 = self.color1:mult(gradient.color1)
local color2 = self.color2:mult(gradient.color2)
return Gradient.new(self.vec2a, self.vec2b, color1, color2)
end
function Gradient:Mult(gradient)
self.color1:Mult(gradient.color1)
self.color2:Mult(gradient.color2)
return self
end
function Gradient:div(gradient)
local color1 = self.color1:div(gradient.color1)
local color2 = self.color2:div(gradient.color2)
return Gradient.new(self.vec2a, self.vec2b, color1, color2)
end
function Gradient:Div(gradient)
self.color1:Div(gradient.color1)
self.color2:Div(gradient.color2)
return self
end
function Gradient:invert()
local color1 = self.color1:invert()
local color2 = self.color2:invert()
return Gradient.new(self.vec2a, self.vec2b, color1, color2)
end
function Gradient:Invert()
self.color1:Invert()
self.color2:Invert()
return self
end
function Gradient:clone()
return Factory.clone(self)
end
function Gradient:to_paint()
return linear_gradient(
self.vec2a:to_xy_table(),
self.vec2b:to_xy_table(),
self.color1:table(),
self.color2:table()
)
end
Paint = {}
function Paint.create(color, gradient)
if gradient ~= nil then
return gradient:to_paint()
else
return color:to_paint()
end
end
Color = {}
C = Color
Color.__index = Color
Color.id = 1
function Color.new(...)
local self = setmetatable({}, Color)
local args = { ... }
Utils.assign_ids(self)
-- TODO do we need this intermediary private table?
self.__color_table = Color.args_to_color_table(args)
self.r = self.__color_table[1]
self.g = self.__color_table[2]
self.b = self.__color_table[3]
self.a = self.__color_table[4]
return self
end
function Color.args_to_color_table(args)
if Color.args_are_color_table(args) then
return args[1]
elseif Color.args_are_rgba(args) then
return { args[1], args[2], args[3], args[4] }
elseif Color.args_are_rgb(args) then
return { args[1], args[2], args[3], 1 }
elseif Color.args_are_hex_code(args) then
return Color.hex_to_color_table(args[1])
else
return theme.text
end
end
function Color.args_are_rgb(args)
return #args == 3 and
type(args[1]) == "number" and
type(args[2]) == "number" and
type(args[3]) == "number"
end
function Color.args_are_rgba(args)
return #args == 4 and
type(args[1]) == "number" and
type(args[2]) == "number" and
type(args[3]) == "number" and
type(args[4]) == "number"
end
function Color.args_are_color_table(args)
return #args == 1 and
type(args[1]) == "table" and
type(args[1][1]) == "number" and
type(args[1][2]) == "number" and
type(args[1][3]) == "number" and
type(args[1][4]) == "number"
end
function Color.args_are_hex_code(args)
return Color.is_hex_code(args[1])
end
function Color.__add(self, other)
return self:clone():add(other)
end
function Color.__sub(self, other)
return self:clone():sub(other)
end
function Color.__mul(self, other)
return self:clone():mult(other)
end
function Color.__div(self, other)
return self:clone():div(other)
end
function Color.__unm(self)
return self:clone():invert()
end
function Color.is_color(obj)
return getmetatable(obj) == Color
end
function Color.is_hex_code(hex_code)
local s = tostring(hex_code):gsub("#", "")
local invalidChars = string.match(s, "[^0-9a-fA-F]+")
local hasValidChars = invalidChars == nil
local isValidLen = #s == 3 or #s == 4 or #s == 6 or #s == 8
return hasValidChars and isValidLen
end
function Color.hex_to_color_table(hex_code)
local s = tostring(hex_code):gsub("#", "")
local hex_table = {}
if #s == 3 or #s == 4 then
hex_table[1] = s:sub(1, 1):rep(2)
hex_table[2] = s:sub(2, 2):rep(2)
hex_table[3] = s:sub(3, 3):rep(2)
hex_table[4] = (#s == 4) and s:sub(4, 4):rep(2) or "FF"
elseif #s == 6 or #s == 8 then
hex_table[1] = s:sub(1, 2)
hex_table[2] = s:sub(3, 4)
hex_table[3] = s:sub(5, 6)
hex_table[4] = (#s == 8) and s:sub(7, 8) or "FF"
end
local color_table = {}
for i = 1, #hex_table do
color_table[i] = tonumber(hex_table[i], 16) / 255
end
return color_table
end
function Color.is_color_table(table)
return type(table) == "table" and
#table == 4 and
type(table[1]) == "number" and
type(table[2]) == "number" and
type(table[3]) == "number" and
type(table[4]) == "number"
end
function Color.assign_color(object, options)
local c = options.color or Color.new()
if Color.is_color(c) then
object.color = c:clone()
elseif Color.is_color_table(c) then
object.color = Color.new(c)
else
error("Expected a Color instance or a color table.")
end
end
function Color.rgba_to_hsla(color_table)
local r = color_table[1]
local g = color_table[2]
local b = color_table[3]
local a = color_table[4]
local cmin = math.min(r, g, b)
local cmax = math.max(r, g, b)
local delta = cmax - cmin
local l = (cmax + cmin) / 2
local s
if delta == 0 then
s = 0
elseif l < 0.5 then
s = delta / (cmax + cmin)
else
s = delta / (2 - cmax - cmin)
end
local h
if delta == 0 then
h = 0
elseif cmax == r then
h = (g - b) / delta
if g < b then h = h + 6 end
elseif cmax == g then
h = (b - r) / delta + 2
elseif cmax == b then
h = (r - g) / delta + 4
end
h = h * 60
return { h, s, l, a }
end
function Color.hsla_to_rgba(hsla_table)
local h = hsla_table[1]
local s = hsla_table[2]
local l = hsla_table[3]
local a = hsla_table[4]
if s == 0 then
return { l, l, l, a }
end
local function hue_to_rgb(p, q, t)
if t < 0 then t = t + 1 end
if t > 1 then t = t - 1 end
if t < 1 / 6 then return p + (q - p) * 6 * t end
if t < 1 / 2 then return q end
if t < 2 / 3 then return p + (q - p) * (2 / 3 - t) * 6 end
return p
end
local q
if l < 0.5 then
q = l * (1 + s)
else
q = l + s - l * s
end
local p = 2 * l - q
local r = hue_to_rgb(p, q, h / 360 + 1 / 3)
local g = hue_to_rgb(p, q, h / 360)
local b = hue_to_rgb(p, q, h / 360 - 1 / 3)
return { r, g, b, a }
end
function Color.print_swatches(colors)
local size = 10
for i, color in ipairs(colors) do
local x = -i * size
fill_rect({ x, 0 }, { x + size, size }, 0, color:to_paint())
end
end
-- TODO Black, grey, white, etc?
function Color.red()
return Color.new({ 1, 0, 0, 1 })
end
function Color.orange()
return Color.new({ 1, 0.647, 0, 1 })
end
function Color.yellow()
return Color.new({ 1, 1, 0, 1 })
end
function Color.green()
return Color.new({ 0, 1, 0, 1 })
end
function Color.purple()
return Color.new({ 0.5, 0, 0.5, 1 })
end
function Color.blue()
return Color.new({ 0, 0, 1, 1 })
end
function Color:add(color)
local t = Math.vmap(self:table(), color:table(), Math.add)
local color_table = Math.map(t, Math.clamp_normal)
return Color.new(color_table)
end
function Color:sub(color)
local t = Math.vmap(self:table(), color:table(), Math.sub)
local color_table = Math.map(t, Math.clamp_normal)
return Color.new(color_table)
end
function Color:mult(color)
local t = Math.vmap(self:table(), color:table(), Math.mult)
local color_table = Math.map(t, Math.clamp_normal)
return Color.new(color_table)
end
function Color:div(color)
local t = Math.vmap(self:table(), color:table(), Math.div)
local color_table = Math.map(t, Math.clamp_normal)
return Color.new(color_table)
end
function Color:analogous(offset_degrees)
local offset = offset_degrees or 30
local hsla = Color.rgba_to_hsla(self:table())
local hsla1 = { (hsla[1] - offset) % 360, hsla[2], hsla[3], hsla[4] }
local rgba1 = Color.hsla_to_rgba(hsla1)
local color1 = Color.new(rgba1)
local hsla2 = { (hsla[1] + offset) % 360, hsla[2], hsla[3], hsla[4] }
local rgba2 = Color.hsla_to_rgba(hsla2)
local color2 = Color.new(rgba2)
return color1, color2
end
function Color:brightness(brightness)
local r = self.r * brightness
local g = self.g * brightness
local b = self.b * brightness
return Color.new({ r, g, b, self.a })
end
function Color:Brightness(brightness)
self.r = self.r * brightness
self.g = self.g * brightness
self.b = self.b * brightness
return self
end
function Color:clone()
return Color.new({ self.r, self.g, self.b, self.a })
end
function Color:complementary()
local hsla = Color.rgba_to_hsla(self:table())
hsla[1] = (hsla[1] + 180) % 360
local rgba = Color.hsla_to_rgba(hsla)
return Color.new(rgba)
end
function Color:Complimentary()
local hsla = Color.rgba_to_hsla(self:table())
hsla[1] = (hsla[1] + 180) % 360
local rgba = Color.hsla_to_rgba(hsla)
self:Set(rgba)
return self
end
function Color:lerp(color2, t)
local interpolated_color = {}
interpolated_color[1] = self.r + (color2.r - self.r) * t
interpolated_color[2] = self.g + (color2.g - self.g) * t
interpolated_color[3] = self.b + (color2.b - self.b) * t
interpolated_color[4] = self.a + (color2.a - self.a) * t
return Color.new(interpolated_color)
end
function Color:hue(hue_normalized)
local h = hue_normalized * 360
return self:rotate(h)
end
function Color:Hue(hue_normalized)
local h = hue_normalized * 360
self:Rotate(h)
return self
end
function Color:offset_hue(offset_normalized)
local hsla = Color.rgba_to_hsla(self:table())
hsla[1] = hsla[1] + offset_normalized * 360
local rgba = Color.hsla_to_rgba(hsla)
return Color.new(rgba)
end
function Color:Offset_Hue(offset_normalized)
local hsla = Color.rgba_to_hsla(self:table())
hsla[1] = hsla[1] + offset_normalized * 360
local rgba = Color.hsla_to_rgba(hsla)
self:Set(rgba)
return self
end
function Color:invert()
local r = 1 - self.r
local g = 1 - self.g
local b = 1 - self.b
return Color.new({ r, g, b, self.a })
end
function Color:Invert()
self.r = 1 - self.r
self.g = 1 - self.g
self.b = 1 - self.b
return self
end
function Color:lightness(lightness)
local hsla = Color.rgba_to_hsla(self:table())
hsla[3] = lightness
local rgba = Color.hsla_to_rgba(hsla)
return Color.new(rgba)
end
function Color:Lightness(lightness)
local hsla = Color.rgba_to_hsla(self:table())
hsla[3] = lightness
local rgba = Color.hsla_to_rgba(hsla)
self:Set(rgba)
return self
end
function Color:opacity(opacity)
local a = self.a * opacity
return Color.new({ self.r, self.g, self.b, a })
end
function Color:Opacity(opacity)
self.a = self.a * opacity
return self
end
function Color:rotate(degrees)
local hsla = Color.rgba_to_hsla(self:table())
hsla[1] = degrees
local rgba = Color.hsla_to_rgba(hsla)
return Color.new(rgba)
end
function Color:Rotate(degrees)
local hsla = Color.rgba_to_hsla(self:table())
hsla[1] = degrees
local rgba = Color.hsla_to_rgba(hsla)
self:Set(rgba)
return self
end
function Color:saturation(saturation)
local hsla = Color.rgba_to_hsla(self:table())
hsla[2] = saturation
local rgba = Color.hsla_to_rgba(hsla)
return Color.new(rgba)
end
function Color:Saturation(saturation)
local hsla = Color.rgba_to_hsla(self:table())
hsla[2] = saturation
local rgba = Color.hsla_to_rgba(hsla)
self:Set(rgba)
return self
end
function Color:Set(color_table)
self.r = color_table[1]
self.g = color_table[2]
self.b = color_table[3]
self.a = color_table[4]
return self
end
function Color:split_complementary(offset_degrees)
local offset = offset_degrees or 30
local complementary = self:complementary()
return complementary:analogous(offset)
end
function Color:square()
local left, right = self:analogous(90)
return right, self:complementary(), left
end
function Color:table()
return { self.r, self.g, self.b, self.a }
end
function Color:to_paint()
return color_paint(self:table())
end
function Color:triadic()
return self:analogous(120)
end
function Color:tetradic()
local analogous = self:rotate(-60)
return analogous:complementary(), self:complementary(), analogous
end
function Color:print_schemes(origin)
origin = origin or { 0, 0 }
save()
translate(origin)
local vec2 = Vec2.new(5, 2.5)
Text.new("Complementary", { size = 8, vec2 = vec2 }):draw()
local complementary = { self, self:complementary() }
Color.print_swatches(complementary)
translate { 0, -12 }
Text.new("Analogous", { size = 8, vec2 = vec2 }):draw()
local analogous = { self, self:analogous() }
Color.print_swatches(analogous)
translate { 0, -12 }
Text.new("Split Complementary", { size = 8, vec2 = vec2 }):draw()
local split_complementary = { self, self:split_complementary() }
Color.print_swatches(split_complementary)
translate { 0, -12 }
Text.new("Triadic", { size = 8, vec2 = vec2 }):draw()
local triadic = { self, self:triadic() }
Color.print_swatches(triadic)
translate { 0, -12 }
Text.new("Tetradic", { size = 8, vec2 = vec2 }):draw()
local tetradic = { self, self:tetradic() }
Color.print_swatches(tetradic)
translate { 0, -12 }
Text.new("Square", { size = 8, vec2 = vec2 }):draw()
local square = { self, self:square() }
Color.print_swatches(square)
restore()
end
function Color:print(places)
places = places or 2
local element_id = tostring(self.element_id)
local class_id = tostring(self.class_id)
local color_table = tostring(Utils.table_to_string(self.color_table, true, places))
local r = tostring(Math.truncate(self.r, places))
local g = tostring(Math.truncate(self.g, places))
local b = tostring(Math.truncate(self.b, places))
local a = tostring(Math.truncate(self.a, places))
print("-- Color " .. element_id .. ":" .. class_id .. " --")
print(" element_id: " .. element_id)
print(" class_id: " .. class_id)
print(" color_table: " .. color_table)
print(" r: " .. r)
print(" g: " .. g)
print(" b: " .. b)
print(" a: " .. a)
print("")
end
ColorTables = {}
ColorTables.theme = { yellow = { 0.83, 1, 0, 1 } }
ColorTables.html = { indianred = { 205, 92, 92, 1 }, lightcoral = { 240, 128, 128, 1 }, salmon = { 250, 128, 114, 1 }, darksalmon = { 233, 150, 122, 1 }, lightsalmon = { 255, 160, 122, 1 }, crimson = { 220, 20, 60, 1 }, red = { 255, 0, 0, 1 }, firebrick = { 178, 34, 34, 1 }, darkred = { 139, 0, 0, 1 }, pink = { 255, 192, 203, 1 }, lightpink = { 255, 182, 193, 1 }, hotpink = { 255, 105, 180, 1 }, deeppink = { 255, 20, 147, 1 }, mediumvioletred = { 199, 21, 133, 1 }, palevioletred = { 219, 112, 147, 1 }, coral = { 255, 127, 80, 1 }, tomato = { 255, 99, 71, 1 }, orangered = { 255, 69, 0, 1 }, darkorange = { 255, 140, 0, 1 }, orange = { 255, 165, 0, 1 }, gold = { 255, 215, 0, 1 }, yellow = { 255, 255, 0, 1 }, lightyellow = { 255, 255, 224, 1 }, lemonchiffon = { 255, 250, 205, 1 }, lightgoldenrodyellow = { 250, 250, 210, 1 }, papayawhip = { 255, 239, 213, 1 }, moccasin = { 255, 228, 181, 1 }, peachpuff = { 255, 218, 185, 1 }, palegoldenrod = { 238, 232, 170, 1 }, khaki = { 240, 230, 140, 1 }, darkkhaki = { 189, 183, 107, 1 }, lavender = { 230, 230, 250, 1 }, thistle = { 216, 191, 216, 1 }, plum = { 221, 160, 221, 1 }, violet = { 238, 130, 238, 1 }, orchid = { 218, 112, 214, 1 }, fuchsia = { 255, 0, 255, 1 }, magenta = { 255, 0, 255, 1 }, mediumorchid = { 186, 85, 211, 1 }, mediumpurple = { 147, 112, 219, 1 }, rebeccapurple = { 102, 51, 153, 1 }, blueviolet = { 138, 43, 226, 1 }, darkviolet = { 148, 0, 211, 1 }, darkorchid = { 153, 50, 204, 1 }, darkmagenta = { 139, 0, 139, 1 }, purple = { 128, 0, 128, 1 }, indigo = { 75, 0, 130, 1 }, slateblue = { 106, 90, 205, 1 }, darkslateblue = { 72, 61, 139, 1 }, mediumslateblue = { 123, 104, 238, 1 }, greenyellow = { 173, 255, 47, 1 }, chartreuse = { 127, 255, 0, 1 }, lawngreen = { 124, 252, 0, 1 }, lime = { 0, 255, 0, 1 }, limegreen = { 50, 205, 50, 1 }, palegreen = { 152, 251, 152, 1 }, lightgreen = { 144, 238, 144, 1 }, mediumspringgreen = { 0, 250, 154, 1 }, springgreen = { 0, 255, 127, 1 }, mediumseagreen = { 60, 179, 113, 1 }, seagreen = { 46, 139, 87, 1 }, forestgreen = { 34, 139, 34, 1 }, green = { 0, 128, 0, 1 }, darkgreen = { 0, 100, 0, 1 }, yellowgreen = { 154, 205, 50, 1 }, olivedrab = { 107, 142, 35, 1 }, olive = { 128, 128, 0, 1 }, darkolivegreen = { 85, 107, 47, 1 }, mediumaquamarine = { 102, 205, 170, 1 }, darkseagreen = { 143, 188, 139, 1 }, lightseagreen = { 32, 178, 170, 1 }, darkcyan = { 0, 139, 139, 1 }, teal = { 0, 128, 128, 1 }, aqua = { 0, 255, 255, 1 }, cyan = { 0, 255, 255, 1 }, lightcyan = { 224, 255, 255, 1 }, paleturquoise = { 175, 238, 238, 1 }, aquamarine = { 127, 255, 212, 1 }, turquoise = { 64, 224, 208, 1 }, mediumturquoise = { 72, 209, 204, 1 }, darkturquoise = { 0, 206, 209, 1 }, cadetblue = { 95, 158, 160, 1 }, steelblue = { 70, 130, 180, 1 }, lightsteelblue = { 176, 196, 222, 1 }, powderblue = { 176, 224, 230, 1 }, lightblue = { 173, 216, 230, 1 }, skyblue = { 135, 206, 235, 1 }, lightskyblue = { 135, 206, 250, 1 }, deepskyblue = { 0, 191, 255, 1 }, dodgerblue = { 30, 144, 255, 1 }, cornflowerblue = { 100, 149, 237, 1 }, royalblue = { 65, 105, 225, 1 }, blue = { 0, 0, 255, 1 }, mediumblue = { 0, 0, 205, 1 }, darkblue = { 0, 0, 139, 1 }, navy = { 0, 0, 128, 1 }, midnightblue = { 25, 25, 112, 1 }, cornsilk = { 255, 248, 220, 1 }, blanchedalmond = { 255, 235, 205, 1 }, bisque = { 255, 228, 196, 1 }, navajowhite = { 255, 222, 173, 1 }, wheat = { 245, 222, 179, 1 }, burlywood = { 222, 184, 135, 1 }, tan = { 210, 180, 140, 1 }, rosybrown = { 188, 143, 143, 1 }, sandybrown = { 244, 164, 96, 1 }, goldenrod = { 218, 165, 32, 1 }, darkgoldenrod = { 184, 134, 11, 1 }, peru = { 205, 133, 63, 1 }, chocolate = { 210, 105, 30, 1 }, saddlebrown = { 139, 69, 19, 1 }, sienna = { 160, 82, 45, 1 }, brown = { 165, 42, 42, 1 }, maroon = { 128, 0, 0, 1 }, white = { 255, 255, 255, 1 }, snow = { 255, 250, 250, 1 }, honeydew = { 240, 255, 240, 1 }, mintcream = { 245, 255, 250, 1 }, azure = { 240, 255, 255, 1 }, aliceblue = { 240, 248, 255, 1 }, ghostwhite = { 248, 248, 255, 1 }, whitesmoke = { 245, 245, 245, 1 }, seashell = { 255, 245, 238, 1 }, beige = { 245, 245, 220, 1 }, oldlace = { 253, 245, 230, 1 }, floralwhite = { 255, 250, 240, 1 }, ivory = { 255, 255, 240, 1 }, antiquewhite = { 250, 235, 215, 1 }, linen = { 250, 240, 230, 1 }, lavenderblush = { 255, 240, 245, 1 }, mistyrose = { 255, 228, 225, 1 }, gainsboro = { 220, 220, 220, 1 }, lightgray = { 211, 211, 211, 1 }, silver = { 192, 192, 192, 1 }, darkgray = { 169, 169, 169, 1 }, gray = { 128, 128, 128, 1 }, dimgray = { 105, 105, 105, 1 }, lightslategray = { 119, 136, 153, 1 }, slategray = { 112, 128, 144, 1 }, darkslategray = { 47, 79, 79, 1 }, black = { 0, 0, 0, 1 } }
Factory = {}
Factory.__index = Factory
F = Factory
function Factory.new(class, items, options)
local result = {}
for i = 1, #items do
if type(items[i]) == "table" and not getmetatable(items[i]) then
if options then
local args_with_options = { unpack(items[i]) }
table.insert(args_with_options, options)
result[i] = class.new(unpack(args_with_options))
else
result[i] = class.new(unpack(items[i]))
end
else
result[i] = class.new(items[i], options)
end
end
return result
end
function Factory.Iter(instances, method, ...)
local args = { ... } or nil
for i = 1, #instances do
if args then
if type(args) == "table" and not getmetatable(args) then
instances[i][method](instances[i], unpack(args))
else
instances[i][method](instances[i], args)
end
else
instances[i][method](instances[i])
end
end
end
function Factory.iter(instances, method, ...)
local args = { ... } or nil
local results = {}
for i = 1, #instances do
local result
if args then
if type(args) == "table" and not getmetatable(args) then
result = instances[i][method](instances[i], unpack(args))
else
result = instances[i][method](instances[i], args)
end
else
result = instances[i][method](instances[i])
end
table.insert(results, result)
end
return results
end
function Factory.clone(input)
if getmetatable(input) then
local class = getmetatable(input)
local new_instance = class.new()
for key, value in pairs(input) do
if key ~= "element_id" and key ~= "class_id" then
new_instance[key] = value
end
end
return new_instance
end
return Factory.iter(input, "clone", input)
end
-- TODO Create method that will force numbers as strings to keep zeros to x place
Utils = {}
function Utils.has_non_integer_keys(t)
for k, _ in pairs(t) do
if type(k) ~= "number" or k ~= math.floor(k) or k < 1 then
return true
end
end
return false
end
-- TODO Refactor this to be more generic
function Utils.deep_copy_color(color_table)
local copy = {}
for i = 1, #color_table do
copy[i] = color_table[i]
end
return copy
end
function Utils.table_to_string(t, truncate, places)
truncate = truncate or false
places = places or 0
local parts = {}
local function process_value(v)
if truncate and type(v) == "number" then
return Math.truncate(v, places)
elseif type(v) == "table" then
return Utils.table_to_string(v, truncate, places)
else
return tostring(v)
end
end
if Utils.has_non_integer_keys(t) then
for k, v in pairs(t) do
v = process_value(v)
parts[#parts + 1] = tostring(k) .. " = " .. v
end
else
for _, v in ipairs(t) do
v = process_value(v)
parts[#parts + 1] = v
end
end
return "{ " .. table.concat(parts, ", ") .. " }"
end
function Utils.get_peak_memory(interval)
if _PeakMemory == nil then
_PeakMemory = math.floor(collectgarbage("count"))
end
if Time == nil then Time = 0 end
local current_memory_usage = math.floor(collectgarbage("count"))
local truncated_time = math.floor(Time * 100) / 100
if _PeakMemory < current_memory_usage then
_PeakMemory = current_memory_usage
end
if truncated_time % interval == 0 then _PeakMemory = 0 end
return _PeakMemory
end
function Utils.assign_options(instance, options)
for key, value in pairs(options) do
instance[key] = value
end
end
function Utils.resolve_property(instance, key)
local class = getmetatable(instance)
local value = rawget(instance, key)
if value ~= nil then return value end
if class.styles then
local style = rawget(instance, "style") or "normal"
local style_val = class.styles[style][key]
if style_val ~= nil then return style_val end
end
if class.methods then
local method = rawget(instance, "method")
if method ~= nil and class.methods[method] then
local method_val = class.methods[method][key]
if method_val ~= nil then return method_val end
end
end
if class.attrs then
local attr = class.attrs[key]
if attr ~= nil then return attr end
end
return class[key]
end
function Utils.assign_ids(instance)
instance.element_id = Element.id
Element.id = Element.id + 1
instance.class_id = instance.id
getmetatable(instance).id = instance.id + 1
end
function Utils.has_substring(str, substr)
return string.find(str, substr) ~= nil
end
function Utils.get_names(t)
local names = {}
for _, obj in ipairs(t) do
table.insert(names, obj.name)
end
return "{ " .. table.concat(names, ", ") .. " }"
end
Vec2 = {}
V = Vec2
Vec2.__index = Vec2
Vec2.id = 1
function Vec2.new(x, y)
local self = setmetatable({}, Vec2)
self.x = x or 0
self.y = y or 0
Utils.assign_ids(self)
return self
end
-- Metamethods --
function Vec2.__add(self, other)
return self:add(other)
end
function Vec2.__sub(self, other)