Reputation: 632
How would you manually calculate RGB multipliers and offsets to adjust the brightness of a color so that an argument of -1 was all black and 1 was all white?
If it's less than 1, it's easy. R, G, and B are just multiplied by (1 + brightness).
But how would you calculate the offsets for brightness values greater than 0?
Upvotes: 1
Views: 258
Reputation: 7316
It is simple channel per channel interpolation math. It does not look simple only because there are three channels and they need de/serialization for various purposes.
// Usage.
acoolor:uint = parseRGB(200, 150, 100);
trace(colorToRGB(brightNess(acoolor, 0.5)));
trace(colorToRGB(brightNess(acoolor, -0.5)));
// Implementation.
function parseRGB(ared:uint, agreen:uint, ablue:uint):uint
{
var result:uint;
result += (ared << 16) & 0xFF0000;
result += (agreen << 8) & 0xFF00;
result += (ablue) & 0xFF;
return result;
}
function colorToRGB(acolor:uint):Array
{
result = new Array;
result[0] = (acolor >> 16) & 0xFF;
result[1] = (acolor >> 8) & 0xFF;
result[2] = (acolor) & 0xFF;
return result;
}
function RGBToColor(anrgb:Array):uint
{
return parseRGB.apply(this, anrgb);
}
function brightChannel(achannel:uint, adjust:Number):uint
{
if (adjust <= -1) return 0;
if (adjust >= 1) return 255;
if (adjust < 0) return achannel * (1 + adjust);
if (adjust > 0) return achannel + (255 - achannel) * adjust;
// If adjust == 0
return achannel;
}
function brightNess(acolor:uint, adjust:Number):uint
{
var anrgb:Array = colorToRGB(acolor);
for (var i:int = 0; i < 3; i++)
anrgb[i] = brightChannel(anrgb[i], adjust);
return RGBToColor(anrgb);
}
Upvotes: 1