Created
March 16, 2015 17:35
-
-
Save bgadrian/99f244fabf73f2cb9f12 to your computer and use it in GitHub Desktop.
JS color translate css from RGB to HEX
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
| //usage | |
| //alert (RGB_to_HEX('rgb(119,110,200)')); | |
| function RGB_to_HEX(RGB) | |
| { | |
| var parts = RGB | |
| .match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/); | |
| // parts now should be ["rgb(0, 70, 255", "0", "70", "255"] | |
| delete (parts[0]); | |
| for (var i = 1; i <= 3; ++i) { | |
| parts[i] = parseInt(parts[i]).toString(16); | |
| if (parts[i].length == 1) parts[i] = '0' + parts[i]; | |
| } | |
| return '#' + parts.join(''); // "0070ff" | |
| //thanks to http://stackoverflow.com/questions/638948/background-color-hex-to-javascript-variable-jquery | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment