将AARRGGBB颜色的Alpha通道相乘

时间:2013-07-08 01:50:34

标签: actionscript-3 actionscript colors bitmap rendering

我有一个AARRGGBB值,用于填充我在尝试使用照明引擎时使用的网格中的单元格:

var color:Number = 0xFF000000; // Full opaque black.

有些光源有半径参数。从光源单元到该半径内的附近单元测量距离。然后给每个附近的单元格赋予百分比值,即:

distanceFromSource / sourceRadius

因此,更高的百分比表示远离源的细胞。

我想将上面颜色的alpha通道乘以百分比,然后用结果值填充单元格。基本上我想要AARRGGBB值中的0-100%AA。当我尝试直接乘法时,我得到奇怪的结果:

enter image description here

我认为我需要使用特殊运算符,以及BitmapDataChannel中的值。不幸的是,这就是我遇到的问题。

  

如何将AARRGGBB颜色中的Alpha通道乘以百分比?

1 个答案:

答案 0 :(得分:3)

您需要保留像素的rgb值。只乘以uint的alpha字节。

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);
}
相关问题