Question

I need to write a function that draws pixels using 16 bit colour values. I am currently using following code to draw each pixels.

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

I need to apply 16 bit colour value (such as 111110000000000b, red colour) to above Graphics API function, however it seems like the function require 32 bit RGB colour value. I have also had a look at other possible approaches such as ...

BitmapData()

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

But it also requires 32 bit RGB values. Is there an API in AS3 that handles this issue? Or is there a formula that can seemingly convert a 16 bit colour value into a 32 bit colour value?

Était-ce utile?

La solution

Port of c++ code from Convert 16bit colour to 32bit to AS3:

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);
    */
}

Test:

    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());

output:

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

So you can draw 16 bit red color rec with the code:

    var pixel:Shape = new Shape();
    pixel.graphics.beginFill(rgb16ToRgb32(0xF800));
    pixel.graphics.drawRect (xVal, yVal, pixelWidth, pixelHeight);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top