Saturday, 26 November 2011

More on Colours

As  I noted two days ago I store all colours as integers, in particular as uint values. These are stored as static members in a class, 'Col', for easy access to them, and also in saved levels created in the level editor (it actually stores only four bits per channel, to match the way the editor works).

To work with these I've written a number of functions, to take one or two uint colours and output another. These don't need to be fast: they're usually only called at startup as assets are created or as a screen is loaded and populated. And written like this the code is clear and easy to copy and modify which I've done repeatedly.

static function Darken(iTint:uint, fAmt:Number):uint {
 var iA:int = ((iTint >>> 24) & 0xff)
 var iR:int = ((iTint >>> 16) & 0xff);
 var iG:int = ((iTint >>>  8) & 0xff);
 var iB:int = ( iTint         & 0xff);

 var iScale:int = (1 - fAmt) * 256;
 iR = (iR * iScale) >>> 8;
 iG = (iG * iScale) >>> 8;
 iB = (iB * iScale) >>> 8;
 return iA + ((iR << 8) + iG << 8) + iB;
}

No comments:

Post a Comment