March 28th, 2006
Colour utility methods
Posted by
Ash in
Actionscript
Nothing super-special.. just a couple of utility methods I use for modifying colour values. The function names give clues to what they do
Source code after the jump..
AS:
/* Input colour value such as 0xFF0000, and modifier from -1 to 1. */
private function brighten(color:Number, modifier:Number):Number
{
var z = 0xff * modifier;
var a, b, c;
a = (a=(color & 0xff) + z) > 0xFF ? 0xFF : ( a < 0 ? 0 : a);
b = (b=(color & 0xff00)/0×100 + z) > 0xFF ? 0xFF : ( b < 0 ? 0 : b);
c = (c=(color & 0xff0000)/0×10000 + z) > 0xFF ? 0xFF : ( c < 0 ? 0 : c);
return ((a) | (b << 8) | (c << 16));
}
/* Blends two colours. Percentage should be 50 for an equal blend. */
private function blend(first:Number, second:Number, percent:Number):Number
{
var a, b, c;
a = ((first & 0xff)*(100-percent) + (second & 0xff)*percent)/100;
b = ((first & 0xff00)/0×100*(100-percent) + (second & 0xff00)/0×100*percent)/100;
c = ((first & 0xff0000)/0×10000*(100-percent) + (second & 0xff0000)/0×10000*percent)/100;
return ((a) | (b << 8) | (c << 16));
}
private function desaturate(color:Number, percent:Number):Number
{
return blend(color, 0×808080, percent);
}
private function fade(color:Number, percent:Number):Number
{
return blend(color, 0xFFFFFF, percent);
}
/* Input colour value such as 0xFF0000, and modifier from -1 to 1. */
private function brighten(color:Number, modifier:Number):Number
{
var z = 0xff * modifier;
var a, b, c;
a = (a=(color & 0xff) + z) > 0xFF ? 0xFF : ( a < 0 ? 0 : a);
b = (b=(color & 0xff00)/0×100 + z) > 0xFF ? 0xFF : ( b < 0 ? 0 : b);
c = (c=(color & 0xff0000)/0×10000 + z) > 0xFF ? 0xFF : ( c < 0 ? 0 : c);
return ((a) | (b << 8) | (c << 16));
}
/* Blends two colours. Percentage should be 50 for an equal blend. */
private function blend(first:Number, second:Number, percent:Number):Number
{
var a, b, c;
a = ((first & 0xff)*(100-percent) + (second & 0xff)*percent)/100;
b = ((first & 0xff00)/0×100*(100-percent) + (second & 0xff00)/0×100*percent)/100;
c = ((first & 0xff0000)/0×10000*(100-percent) + (second & 0xff0000)/0×10000*percent)/100;
return ((a) | (b << 8) | (c << 16));
}
private function desaturate(color:Number, percent:Number):Number
{
return blend(color, 0×808080, percent);
}
private function fade(color:Number, percent:Number):Number
{
return blend(color, 0xFFFFFF, percent);
}

Nice work Ash! I noticed you added two more methods, desaturate and fade. I admit, I use the first two methods often.
Oh, Im going to want those components you are working on by EOD.
Adam
:oops::oops::oops::oops::oops: thank`s for this script ( little code )