将 RGBA 转换为十六进制或 ARGB 转换为十六进制 [英] Convert RGBA to Hex or ARGB to Hex
问题描述
我正在尝试将 Flash 应用程序中的十进制颜色代码转换为用于 HTML 显示的十六进制颜色代码
我有这些 8 位数字,但我不确定它们是 ARGB 还是 RGBA.有没有办法从颜色代码本身来解决这个问题?
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?
我有一个 javascript 函数,可以将十进制数转换为十六进制数,但我没有补偿 A 值(或删除它).你能帮我修复我的函数以从 RGB 十进制代码中提取/删除 A 值吗?
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);
}
推荐答案
如果 alpha 值在最高字节中,则按位 AND 与 0x00FFFFFF 将其删除.所以:
If the alpha value is in the highest byte, then bitwise AND with 0x00FFFFFF to remove that. So:
var intNum = (parseInt(num,10)) & 0x00FFFFFF;
或者,如果 alpha 值在最低字节中,与 0xFFFFFF00 按位与,然后右移 8:
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;
这篇关于将 RGBA 转换为十六进制或 ARGB 转换为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!