Guest User

color transition

a guest
Jul 15th, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.72 KB | None | 0 0
  1. -- Color transition wrapper
  2. -- Author: atoko
  3. -- Release date: 5/9/2012
  4. -- Version: 1.0.0
  5. --
  6. --
  7. --
  8. --Description:
  9. --      This is a wrapper for changing fill color within a transition
  10. --  Time, delay and easing values are optional
  11. --
  12. --Usage:
  13. -- cTrans = require( "colortransition" ) ;
  14. --
  15. -- cTrans:colorTransition(displayObject, colorFrom, colorTo, [time], [delay], [easing]) ;
  16. -- ex:
  17. --  
  18. --  local rect = display.newRect(0,0,250,250) ;
  19. --      local white = {255,255,255}    
  20. --  local red = {255,0,0} ;
  21. --      
  22. --  cTrans:colorTransition(rect, white, red, 1200) ;
  23.  
  24.  
  25. local _M = {}
  26.  
  27. --Local reference to transition function
  28. _M.callback = transition.to ;
  29.  
  30.  
  31. -- function _M:colorTransition( obj, colorFrom, colorTo, time, onComplete, delay, ease )
  32. function _M:colorTransition( obj, colorFrom, colorTo, time, params  )
  33.        
  34.         local _obj =  obj ;
  35.         local ease = params and params.ease or easing.linear
  36.         local callbackOnComplete = params and params.onComplete or nil --callback functionality added
  37.        
  38.        
  39.         local fcolor = colorFrom or {1,1,1} ; -- defaults to white
  40.         local tcolor = colorTo or {0,0,0} ; -- defaults to black
  41.         local t = nil ;
  42.         local p = {} --hold parameters here
  43.        
  44.         local rDiff = tcolor[1] - fcolor[1] ; --Calculate difference between values
  45.  
  46.         local gDiff = tcolor[2] - fcolor[2] ;
  47.  
  48.         local bDiff = tcolor[3] - fcolor[3] ;
  49.        
  50.                 --Set up proxy
  51.         local proxy = {step = 0} ;
  52.        
  53.         local mt = {
  54.                 __index = function(t,k)
  55.                         --print("get") ;
  56.                         return t["step"]
  57.                 end,
  58.                
  59.                 __newindex = function (t,k,v)
  60.                         --print("set")
  61.                         --print(t,k,v)
  62.                         if(_obj.setFillColor) then
  63.                                 _obj:setFillColor(fcolor[1] + (v*rDiff) ,fcolor[2] + (v*gDiff) ,fcolor[3] + (v*bDiff) )
  64.                                 --_obj.rCol = fcolor[1] + (v*rDiff)
  65.                                 --_obj.gCol = fcolor[2] + (v*gDiff)
  66.                                 --_obj.bCol = fcolor[3] + (v*bDiff)
  67.                         end
  68.                         t["step"] = v ;      
  69.                 end    
  70.         }
  71.        
  72.         p.time = time or 1000 ; --defaults to 1 second
  73.         p.delay = params and params.delay or 0 ;
  74.         p.transition = ease ;
  75.         p.onComplete = callbackOnComplete; --callback functionality added
  76.        
  77.  
  78.         setmetatable(proxy,mt) ;
  79.        
  80.         p.colorScale = 1 ;
  81.        
  82.         t = self.callback( proxy, p, 1 )  ;
  83.  
  84.         return t
  85.  
  86. end
  87.  
  88.  
  89. return _M ;
Advertisement
Add Comment
Please, Sign In to add comment