The example in this article describes how to convert hexadecimal color values to RGB using JavaScript. Share it with everyone for your reference. The specific implementation method is as follows:
16进制颜色值转RGB
<script><br>
function hexToR(h) {<br>
return parseInt((cutHex(h)).substring(0, 2), 16)<br>
}<br>
function hexToG(h) {<br>
return parseInt((cutHex(h)).substring(2, 4), 16)<br>
}<br>
function hexToB(h) {<br>
return parseInt((cutHex(h)).substring(4, 6), 16)<br>
}<br>
function cutHex(h) {<br>
return h.charAt(0) == "#" ? h.substring(1, 7) : h<br>
}<br>
function setBgColorById(id, sColor) {<br>
var elems;<br>
if (document.getElementById) {<br>
if (elems = document.getElementById(id)) {<br>
if (elems.style) elems.style.backgroundColor = sColor;<br>
}<br>
}<br>
}<br>
</script>
JavaScript原生16进制颜色值转RGB值
The operation effect is as shown below:
I hope this article will be helpful to everyone’s JavaScript programming design.