0

我需要编写一个使用 16 位颜色值绘制像素的函数。我目前正在使用以下代码来绘制每个像素。

var pixel:Shape = new Shape();
pixel.graphics.beginFill(//16bit colour value);
pixel.graphics.drawRect (xVal, yVal, pixelWidth, pixelHeight);

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Graphics.html

我需要将 16 位颜色值(例如 111110000000000b,红色)应用到上述图形 API 函数,但似乎该函数需要 32 位 RGB 颜色值。我还查看了其他可能的方法,例如...

BitmapData()

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#BitmapData()

但它也需要 32 位 RGB 值。AS3 中是否有处理此问题的 API?或者是否有一个看似可以将 16 位颜色值转换为 32 位颜色值的公式?

4

1 回答 1

3

从Convert 16bit color to 32bit到 AS3的c++代码端口:

public static function rgb16ToRgb32(a:uint):uint
{
    /* 1. Extract the red, green and blue values */

    /* from rrrr rggg gggb bbbb */
    var r:uint = (a & 0xF800) >> 11;
    var g:uint = (a & 0x07E0) >> 5;
    var b:uint = (a & 0x001F);

    /* 2. Convert them to 0-255 range:
    There is more than one way. You can just shift them left:
    to 00000000 rrrrr000 gggggg00 bbbbb000
    r <<= 3;
    g <<= 2;
    b <<= 3;
    But that means your image will be slightly dark and
    off-colour as white 0xFFFF will convert to F8,FC,F8
    So instead you can scale by multiply and divide: */

    r = r * 255 / 31;
    g = g * 255 / 63;
    b = b * 255 / 31;
    /* This ensures 31/31 converts to 255/255 */

    /* 3. Construct your 32-bit format (this is 0RGB): */
    return (r << 16) | (g << 8) | b;

    /* Or for BGR0:
    return (r << 8) | (g << 16) | (b << 24);
    */
}

测试:

    var reb16b:String = "1111100000000000b";
    var red16:uint = parseInt(reb16b, 2);
    trace("red16 = "+reb16b+ " [0x"+red16.toString(16).toUpperCase()+"]");

    var red32:uint = rgb16ToRgb32(red16);
    trace("red32 = 0x"+red32.toString(16).toUpperCase());

输出:

    red16 = 1111100000000000b [0xF800]
    red32 = 0xFF0000

因此,您可以使用以下代码绘制 16 位红色记录:

    var pixel:Shape = new Shape();
    pixel.graphics.beginFill(rgb16ToRgb32(0xF800));
    pixel.graphics.drawRect (xVal, yVal, pixelWidth, pixelHeight);
于 2013-09-23T07:48:39.423 回答