最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert RGBA to Hex or ARGB to Hex - Stack Overflow

programmeradmin4浏览0评论

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 pensating 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);
}

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 pensating 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);
}
Share Improve this question asked Nov 1, 2011 at 2:36 sazrsazr 25.9k70 gold badges214 silver badges386 bronze badges 3
  • Your question doesn't make any sense. Are you trying to convert dec to hex, or RGBA with a background color to RGB? – Brad Commented Nov 1, 2011 at 2:40
  • Is there a way to figure this out from the colour codes themselves? No; at least not in all cases. – Peter Olson Commented Nov 1, 2011 at 2:40
  • well I'm trying to convert a flash col value(which I am unsure if its either ARGB or RGBA) to some thing like #ffffff form. So would that be decimal to hex? – sazr Commented Nov 1, 2011 at 2:45
Add a ment  | 

1 Answer 1

Reset to default 6

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;
发布评论

评论列表(0)

  1. 暂无评论