Skip to content

Instantly share code, notes, and snippets.

@Alquimista
Created April 19, 2011 22:30
Show Gist options
  • Save Alquimista/929876 to your computer and use it in GitHub Desktop.
Save Alquimista/929876 to your computer and use it in GitHub Desktop.
Interpolate for NyuFX
-- Almost Ctr+C Ctrl+V of
-- https://bitbucket.org/alquimista/crayon/
-- Author: Alquimista <[email protected]>
-- Equation
-- http://codeplea.com/simple-interpolation
-- http://algorithmist.wordpress.com/
-- Add this module to the includes in NyuFX [http://youka.xobor.de/]
interpolate = {}
-- How to use?
-------------------------------------------------------------------------------
-- Interpolate2(<0-1>, <nomber,asscolor,assalpha>,
-- <number,asscolor,assalpha>, [<function>])
-- Interpolate2(0.5, &HFFFFFF&, &H000000&)
-------------------------------------------------------------------------------
-- Like a class in python
function interpolate.new ()
local self = {}
function self:linear (t, vmin, vmax)
return t * (vmax - vmin) + vmin
end
function self:cosine (t, vmin, vmax)
t = 0.5 - (math.cos(math.pi * t) / 2)
return self:linear(t, vmin, vmax)
end
function self:smooth_step (t, vmin, vmax)
t = (t ^ 2) * (3 - (2 * t))
return self:linear(t, vmin, vmax)
end
function self:acceleration (t, vmin, vmax)
t = t ^ 2
return self:linear(t, vmin, vmax)
end
function self:deceleration (t, vmin, vmax)
t = 1 - (1 - t) ^ 2
return self:linear(t, vmin, vmax)
end
return self
end
function Interpolate2(t, vmin, vmax, func)
-- Default argument(interpolate function)
if not func then
func = 'linear'
end
-- I am repeating much code to lack of knowledge of lua
local i = interpolate.new ()
if func == 'linear' then
-- Nummber
if type(vmin) == "number" and type(vmax) == "number" then
return i:linear(t, vmin, vmax)
end
local r1, g1, b1 = ASS_to_RGB(vmin)
local r2, g2, b2 = ASS_to_RGB(vmax)
-- Color RGB &HBBGGRR&
if b1 and b2 then
local r, g, b
r = i:linear(t, r1, r2)
g = i:linear(t, g1, g2)
b = i:linear(t, b1, b2)
return RGB_to_ASS(r, g, b)
end
-- Color ALPHA &HAA&
local a1 = ASS_to_A(vmin)
local a2 = ASS_to_A(vmax)
if a1 and a2 then
local a = i:linear(t, a1, a2)
return A_to_ASS(a)
end
elseif func == 'cosine'then
if type(vmin) == "number" and type(vmax) == "number" then
return i:cosine(t, vmin, vmax)
end
local r1, g1, b1 = ASS_to_RGB(vmin)
local r2, g2, b2 = ASS_to_RGB(vmax)
if b1 and b2 then
local r, g, b
r = i:cosine(t, r1, r2)
g = i:cosine(t, g1, g2)
b = i:cosine(t, b1, b2)
return RGB_to_ASS(r, g, b)
end
local a1 = ASS_to_A(vmin)
local a2 = ASS_to_A(vmax)
if a1 and a2 then
local a = i:cosine(t, a1, a2)
return A_to_ASS(a)
end
elseif func == 'smooth_step' then
if type(vmin) == "number" and type(vmax) == "number" then
return i:smooth_step(t, vmin, vmax)
end
local r1, g1, b1 = ASS_to_RGB(vmin)
local r2, g2, b2 = ASS_to_RGB(vmax)
if b1 and b2 then
local r, g, b
r = i:smooth_step(t, r1, r2)
g = i:smooth_step(t, g1, g2)
b = i:smooth_step(t, b1, b2)
return RGB_to_ASS(r, g, b)
end
local a1 = ASS_to_A(vmin)
local a2 = ASS_to_A(vmax)
if a1 and a2 then
local a = i:smooth_step(t, a1, a2)
return A_to_ASS(a)
end
elseif func == 'acceleration' then
if type(vmin) == "number" and type(vmax) == "number" then
return i:acceleration(t, vmin, vmax)
end
local r1, g1, b1 = ASS_to_RGB(vmin)
local r2, g2, b2 = ASS_to_RGB(vmax)
if b1 and b2 then
local r, g, b
r = i:acceleration(t, r1, r2)
g = i:acceleration(t, g1, g2)
b = i:acceleration(t, b1, b2)
return RGB_to_ASS(r, g, b)
end
local a1 = ASS_to_A(vmin)
local a2 = ASS_to_A(vmax)
if a1 and a2 then
local a = i:acceleration(t, a1, a2)
return A_to_ASS(a)
end
elseif func == 'deceleration' then
if type(vmin) == "number" and type(vmax) == "number" then
return i:deceleration(t, vmin, vmax)
end
local r1, g1, b1 = ASS_to_RGB(vmin)
local r2, g2, b2 = ASS_to_RGB(vmax)
if b1 and b2 then
local r, g, b
r = i:deceleration(t, r1, r2)
g = i:deceleration(t, g1, g2)
b = i:deceleration(t, b1, b2)
return RGB_to_ASS(r, g, b)
end
local a1 = ASS_to_A(vmin)
local a2 = ASS_to_A(vmax)
if a1 and a2 then
local a = i:deceleration(t, a1, a2)
return A_to_ASS(a)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment