Created
March 25, 2012 13:02
-
-
Save stefanocudini/2193559 to your computer and use it in GitHub Desktop.
rgb2hex, hex2rgb html color
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 rgb2hex(rgb) //converte da array di valori 0-255 e stringa esadecimale | |
| { | |
| function toHex(n) { | |
| n = parseInt(n,10); | |
| if (isNaN(n)) return "00"; | |
| n = Math.max(0,Math.min(n,255)); | |
| return "0123456789ABCDEF".charAt((n-n%16)/16) | |
| + "0123456789ABCDEF".charAt(n%16); | |
| } | |
| return '#'+toHex(rgb[0])+toHex(rgb[1])+toHex(rgb[2]); | |
| } | |
| function hex2rgb(hex) | |
| { | |
| var c, o = [], k = 0, m = hex.match(/^#(([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})|([0-9a-f])([0-9a-f])([0-9a-f]))$/i); | |
| if (!m) return {r:0,g:0,b:0}; | |
| for (var i = 2, s = m.length; i < s; i++) { | |
| if (undefined === m[i]) continue; | |
| c = parseInt(m[i], 16); | |
| o[k++] = c + (i > 4 ? c * 16 : 0); | |
| } | |
| return o; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment