Quick color fading snippet
by Matthew Langley · in Torque Game Builder · 03/29/2006 (1:03 pm) · 2 replies
Just a quick color fading snippet I figured I would post :)
you can call it on an object like this
$player.fadeToColor(1, 1, 0, 0);
that will fade the player to a 1 red, 0 green, and 0 blue in 1 second... the inc value defaults to 10 if not specified, that is how many time incriments the calls are seperated into, also note the time is only as accurate as schedule is, so the time won't be exact, but should be close enough.
(also note you can call it like this
$player.fadeToColor(1, "1 0 0");
this allows you to do things like this
$prevColor = $player.getBlendColor();
$player.fadeToColor(1, $prevColor);
)
edit: fixed an issue with passing in a string of colors like "0.5 0.5 0.5"
you can call it on an object like this
$player.fadeToColor(1, 1, 0, 0);
that will fade the player to a 1 red, 0 green, and 0 blue in 1 second... the inc value defaults to 10 if not specified, that is how many time incriments the calls are seperated into, also note the time is only as accurate as schedule is, so the time won't be exact, but should be close enough.
(also note you can call it like this
$player.fadeToColor(1, "1 0 0");
this allows you to do things like this
$prevColor = $player.getBlendColor();
$player.fadeToColor(1, $prevColor);
)
function t2dSceneObject::fadeToColor(%this, %time, %red, %green, %blue, %inc)
{
// store the starting color
%this.startColorBeforeFade = %this.getBlendColor();
// grab the starting colors
%startRed = getWord(%this.startColorBeforeFade, 0);
%startGreen = getWord(%this.startColorBeforeFade, 1);
%startBlue = getWord(%this.startColorBeforeFade, 2);
// this will allow you to pass in a target color as "0.5 0.5 0.5" as well as "0.5, 0.5, 0.5"
if(getWord(%red, 2) !$= "")
{
%inc = %green;
%green = getWord(%red, 1);
%blue = getWord(%red, 2);
%red = getWord(%red, 0);
}
// if our target colors and our present colors are the same then we need to do nothing
if((%startRed == %red) && (%startGreen == %green) && (%startBlue == %blue))
return;
// if we are already doing a color change then cancel it out
if(isEventPending(%this.colorFadeSchedule))
cancel(%this.colorFadeSchedule);
// how many time incriments do we want
if(%inc $= "" || %inc < 1 || %inc > 100)
%inc = 10;
// divide out what each time inc will be
%timeInc = (%time * 100) / %inc;
// get the different between starting and ending colors
%redDiff = %red - %startRed;
%greenDiff = %green - %startGreen;
%blueDiff = %blue - %startBlue;
// get the ammount we must inc each color each scheduled change
%redInc = %redDiff / %inc;
%greenInc = %greenDiff / %inc;
%blueInc = %blueDiff / %inc;
// call the color change
%this.fadingColor(%inc, %timeInc, %redInc, %greenInc, %blueInc);
}
function t2dSceneObject::fadingColor(%this, %inc, %timeInc, %redInc, %greenInc, %blueInc, %count)
{
// init the count if not specified
if(%count $= "")
%count = 0;
// grab the current color
%color = %this.getBlendColor();
// divide up the colors and inc them
%red = getWord(%color, 0) + %redInc;
%green = getWord(%color, 1) + %greenInc;
%blue = getWord(%color, 2) + %blueInc;
// set the new colors
%this.setBlendColor(%red, %green, %blue);
// inc the count
%count++;
// if the count is less than the planned incs then schedule this to be called again
if(%count < %inc)
%this.colorFadeSchedule = %this.schedule(%timeInc, "fadingColor", %inc, %timeInc, %redInc, %greenInc, %blueInc, %count);
}edit: fixed an issue with passing in a string of colors like "0.5 0.5 0.5"
About the author
Was a GG Associate and then joined GG in 2005. Lead tool dev for T2D and T3D. In 2011 joined mobile company ngmoco/DeNA and spent about 4 years working game and server tech. 2014 joined startup Merigo Games developing server technology.
Torque Owner Drew -Gaiiden- Sikora