I具有ARGB格式的闪存应用程序的颜色值(A是alpha/透明性值)。
我必须在Javascript中将这个长十进制数字转换为RGB/十六进制数。
你知道我如何从一个长(8位)数字中提取单个R,G,B&A值吗?
下面是我的函数,它将一个数字转换为十六进制,但它不够好,因为它需要单独转换值(R、G、B、A):
function decimalToHex( num )
{
if (num == null || num == "undefined") { return "#FFFFFF"; }
var intNum = (parseInt(num,10)) & 0x00FFFFFF;
var strNum = intNum.toString(16);
while (strNum.length <6) { strNum = "0"+strNum; }
return "#"+strNum; //+intNum.toString(16);
}
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?
}
function getB( num )
{
// eg value for num is 84545883
return (parseInt(num,10)) & 0x000000FF; // does this correctly get the B value from a ARGB value?
}发布于 2011-11-01 05:40:05
使用匹配:
var argb = '12345678';
var bits = argb.match(/\d\d/g);
alert(bits); // ['12', '34', '56', '78']现在,这些位在数组中,您可以将其转换为十六进制和级联,以获得十六进制RGB值。如果原始值实际上是十进制值,那么每个值(000到255)可能允许3个字符,您可能需要:
var argb= '000101234034'
var bits = argb.match(/\d\d\d/g); // ['000', '101', '234', '034']发布于 2011-11-01 05:47:07
看看这是否能帮你。
https://stackoverflow.com/questions/7962750
复制相似问题