Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Colour Changer
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ericfourfour




PostPosted: Fri Nov 03, 2006 4:16 pm   Post subject: Colour Changer

This program allows you to add a blending colour effect very easily. Here is the source:
code:
class ColourChanger
    export set_duration, add_colour, remove_colours, get_colour, was_switched, update

    var clr : flexible array 0 .. -1 of int
    var clr_bound := -1
    var id : array 0 .. 1 of int := init (0, 1)
    var current_colour : int
    var change_duration := 1000
    var start_time := 0
    var timer := 0
    var switched := false

    fcn get_colour () : int
        result current_colour
    end get_colour

    fcn was_switched () : boolean
        result switched
    end was_switched

    proc set_duration (ms : int)
        start_time := Time.Elapsed
        timer := 0
        if ms not= 0 then
            change_duration := ms
        else
            change_duration := 1
        end if
    end set_duration

    proc add_colour (c : int)
        clr_bound += 1
        new clr, clr_bound
        clr (clr_bound) := c
    end add_colour

    proc remove_colours ()
        clr_bound := -1
        new clr, clr_bound
    end remove_colours

    fcn merge (clr1, clr2 : int, fraction : real) : int
        var r, b, g : array 0 .. 2 of real
        var amount : array 0 .. 1 of real


        RGB.GetColour (clr1, r (0), g (0), b (0))
        RGB.GetColour (clr2, r (1), g (1), b (1))


        amount (0) := 1 - fraction
        amount (1) := fraction

        r (2) := r (0) * amount (0) + r (1) * amount (1)
        g (2) := g (0) * amount (0) + g (1) * amount (1)
        b (2) := b (0) * amount (0) + b (1) * amount (1)

        result RGB.AddColour (r (2), g (2), b (2))
    end merge

    proc swith_colours ()
        for i : 0 .. 1
            id (i) := (id (i) + 1) rem (clr_bound + 1)
        end for
        switched := true
        set_duration (change_duration)
    end swith_colours

    proc update ()
        timer := Time.Elapsed - start_time
        var fraction := timer / change_duration
        if fraction > 1 then
            swith_colours ()
        else
            switched := false
            if clr_bound > 0 then
                current_colour := merge (clr (id (0)), clr (id (1)), fraction)
            elsif clr_bound = 0 then
                current_colour := clr (0)
            end if
        end if
    end update
end ColourChanger


Here is a program that uses it:
code:
var c : ^ColourChanger
new ColourChanger, c

c -> set_duration (500)
c -> add_colour (red)
c -> add_colour (blue)

loop
    c -> update ()
    Draw.FillBox (0, 0, 100, 100, c -> get_colour ())
    delay (10)
end loop


You can set the duration between colours (how long the transition is). You can also use an unlimited amount of colours. It is fps independent meaning if you set the duration to 1000 milliseconds and your delay is 10 or you delay is 273 it will always be at the next colour after 1000 milliseconds (although 1000 isn't a multiple of 273 so it technically can't reach the next colour. However it would still be at its respective colour in the transition).

To set the duration:
code:
set_duration (ms : int)


To add a colour:
code:
add_colour (c : int)


To remove the colours:
code:
remove_colours ()


To update the object:
code:
update ()


To get the colour:
code:
get_colour () : int


To tell if the colour was switched in the previous update:
code:
was_switched () : boolean
Sponsor
Sponsor
Sponsor
sponsor
Hackmaster




PostPosted: Sat Dec 09, 2006 10:09 am   Post subject: (No subject)

that's nice, eric.

now animate it! move it across the screen! you are halfway to making that flowerbox screensaver for windows! Very Happy

or, alterativley, make it the whole screen!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: