以下函数转换一个十进制数字,该数字表示一种以Microsoft Windows存储颜色的方式存储的颜色(低字节为红色),并将其转换为Web应用程序所需的十六进制字符串,即“ #RRGGBB”。
function decimalColorToHTMLcolor(number) {
//converts to a integer
var intnumber = number - 0;
// isolate the colors - really not necessary
var red, green, blue;
// needed since toString does not zero fill on left
var template = "#000000";
// in the MS Windows world RGB colors
// are 0xBBGGRR because of the way Intel chips store bytes
red = (intnumber&0x0000ff) << 16;
green = intnumber&0x00ff00;
blue = (intnumber&0xff0000) >>> 16;
// mask out each color and reverse the order
intnumber = red|green|blue;
// toString converts a number to a hexstring
var HTMLcolor = intnumber.toString(16);
//template adds # for standard HTML #RRGGBB
HTMLcolor = template.substring(0,7 - HTMLcolor.length) + HTMLcolor;
return HTMLcolor;
}
本文介绍了一个JavaScript函数,用于将表示颜色的十进制数(Microsoft Windows格式,低字节为红色)转换为HTML应用程序使用的标准十六进制颜色字符串(#RRGGBB)。该函数首先将输入转换为整数,然后分离红、绿、蓝三个颜色通道,并调整其顺序以匹配HTML颜色格式。最后,使用模板填充不足的零,确保输出符合标准格式。
4717

被折叠的 条评论
为什么被折叠?



