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 |