Convert RGB to Hex color
Below is a function to convert rgb to hex color. But it is not totally correct. With (0, 255, 0) (#00ff00). it return ff00 and so it is not valid color. I need help to modify it to return correct hex value.
function rgbToHex(r, g, b) {
var rgb = b | (g << 8) | (r << 16);
return rgb.toString(16);
}
这个怎么样:
//...
return (0x1000000 | rgb).toString(16).substring(1);
尝试这个:
return ("000000"+rgb.toString(16)).slice(-6);
// ^----returns last 6 chars
return ((b | g << 8 | r << 16) / 16777216).toString(16).substring(2);
要么
return ((b | g << 8 | r << 16) / 0x1000000).toString(16).substring(2);
链接地址: http://www.djcxy.com/p/87804.html
上一篇: RGB / HEX / HSL拆分16777216基本16种颜色范围内的颜色
下一篇: 将RGB转换为十六进制颜色