-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.lua
49 lines (43 loc) · 1.33 KB
/
lib.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
local max = math.max
local min = math.min
local function clamp(x, mi, ma)
return min(ma, max(mi, x))
end
-- same as xpcall(f, errf), but additional args can follow: xxpcall(f, errf, ...)
local xxpcall
do
-- code is slightly convoluted to prevent additional per-call memory allocation
local ARGS = {}
local CALLFUNC
local NUMARGS
local unpack = unpack
local select = select
local function _fillArgs(f, ...)
CALLFUNC = f
NUMARGS = select("#", ...)
for i = 1, NUMARGS do
ARGS[i] = select(i, ...)
end
end
local function _callHelper()
return CALLFUNC(unpack(ARGS, 1, NUMARGS)) -- this safely handles returned NILs or NILs in ARGS
end
xxpcall = function(f, errf, ...)
_fillArgs(f, ...)
return xpcall(_callHelper, errf)
end
end
local function hue2rgb(index, bright_percent)
if index < 85 then
return index * 3 * bright_percent / 100, (255 - index * 3) * bright_percent / 100, 0
elseif index < 170 then
index = index - 85
return (255 - index * 3) * bright_percent / 100, 0, index * 3 * bright_percent / 100
else
index = index - 170
return 0, index * 3 * bright_percent / 100, (255 - index * 3) * bright_percent / 100
end
end
setglobal("xxpcall", xxpcall)
setglobal("hue2rgb", hue2rgb)
setglobal("clamp", clamp)