A changing color class in 20 lines of active code...

Hello,
Due to the fact that it’s spring break for me, you’ll see me rolling out some good code. Here is one of the peices of code: a smooth chanign color class. Here is is:

--Changing color class
--By Zoyt
--cc = color changing pace; r, g, b = starting colors

ChangeColor = class()

function ChangeColor:init(cc,r,g,b)
    self.phase = 0
    self.changingpace = cc
    self.bgr = r
    self.bgg = g
    self.bgb = b
end

function ChangeColor:update()
    self.bgr = 128+ 127 * math.sin(self.phase / (2*math.pi))
    self.bgg = 128+ 127 * math.sin(self.phase * 2/ (2*math.pi))
    self.bgb = 128+ 127 * math.sin(self.phase * 3/ (2*math.pi))
    
    self.phase = self.phase + self.changingpace
    
    if self.phase > (360 * 6) then
        self.phase = 0
    end
end

function ChangeColor:getColor()
    return self.bgr,self.bgg,self.bgb
end

Have fun!
Thanks!

Oh, and a little example to show how to use it:


function setup()
    displayMode(FULLSCREEN)
    ChangeColor:init(0.1)
end

function draw()
    ChangeColor:update()
    background(ChangeColor:getColor())
end

Have fun!