Web site development will inevitably encounter the color of the situation, such as changing the color of pictures and so on.
Let's take a look at the RGB turn hex method
function Rgb2hex () {
hexcolor = "#";
var RGB = Array.prototype.slice.call (arguments);
var rgblength = rgb.length;
for (var i=0; i<rgblength; i++) {
var c = "0123456789ABCDEF",
B = "",
a = rgb[i]%;
b = C.substr (A, 1);
A = (rgb[i]-a)/+;
Hexcolor + = C.substr (A, 1) + B;
}
return hexcolor;
}
Call
Console.log (Rgb2hex (255, 0, 0)); #FF0000
See where the logic has been dizzy please open the purification summoner skills to relieve control, seriously watch, no words in a change posture, to ensure the walk bit ...
In fact, the principle is not difficult, mainly on the 16 operation, and then the matching assembly, of course, can superficial understanding,, into the next inning.
First inning on your knees, come to the second inning--Hex turn RGB
function Hex2rgb (hexcolor) {
if (hexcolor.substr (0, 1) = = "#") Hexcolor = hexcolor.substring (1);
Hexcolor = Hexcolor.tolowercase ();
b = new Array ();
for (x = 0; x < 3; + +) {
b[0] = hexcolor.substr (x * 2, 2)
b[3] = "0123456789abcdef";
B[1] = b[0].substr (0, 1)
b[2] = b[0].substr (1, 1)
b[20 + x] = B[3].indexof (b[1]) * + b[3].indexof (b[2])
}
return b[20] + "," + b[21] + "," + b[22];
}
Call
Console.log (Hex2rgb (' #ff0000 ')); 255,0,0
Alas, again charged,,, completely dizzy ... Can only be collected by the past.