The core of JavaScript color conversion is the transformation between transitions. RGB format is actually decimal notation, so the hexadecimal color and RGB color conversion is hexadecimal and decimal conversion between.
16 conversion to decimal is relatively easy, the core code is as follows: parseint ("0xFF"), the result is 255, "0x" indicates that the current is 16, because the parseint after no parameters, the default is to convert to 10 into the system.
Decimal conversion to 16, the core code is as follows: Var num=255; Num.tostring (16), and the result is ff. "16″ represents a numeric conversion to a 16-in string.
Cases
The code is as follows |
Copy Code |
var r = parseint (Math.random () *16); var g = parseint (Math.random () *16); var B = parseint (Math.random () *16); Convert to 16, using int.tostring (16). Accordingly, you can also use ToString (Ten), ToString (8), ToString (2) to convert to decimal, octal, binary, etc. R = r.tostring (16); g = g.tostring (16); b = b.tostring (16); RGB values that are spliced into colors var color = ' # ' +r+g+b; |
The system converts to hexadecimal or octal, or vice versa
Do you write a separate function to convert hexadecimal (or octal)? Stop now! There are easier ready-made functions to use:
JavaScript code copy content to clipboard
code is as follows |
copy code |
• (int). ToString (16); converts int to hex, eg => "C" • (int). toString (8); //converts in T to octal, eg. => parseint (string,16)//converts hex to int, eg. "FF" => 255 parseint (string,8)//converts octal to int, eg. "=>" |