-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.lua
93 lines (82 loc) · 1.99 KB
/
util.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
local vec2 = require("lib.vector2")
local vars = require("vars")
local function serializeTable(t, indent)
indent = indent or 1
if type(t) == "table" and indent <= 1 then
local str = tostring(t) .. " {\n"
for k, v in pairs(t) do
str = str .. string.rep(" ", indent) .. "[" .. k .. "] = " .. serializeTable(v, indent + 1) .. ",\n"
end
str = str .. string.rep(" ", indent - 1) .. "}"
return str
else
return tostring(t)
end
end
local function logTable(t)
print(serializeTable(t))
end
function math.lerp(a, b, t)
return t * (b - a) + a
end
local function bezier(p0, p1, p2, p3, t)
return
p0 * (-t ^ 3 + 3 * t ^ 2 - 3 * t + 1) +
p1 * (3 * t ^ 3 - 6 * t ^ 2 + 3 * t) +
p2 * (-3 * t ^ 3 + 3 * t ^ 2) +
p3 * (t ^ 3)
end
local function hsv2rgb(h, s, v, a)
s = 1 - s
local i = h / 60 % 6
local r, g, b
if i < 1 then --red to yellow
r = v
g = math.lerp((i % 1), 1, s) * v
b = s * v
elseif i < 2 then --yellow to green
r = math.lerp((1 - i % 1), 1, s) * v
g = v
b = s * v
elseif i < 3 then --green to cyan
r = s * v
g = v
b = math.lerp((i % 1), 1, s) * v
elseif i < 4 then --cyan to blue
r = s * v
g = math.lerp((1 - i % 1), 1, s) * v
b = v
elseif i < 5 then --blue to magenta
r = math.lerp((i % 1), 1, s) * v
g = s * v
b = v
else --magenta to red
r = v
g = s * v
b = math.lerp((1 - i % 1), 1, s) * v
end
return r, g, b, a
end
local function getUnit()
unit = math.min(love.graphics.getDimensions()) / 11
return unit
end
local function toScreenCoords(x, y)
return vec2(love.graphics.getDimensions()) / 2 + vec2(x - 5, y - 5) * getUnit()
end
local function loadState(state)
if type(state) == "string" then
state = require("states."..state)
end
vars.state = state
return state
end
return {
bezier = bezier,
hsv2rgb = hsv2rgb,
getUnit = getUnit,
toScreenCoords = toScreenCoords,
printTable = logTable,
serializeTable = serializeTable,
loadState = loadState,
}