This article describes how to convert a hexadecimal color value to an RGB color value in JavaScript. It is a typical numeric conversion technique in javascript and is of great practical value, for more information about how to convert hexadecimal color values to RGB, see the example in this article. Share it with you for your reference. The specific implementation method is as follows:
The Code is as follows:
Hexadecimal color value to RGB
Script
Function hexToR (h ){
Return parseInt (cutHex (h). substring (0, 2), 16)
}
Function hexToG (h ){
Return parseInt (cutHex (h). substring (2, 4), 16)
}
Function hexToB (h ){
Return parseInt (cutHex (h). substring (4, 6), 16)
}
Function cutHex (h ){
Return h. charAt (0) = "#"? H. substring (1, 7): h
}
Function setBgColorById (id, sColor ){
Var elems;
If (document. getElementById ){
If (elems = document. getElementById (id )){
If (elems. style) elems. style. backgroundColor = sColor;
}
}
}
Script
JavaScript native hexadecimal color value to RGB Value
Shows the running effect:
I hope this article will help you design javascript programs.