sazr
sazr

Reputation: 25928

Convert RGBA to Hex or ARGB to Hex

I am trying to convert a decimal colour code from a Flash Application into a hexadecimal colour code for HTML display

I have these numbers which are 8 digits long, but I am not sure if they are ARGB or RGBA. Is there a way to figure this out from the colour codes themselves?

I have a javascript function that can convert a decimal to a hexadecimal number but I am not compensating for the A value(or removing it). Can you help me fix my function to extract/remove the A value from the RGB decimal code?

function decimalToHex( num )
{
  if (num == null || num == "undefined") { return "0xFFFFFF"; }

  var intNum = (parseInt(num,10)) & 8;  // does this remove the most significant 8 bits?
  return intNum.toString(16);
}

Upvotes: 2

Views: 6795

Answers (1)

cbranch
cbranch

Reputation: 4769

If the alpha value is in the highest byte, then bitwise AND with 0x00FFFFFF to remove that. So:

var intNum = (parseInt(num,10)) & 0x00FFFFFF;

Or, if the alpha value is in the lowest byte, bitwise AND with 0xFFFFFF00, then shift right 8:

var intNum = (parseInt(num,10) & 0xFFFFFF00) >> 8;

Upvotes: 6

Related Questions