Bitwise Ops [ Pt. 2 ] Colors: Hex to RGB
Again, for another purpose but sharing for those interested.
A Pen by Tiffany Rayside on CodePen.
Bitwise Ops [ Pt. 2 ] Colors: Hex to RGB
Again, for another purpose but sharing for those interested.
A Pen by Tiffany Rayside on CodePen.
| <h4>Hexadecimal Color <br>To 16-Bit Integer<br> To RGB Values<br> Using Bitwise Shifting </h4> | |
| <form> | |
| <div> | |
| <input type="text" value="E9848C" id="hex" name="hex" /> | |
| <input type="button" value="Do It" onclick="colorBit();" /> | |
| </div> | |
| </form> | |
| <div id='res'></div> |
| var $ = function(id) { | |
| return document.getElementById(id); | |
| }; | |
| var colorBit = function() { | |
| var hex = $('hex').value; | |
| var _16int = parseInt(hex, 16); | |
| var r = (_16int >> 16) & 0xFF; | |
| var g = (_16int >> 8) & 0xFF; | |
| var b = _16int & 0xFF; | |
| $('res').style.color = '#' + hex; | |
| $('res').innerHTML = | |
| 'Hex: ' + '#' + hex + | |
| '<br>16-Bit Int: ' + _16int + | |
| '<br>Red (R): ' + r + | |
| '<br>Green (G): ' + g + | |
| '<br>Blue (B): ' + b; | |
| } |
| @import url(http://fonts.googleapis.com/css?family=Electrolize); | |
| body { | |
| background: hsla(0, 0%, 10%, 1); | |
| margin: 5%; | |
| overflow: hidden; | |
| color: hsla(255, 255%, 255%, 1); | |
| letter-spacing: 3px; | |
| font-size: 2em; | |
| line-height: 1.5em; | |
| font-family: 'Electrolize', sans-serif; | |
| } | |
| #res { | |
| padding-top: 1em; | |
| background: hsla(0, 0%, 20%, .1); | |
| } | |
| input { | |
| color: hsla(0, 0%, 0%, 1); | |
| padding: .2em; | |
| } |