Created
February 21, 2013 22:04
-
-
Save wolfiestyle/5008743 to your computer and use it in GitHub Desktop.
js functions to convert between hex string and rgb object colors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function hexToRgb(hex) | |
| { | |
| var res = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); | |
| return res ? { | |
| r: parseInt(res[1], 16), | |
| g: parseInt(res[2], 16), | |
| b: parseInt(res[3], 16) | |
| } : null; | |
| } | |
| function rgbToHex(r, g, b) | |
| { | |
| return ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment