-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.lua
88 lines (84 loc) · 2.13 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
function gcd(a, b)
while b ~= 0 do
local t = b
b = a % t
a = t
end
return a
end
-- Converts HSL to RGB. (input and output range: 0 - 1)
function HSL(h, s, l, a)
if s <= 0 then
return l, l, l, a
end
h, s, l = h * 6, s, l
local c = (1 - math.abs(2 * l - 1)) * s
local x = (1 - math.abs(h % 2 - 1)) * c
local m, r, g, b = (l - .5 * c), 0, 0, 0
if h < 1 then
r, g, b = c, x, 0
elseif h < 2 then
r, g, b = x, c, 0
elseif h < 3 then
r, g, b = 0, c, x
elseif h < 4 then
r, g, b = 0, x, c
elseif h < 5 then
r, g, b = x, 0, c
else
r, g, b = c, 0, x
end
return r + m, g + m, b + m, a
end
function worldToScreenSpace(x, y, ppu, canvas)
ppu = ppu or 1
local screenWidth, screenHeight
if canvas == nil then
screenWidth, screenHeight = love.graphics.getDimensions()
else
screenWidth, screenHeight = canvas:getDimensions()
end
local newX = x - math.floor((screenWidth / 2))
newX = x / ppu
local newY = y - math.floor((screenHeight / 2))
newY = y / ppu
return newX, newY
end
-- https://gist.github.com/Uradamus/10323382?permalink_comment_id=2754684#gistcomment-2754684
function shuffle(tbl)
for i = #tbl, 2, -1 do
local j = math.random(i)
tbl[i], tbl[j] = tbl[j], tbl[i]
end
return tbl
end
-- returns a string in the format HH:MM:SS for `time` seconds
function timestring(time)
function timestring(time)
local tcopy = time
local str = ""
local h, m, s
h = math.floor(tcopy / 3600)
tcopy = tcopy - (h * 3600)
m = math.floor(tcopy / 60)
tcopy = tcopy - (m * 60)
s = tcopy
if h > 0 then
str = str .. h .. ":"
end
if m > 0 or h > 0 then
if m < 10 then
str = str .. "0"
end
str = str .. m .. ":"
end
if s < 10 then
str = str .. '0'
end
str = str .. math.floor(s)
if m <= 0 and h <= 0 then
str = str .. 's'
end
return str
end
end