Home > Web Front-end > JS Tutorial > JavaScript method to convert hexadecimal color value to RGB_javascript skills

JavaScript method to convert hexadecimal color value to RGB_javascript skills

WBOY
Release: 2016-05-16 16:15:06
Original
1420 people have browsed it

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:

Copy code The code 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值
                

                    
              
              


                   R:
G:
                  B:
                            
                                                                                                                                              


The operation effect is as shown below:

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template