2

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

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

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

distanceFromSource / sourceRadius

因此,较高的百分比表示离源较远的单元格。

我想将上面颜色的 alpha 通道乘以百分比,并用结果值填充单元格。基本上我希望 AARRGGBB 值的 AA 介于 0-100% 之间。当我尝试做直接乘法时,我得到奇怪的结果:

在此处输入图像描述

我相信我需要为此使用特殊运算符,以及BitmapDataChannel. 不幸的是,这是我被卡住的地方。

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

4

1 回答 1

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);
}
于 2013-07-08T03:21:38.837 回答