Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

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

edited March 2012 in General Posts: 2,820

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

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

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

Have fun!


  • Posts: 2,820

    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!

Sign In or Register to comment.