-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransitionColor.lua
110 lines (88 loc) · 3.17 KB
/
transitionColor.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
-- =============================================================
-- Copyright Roaming Gamer, LLC. 2009-2015
-- =============================================================
-- License
-- =============================================================
--[[
> SSK is free to use.
> SSK is free to edit.
> SSK is free to use in a free or commercial game.
> SSK is free to use in a free or commercial non-game app.
> SSK is free to use without crediting the author (credits are still appreciated).
> SSK is free to use without crediting the project (credits are still appreciated).
> SSK is NOT free to sell for anything.
> SSK is NOT free to credit yourself with.
]]
-- =============================================================
-- Adopted from http://developer.coronalabs.com/code/color-transition-wrapper with fixes.
-- =============================================================
--Description:
-- This is a wrapper for changing fill color within a transition
-- Time, delay and easing values are optional
--
--Usage:
--
-- transition.fromtocolor(displayObject, colorFrom, colorTo, [time], [delay], [easing]) ;
-- ex:
--
-- local rect = display.newRect(0,0,250,250) ;
-- local white = {1,1,1}
-- local red = {1,0,0} ;
--
-- transition.fromtocolor(rect, white, red, 1200) ;
-- =============================================================
--Local reference to transition function
transition.callback = transition.to ;
function transition.fromtocolor(obj, colorFrom, colorTo, time, delay, ease)
local _obj = obj ;
local ease = ease or easing.linear
local fcolor = colorFrom or {1,1,1,1} ; -- defaults to white
local tcolor = colorTo or {0,0,0,1} ; -- defaults to black
local t = nil ;
local p = {} --hold parameters here
local rDiff = tcolor[1] - fcolor[1] ; --Calculate difference between values
local gDiff = tcolor[2] - fcolor[2] ;
local bDiff = tcolor[3] - fcolor[3] ;
local aDiff = tcolor[4] - fcolor[4] ;
--Set up proxy
local proxy = {step = 0} ;
local mt
if( obj and obj.setFillColor ) then
mt = {
__index = function(t,k)
--print("get") ;
return t["step"]
end,
__newindex = function (t,k,v)
--print("set")
--print(t,k,v)
if(_obj.setFillColor) then
_obj:setFillColor(fcolor[1] + (v*rDiff) ,fcolor[2] + (v*gDiff) ,fcolor[3] + (v*bDiff), fcolor[4] + (v*aDiff) )
end
t["step"] = v ;
end
}
else
mt = {
__index = function(t,k)
--print("get") ;
return t["step"]
end,
__newindex = function (t,k,v)
--print("set")
--print(t,k,v)
if(_obj.setFillColor) then
_obj:setFillColor(fcolor[1] + (v*rDiff) ,fcolor[2] + (v*gDiff) ,fcolor[3] + (v*bDiff), fcolor[4] + (v*aDiff) )
end
t["step"] = v ;
end
}
end
p.time = time or 1000 ; --defaults to 1 second
p.delay = delay or 0 ;
p.transition = ease ;
setmetatable(proxy,mt) ;
p.colorScale = 1 ;
t = transition.to(proxy,p , 1 ) ;
return t
end