Colour utility methods

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)/0x100 + z) > 0xFF ? 0xFF : ( b < 0 ? 0 : b);
                c = (c=(color & 0xff0000)/0x10000 + 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)/0x100*(100-percent) + (second & 0xff00)/0x100*percent)/100;
                c = ((first & 0xff0000)/0x10000*(100-percent) + (second & 0xff0000)/0x10000*percent)/100;
               
                return ((a) | (b << 8) | (c << 16));
        }
       
        private function desaturate(color:Number, percent:Number):Number
        {
                return blend(color, 0x808080, percent);
        }
       
        private function fade(color:Number, percent:Number):Number
        {
                return blend(color, 0xFFFFFF, percent);
        }

This entry was posted on Tuesday, March 28th, 2006 at 6:20 pm and is filed under Actionscript. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.

2 Responses to “Colour utility methods”

  1. adam jones Says:

    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

  2. Nikola Says:

    :oops: :oops::oops::oops::oops: thank`s for this script ( little code )

Leave a Reply