• Advertisement
Sign in to follow this  

Extract the R, G, B values from a Flash decimal number

This topic is 2361 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello

I have colour values from a Flash application that are ARGB format(A being the alpha/transparency value).

I have to convert that long decimal number to a RGB/Hexadecimal number in Javascript. I REALLY struggle with bitwise operations. In Flash the colour white is represented by the decimal number: 16777215, black is obviously 0. Other colours are like: 84545883, 4803910 etc which I have no idea what colour they are.

Do you know how I can extract the individual R, G, B & A values from a long(8 digit) number?

Can you help me get these functions to extract the (R,G,B,A) values?


function getA( num )
{
// eg value for num is 84545883
return (parseInt(num,10)) & 0xFF000000; // does this correctly get the A value from a ARGB value?
}

function getR( num )
{
// eg value for num is 84545883
return (parseInt(num,10)) & 0x00FF0000; // does this correctly get the R value from a ARGB value?
}

function getG( num )
{
// eg value for num is 84545883
return (parseInt(num,10)) & 0x0000FF00; // does this correctly get the G value from a ARGB value?
}


Share this post


Link to post
Share on other sites
Advertisement
You are masking out the undesired bits but you are not shifting.

function getA( num ) {
return (parseInt( num, 10 ) & 0xFF000000) >> 24;
}


function getR( num ) {
return (parseInt( num, 10 ) & 0x00FF0000) >> 16;
}

function getG( num ) {
return (parseInt( num, 10 ) & 0x0000FF00) >> 8;
}



L. Spiro

Share this post


Link to post
Share on other sites
Also note that you're better off doing the right shifts before masking, and left shifts after masking.
Thus:function getA(string num)
{
return (parseInt(num, 10) >> 24) & 0xFF;
}

function getR(string num)
{
return (parseInt(num, 10) >> 16) & 0xFF;
}

function getG(string num)
{
return (parseInt(num, 10) >> 8) & 0xFF;
}

function getB(string num)
{
return parseInt(num, 10) & 0xFF;
}

Share this post


Link to post
Share on other sites
In order to make things easier for humans, you can write the numbers in hexadecimal instead of decimal, and then every two hexadecimal digits correspond to a byte, so it's much easier to pick out the individual bytes:
16777215 = 0xFFFFFF (red=0xFF=255, green=0xFF=255, blue=0xFF=255)
4803910 = 0x494D46 (red=0x49=73, green=0x4D=77, blue=0x46=70)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement