fade
Gets the mix between two colors at a given rate. I already posted this function here, but reposting for the sake of making it easy
Time passed, Time limit, First color's table, Second color's table
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
Just a quick mathematical function that gets the distance between two points (a.k.a Pythagoras' Theorem)
Point 1 x, Point 1 y, Point 2 x, Point 2 y
Code: Select all
function distance(cx, cy, x, y)
return math.sqrt( math.abs(x-cx)^2 + math.abs(y-cy)^2 )
end
Generates an image of a radial gradient between two colors. Requires the "fade" and "distance" functions.
Gradient's radius, First color's table, Second color's table
Code: Select all
function renderGradient(size, c1, c2)
local i = love.image.newImageData(size*2, size*2)
for x = 0, size*2-1 do
for y = 0, size*2-1 do
local d = self.distance(size, size, x+1, y+1)
local f = d/size
f = math.max(0, f)
i:setPixel(x, y, self.fade(f, 1, c1, c2))
end
end
return love.graphics.newImage(i)
end
Used to make a stencil of a circle with a hole in it (can also be used as a drawing polygon on it's own)
Center's x, Center's y, Hole's radius, Circle's radius, Circle's segments
Code: Select all
function pokedStencil(cx, cy, d1, d2, s)
for a = 0, s-1 do
local p1x = math.cos(a/s*(math.pi*2))*d2
local p1y = -math.sin(a/s*(math.pi*2))*d2
local p2x = math.cos(a/s*(math.pi*2))*d1
local p2y = -math.sin(a/s*(math.pi*2))*d1
local p3x = math.cos((a+1)/s*(math.pi*2))*d1
local p3y = -math.sin((a+1)/s*(math.pi*2))*d1
local p4x = math.cos((a+1)/s*(math.pi*2))*d2
local p4y = -math.sin((a+1)/s*(math.pi*2))*d2
love.graphics.polygon("fill", cx+p1x, cy+p1y, cx+p2x, cy+p2y, cx+p3x, cy+p3y, cx+p4x, cy+p4y)
end
end