Thursday, 24 November 2011

The Colourise class

This is my replacement for the ColorTransform class. My reason for creating it was as I store colours as RGB integers, such as 0xffffff, 0x000000, but ColorTransform works with separate floating point values. With this class I can easily use RGB colours whenever I need to use a ColorTransform (which is quite a lot).

package {
 import flash.geom.ColorTransform;

 class Colourise extends ColorTransform {
  
  function Colourise(
    iTint:uint = 0xffffff, iTint2:uint = 0x000000, fAMult:Number = 1
  ) {
   var fRed:Number, fGreen:Number, fBlue:Number;
   // white to iTint, black to iTint2
   fRed  = ((iTint >>> 16) & 0xff) / 255;
   fGreen   = ((iTint >>>  8) & 0xff) / 255;
   fBlue  = (iTint          & 0xff) / 255;

   redOffset  = (iTint2 >>> 16) & 0xff;
   greenOffset  = (iTint2 >>>  8) & 0xff;
   blueOffset  = iTint2          & 0xff;

   redMultiplier  = fRed   - redOffset   / 255;
   greenMultiplier = fGreen - greenOffset / 255;
   blueMultiplier = fBlue  - blueOffset  / 255;
   alphaMultiplier = fAMult;
   alphaOffset = 0;
  }
 }
}
This makes it easy to create and use a ColorTransform. For example the following code from today, which creates a coloured dot to apply to another BitmapData:
dat = gGuns.gGfx.datSpellD.clone();
dat.colorTransform(dat.rect, new Colourise(Col.aSpells[Magic.eRed], Col.cBlack));
bitmapData.draw(dat, new Matrix(1, 0, 0, 1, 1 + iIcon * 7, 1));

No comments:

Post a comment