-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.lua
415 lines (328 loc) · 10.4 KB
/
init.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
-- from incredible-gmod.ru with love <3
-- https://github.com/Be1zebub/Color.lua
local Color = {}
local function IsColor(var)
return getmetatable(var) == Color
end
do -- meta events
Color.__index = Color
function Color:__tostring()
return string.format("Color(%d, %d, %d, %d)", self.r, self.g, self.b, self.a)
end
function Color:__concat(v)
return tostring(self) .. (IsColor(v) and (" ".. tostring(v)) or v)
end
function Color:__unm()
return self:Copy():Invert()
end
function Color:__add(other)
return Color(self.r + other.r, self.g + other.g, self.b + other.b, self.a + other.a)
end
function Color:__sub(other)
return Color(self.r - other.r, self.g - other.g, self.b - other.b, self.a - other.a)
end
function Color:__mul(other)
if type(other) == "number" then
return Color(self.r * other, self.g * other, self.b * other, self.a * other)
end
return Color(self.r * other.r, self.g * other.g, self.b * other.b, self.a * other.a)
end
function Color:__div(other)
if type(other) == "number" then
return Color(self.r / other, self.g / other, self.b / other, self.a / other)
end
return Color(self.r / other.r, self.g / other.g, self.b / other.b, self.a / other.a)
end
function Color:__eq(other)
return self.r == other.r and self.g == other.g and self.b == other.b and self.a == other.a
end
function Color:__lt(other) -- is darker then other
return select(3, self:ToHSL()) < select(3, other:ToHSL())
end
function Color:__le(other) -- is dark or darker then other
return select(3, self:ToHSL()) <= select(3, other:ToHSL())
end
end
do -- methods
function Color:Unpack()
return self.r, self.g, self.b, self.a
end
function Color:SetUnpacked(r, g, b, a)
self.r, self.g, self.b, self.a = r or self.r, g or self.g, b or self.b, a or self.a
return self
end
function Color:Normalize()
self.r, self.g, self.b, self.a = self.r / 255, self.g / 255, self.b / 255, self.a / 255
return self
end
function Color:GetNormalized()
return self:Copy():Normalize()
end
function Color:String(str)
return string.format("\27[38;2;%d;%d;%dm%s\27[0m", self.r, self.g, self.b, str)
end
function Color:ToString()
return string.format("\27[38;2;%d;%d;%dm%s\27[0m", self.r, self.g, self.b, self)
end
function Color:Print(str)
print(self:String(str))
return self
end
function Color:Grey() -- reduce saturation to 0
local h, _, v = self:ToHSV()
self:From("hsv", h, 0, v)
return self
end
function Color:Invert()
self.r, self.g, self.b = math.abs(255 - self.r), math.abs(255 - self.g), math.abs(255 - self.b)
return self
end
function Color:Copy()
return Color(self.r, self.g, self.b, self.a)
end
function Color:ToTable()
return {self.r, self.g, self.b, self.a}
end
function Color:ToVector()
return Vector(self.r / 255, self.g / 255, self.b / 255)
end
function Color:Contrast(smooth)
if smooth then
local c = 255 - self:ToHexDecimal() / 0xffffff * 255
return Color(c, c, c)
else
return self:ToHexDecimal() > 0xffffff / 2 and Color(0, 0, 0) or Color(255, 255, 255)
end
end
end
do -- convert rgb > X
local bit = bit or bit32
function Color:ToHexDecimal() -- 24bit
return bit.bor(bit.lshift(self.r, 16), bit.lshift(self.g, 8), self.b)
end
function Color:ToHexaDecimal() -- Alpha support, 32bit
return bit.bor(bit.lshift(self.r, 24), bit.lshift(self.g, 16), bit.lshift(self.b, 8), self.a)
end
function Color:ToHex(hash)
if hash then
return string.format("#%x", (self.r * 0x10000) + (self.g * 0x100) + self.b):upper()
end
return string.format("%x", (self.r * 0x10000) + (self.g * 0x100) + self.b):upper()
end
function Color:ToHexa(hash)
if hash then
return string.format("#%x", (self.r * 0x1000000) + (self.g * 0x10000) + (self.b * 0x100) + self.a):upper()
end
return string.format("%x", (self.r * 0x1000000) + (self.g * 0x10000) + (self.b * 0x100) + self.a):upper()
end
function Color:ToHSV()
local h, s, v
local min = math.min(self.r, self.g, self.b)
local max = math.max(self.r, self.g, self.b)
v = max
local delta = max - min
if max ~= 0 then
s = delta / max
else
s = 0
h = -1
return h, math.floor(s * 100), v / 2.55
end
if self.r == max then
h = (self.g - self.b) / delta
elseif self.g == max then
h = 2 + (self.b - self.r) / delta
else
h = 4 + (self.r - self.g) / delta
end
h = h * 60
if h < 0 then
h = h + 360
end
return h, math.floor(s * 100), v / 2.55
end
function Color:ToHWB()
local h, s, v = self:ToHSV()
return h, (100 - s) * v, 100 - v
end
function Color:ToHSL()
local r, g, b = self.r / 255, self.g / 255, self.b / 255
local min = math.min(r, g, b)
local max = math.max(r, g, b)
local delta = max - min
local h, s, l = 0, 0, (min + max) / 2
if l > 0 and l < 0.5 then s = delta / (max + min) end
if l >= 0.5 and l < 1 then s = delta / (2 - max - min) end
if delta > 0 then
if max == r and max ~= g then h = h + (g - b) / delta end
if max == g and max ~= b then h = h + 2 + (b - r) / delta end
if max == b and max ~= r then h = h + 4 + (r - g) / delta end
h = h / 6
end
if h < 0 then h = h + 1 end
if h > 1 then h = h - 1 end
return h * 360, s * 100, l * 100
end
function Color:ToCMYK()
local max = math.max(self.r, self.g, self.b)
return
(max - self.r) / max * 100,
(max - self.g) / max * 100,
(max - self.b) / max * 100,
math.min(self.r, self.g, self.b) / 2.55
end
end
do -- convert X > rgb
local convert = {}
function convert.hex(hex)
if type(hex) == "string" then
hex = tonumber(hex:gsub("^[#0]x?", ""), 16)
end
return
bit.rshift(bit.band(hex, 0xFF0000), 16),
bit.rshift(bit.band(hex, 0xFF00), 8),
bit.band(hex, 0xFF)
end
function convert.hexa(hexa)
if type(hexa) == "string" then
hexa = tonumber(hexa:gsub("^[#0]x?", ""), 16)
end
return
bit.rshift(bit.band(hexa, 0xFF000000), 24),
bit.rshift(bit.band(hexa, 0xFF0000), 16),
bit.rshift(bit.band(hexa, 0xFF00), 8),
bit.band(hexa, 0xFF)
end
function convert.hsv(h, s, v)
h = h / 360
s = s / 100
v = v / 100
local r, g, b
local i = math.floor(h * 6)
local f = h * 6 - i
local p = v * (1 - s)
local q = v * (1 - f * s)
local t = v * (1 - (1 - f) * s)
i = i % 6
if i == 0 then
r, g, b = v, t, p
elseif i == 1 then
r, g, b = q, v, p
elseif i == 2 then
r, g, b = p, v, t
elseif i == 3 then
r, g, b = p, q, v
elseif i == 4 then
r, g, b = t, p, v
elseif i == 5 then
r, g, b = v, p, q
end
return
r * v * 255,
g * v * 255,
b * v * 255
end
local function hsl2rgb(m, m2, h)
if h < 0 then h = h + 1 end
if h > 1 then h = h - 1 end
if h * 6 < 1 then
return m + (m2 - m) * h * 6
elseif h * 2 < 1 then
return m2
elseif h * 3 < 2 then
return m + (m2 - m) * (2 / 3 - h) * 6
else
return m
end
end
function convert.hsl(h, s, l)
h, s, l = h / 360, s / 100, l / 100
local m2 = l <= 0.5 and l * (s + 1) or l + s - l * s
local m = l * 2 - m2
return
hsl2rgb(m, m2, h + 1 / 3) * 255,
hsl2rgb(m, m2, h) * 255,
hsl2rgb(m, m2, h - 1 / 3) * 255
end
function convert.hwb(h, w, b)
return convert.hsv(h, 100 - w / (100 - b), 100 - b)
end
function convert.cmyk(c, m, y, k)
c, m, y, k = c / 100, m / 100, y / 100, k / 100
local mk = 1 - k
return
(1 - c) * mk * 255,
(1 - m) * mk * 255,
(1 - y) * mk * 255
end
local constructor = {}
function constructor:__index(model)
local make = convert[model]
if make then
return function(...)
local args = {...}
local a = table.remove(args, debug.getinfo(make).nparams + 1)
local r, g, b, a2 = make(unpack(args))
return Color(r, g, b, a2 or a)
end
end
end
function constructor:__call(r, g, b, a)
return setmetatable({r = r or 0, g = g or 0, b = b or 0, a = a or 255}, Color)
end
setmetatable(Color, constructor)
function Color:From(model, ...)
if convert[model] then
return self:SetUnpacked(
convert[model](...)
)
end
end
end
function Color.test()
local alpha = 175
Color(255, 255, 0):Print("from incredible-gmod.ru with <3")
print("\nconverters:")
print("\trgb > color\t", Color(0, 255, 0, alpha):ToString())
print("\thex > color\t", Color.hex("#de621f", alpha):ToString())
print("\thexdec > color\t", Color.hex(0xDED81F, alpha):ToString())
print("\thexa > color\t", Color.hexa(0x82DE1FAF):ToString())
print("\thsv > color\t", Color.hsv(120, 86, 87, alpha):ToString())
print("\thsl > color\t", Color.hsl(150, 75, 50, alpha):ToString())
print("\thwb > color\t", Color.hwb(0, 0, 0, alpha):ToString())
print("\tcmyk > color\t", Color.cmyk(86, 37, 0, 13, alpha):ToString())
print("")
print("\tcolor > hex\t", Color(0, 255, 0, alpha):ToHex())
print("\tcolor > hexa\t", Color(222, 98, 31, alpha):ToHexa())
print("\tcolor > hexdecimal", Color(222, 216, 31, alpha):ToHexDecimal())
print("\tcolor > hexadecimal", Color(130, 222, 31, alpha):ToHexaDecimal())
print("\tcolor > hsv\t", table.concat({Color(27, 193, 27, alpha):ToHSV()}, ", "))
print("\tcolor > hsl\t", table.concat({Color(31, 223, 127, alpha):ToHSL()}, ", "))
print("\tcolor > hwb\t", table.concat({Color(255, 0, 0, alpha):ToHWB()}, ", "))
print("\tcolor > cmyk\t", table.concat({Color(31, 139, 221, alpha):ToCMYK()}, ", "))
print("meta events:")
print("\t__tostring\t", tostring(Color(123)))
print("\t__concat\t", "test ".. Color(100) .. " :)")
print("\t__unm\t\t", -Color(1, 2, 3))
print("\t__add\t\t", Color(25, 25, 25) + Color(150, 75, 0, 0))
print("\t__sub\t\t", Color(255, 255, 255) - Color(0, 255, 255, 0))
print("\t__mul\t\t", Color(100, 50, 0, 100) * 2)
print("\t__div\t\t", Color(200, 50, 0) / 2)
print("\t__eq\t\t", Color(255, 0, 0) == Color(255, 0, 0), Color(255, 0, 0) == Color(0, 0, 0))
print("\t__lt\t\t", Color(255, 0, 0) < Color(0, 0, 0))
print("\t__le\t\t", Color(255, 0, 0) <= Color(255, 1, 0))
print("methods:")
print("\tUnpack\t\t", table.concat({Color(255, 0, 0):Unpack()}, ", "))
print("\tSetUnpacked\t", Color(255, 0, 0):SetUnpacked(0, 255, 0))
print("\tNormalize\t", Color(255, 50, 100):Normalize())
print("\tGetNormalized\t", Color(255, 50, 100):GetNormalized())
print("\tString\t\t", Color.hex("#16a085"):String("hello"))
print("\tGrey\t\t", Color(255, 255):Grey())
print("\tInvert\t\t", Color(255, 100):Invert())
print("\tCopy\t\t", Color(100):Copy())
print("\tToTable\t\t", Color(123):ToTable())
print("\tToVector\t", Vector and Color(0, 255):ToVector() or "Cant convert to Vector, because Vector class is not defind")
print("\tContrast\t", Color(180, 150):Contrast(true))
end
-- Color.test()
return Color, IsColor