Jump to content

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

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

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
3 replies to this topic

#1 gretty   Members   

273
Like
1Likes
Like

Posted 31 October 2011 - 11:56 PM

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?
}



#2 L. Spiro   Members   

25464
Like
1Likes
Like

Posted 01 November 2011 - 12:21 AM

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

#3 iMalc   Members   

2466
Like
0Likes
Like

Posted 01 November 2011 - 12:24 PM

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

#4 Álvaro   Members   

20922
Like
0Likes
Like

Posted 01 November 2011 - 12:47 PM

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)




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.