Reputation: 39456
I have an AARRGGBB
value which is used to fill cells in a grid that I am using in my attempt at a lighting engine:
var color:Number = 0xFF000000; // Full opaque black.
There are light sources which have a radius parameter. The distance is measured from the light source cell to nearby cells within that radius. A percentage value is then given to each nearby cell, which is:
distanceFromSource / sourceRadius
So a higher percentage represents a cell further away from the source.
I want to multiply the alpha channel in the color I have above by the percentage, and fill the cells with the resulting values. Basically I want between 0-100% AA in the AARRGGBB value. When I try to do straight multiplication I get odd results:
I believe I need to use special operators for this, alongside the values seen in BitmapDataChannel
. Unfortunately this is where I've gotten stuck.
How can I multiply the alpha channel in an AARRGGBB color by a percentage?
Upvotes: 2
Views: 851
Reputation: 5087
You need to preserve the rgb values of your pixel. Only multiply the alpha byte of the uint.
function multiplyAlpha(color:uint, percent:Number):uint
{
//returns the pixel with it's aplha value multiplied by percent
//percent is expected to be in the range 0..1
var a:uint = (color >> 24) * percent;
var rgb:uint = color & 0x00ffffff;
return ((a<<24) | rgb);
}
function setAlphaByPercent(color:uint, percent:Number):uint
{
//returns the pixel with it's a new alpha value based on percent
//percent is expected to be in the range 0..1
var a:uint = 0xff * percent;
var rgb:uint = color & 0x00ffffff;
return ((a<<24) | rgb);
}
Upvotes: 3