Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


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


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   -  Reputation: 212

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



Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 14001

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#3 iMalc   Crossbones+   -  Reputation: 2313

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   Crossbones+   -  Reputation: 13658

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.



PARTNERS