Another table-related small function that I made a long time ago. Pretty useful, I think. Gets the biggest item of a table, being it another table (amount of items), a string (amount of chars) or a number (biggest value). For the biggest table, it only gets array sizes, but not string items (like something["anotherthing"]). For that, I'd highly suggest replacing every "#table" thing on the code by "table.size" and implement this function (may be in the main post).
Code: Select all
function table.biggest(t, dtype) --Copied from Mari0 +Portal, but totally mine :)
local dtype = dtype or "number"
local biggest = nil
for i, v in pairs(t) do
if not biggest then
if type(v) == dtype then
biggest = i
end
else
if type(v) == dtype then
if dtype == "table" then
if #v > #t[biggest] then
biggest = i
end
elseif dtype == "number" then
if v > t[biggest] then
biggest = i
end
elseif dtype == "string" then
if string.len(v) > string.len(t[biggest]) then
biggest = i
end
end
end
end
end
return biggest
end
EDIT: I was trying to get fading colors, but the way LÖVE provides it in Snippets' category is a bit (a lot) confusing. So, I made my own, simple fading color function:
Arguments: the timer value, the maximum timer value, the first color's table, the second color's table
Returns the new colors
Code: Select all
function fade(currenttime, maxtime, c1, c2)
local tp = currenttime/maxtime
local ret = {} --return color
for i = 1, #c1 do
ret[i] = c1[i]+(c2[i]-c1[i])*tp
ret[i] = math.max(ret[i], 0)
ret[i] = math.min(ret[i], 255)
end
return unpack(ret)
end
I also made a small function to get the average value of numbers:
Code: Select all
function math.med( ... )
local args = { ... }
local ret = 0
for i = 1, #args do
ret = ret + args[i]
end
return ret/#args
end