Convert the hexadecimal color in css to the rgb format, cssrgb
Annotation of the dojo/_ base/Color module, source address https://github.com/robinxiong/dojo/blob/master/_base/Color.js
Function fromHex (color) {/* summary: Convert the css attribute value to the "# fff"-> 0 xfff length to 4, A character occupies an attribute. 0x10 = 16 (4 power of 2, that is, 4 digits after 1) = 1 0000, then a placeholder of f is mask = 1111, which is used to occupy the last four bits (& used to retain the last four bits). Other High bits are cleared and each traversal starts from the last four bits, therefore, the B value is obtained first. If it is difficult to move the 4 digits, the B attribute is obtained after the g is obtained, the 4 digits are moved, and the r is obtained: 0000 1111 1111 1111 & 0000 0000 0000 1111 (only 16 bits are listed here, normally 32 bits) 0000 0000 0000 1111 B is saved to the variable c 0000 1111 1111 1111> 4, -> 0000 0000 1111 1111 get g attribute 0000 0 000 1111 1111 & 0000 0000 0000 1111 0000 0000 0000 1111 get the value of g, save to variable c in four places, get the r value "ffffff"-> 0 xffffff length is 8, each two characters represents a function 0xff = 1111 1111 each time get the last eight digits, color value */var t = {}, bits = (color. length = 4 )? 4: 8, // if it is shorthand, # fff, then bits is 4 bits, each representing an attribute, the other values are 8 bits, and each two bits represent an attribute # FFFF00 mask = (1 <bits)-1; // represents a byte placeholder, which shifts four or eight bits to the left, var a = (1 </4)-1-> 10000-1,. toString (2); // 1111, or 8-bit 1111 1111 color = Number ("0x" + color. substr (1); // # convert ff0000 to hexadecimal 0xff0000; if (isNaN (color) {return null; // Color} ["B ", "g", "r"]. forEach (function (x) {var c = color & mask; color >>= bits; t [x] = bits = 4? 17 * c: c; // 0 xfff. An f should represent 255. It should be [0-255], divided by 15 equal portions, with an interval of 17. Therefore, the obtained value must be multiplied by 17 to represent the value of 255 in rgb}); t. a = 1; return t; // Color} console. log (fromHex ("# 00f") // {r: 0, g: 0, B: 255, a: 1}
How can I convert the RGB color value to hexadecimal in the code?
Simple shift and bitwise addition:
Int r, g, B;
Int c; // TC use long int;
// If the order is RRGGBB
C = r <16 | g <8 | B;
// If the order is BBGGRR
C = B <16 | g <8 | r;
---------------------------------
For example:
# Include "stdio. h"
Int RGB (int r, int g, int B)
{
Return r <16 | g <8 | B;
}
Void main ()
{
Int r, g, B, c;
R = 255; g = 255; B = 255;
C = RGB (r, g, B );
Printf ("0x % 06x", c );
}
How to convert hexadecimal to RGB
Each two hexadecimal values is a bit of RGB, such as BLACK = 0 XFFFFFF = RGB (FF, FF, FF)