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

Started by
2 comments, last by alvaro 12 years, 5 months ago
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?
}


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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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;
}
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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)

This topic is closed to new replies.

Advertisement