Created
May 31, 2012 17:11
-
-
Save stefanocudini/2844814 to your computer and use it in GitHub Desktop.
random hex color javascript
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 track_randcolor() //restituisce un array con valori RGB 0-255 | |
| { | |
| 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); | |
| } | |
| function rgb2hex(rgb) { //converte da array di valori 0-255 e stringa esadecimale | |
| return '#'+toHex(rgb[0])+toHex(rgb[1])+toHex(rgb[2]); | |
| } | |
| function minmaxrandom(nmin, nmax) { //genera numero casuale compreso in un range di valori | |
| return Math.random() * (nmax - nmin) + nmin; | |
| } | |
| function c() { | |
| return Math.floor(minmaxrandom(150,240)).toString();//30 e 240 senno' vengono troppo scuri/chiari | |
| } | |
| return rgb2hex([c(),c(),c()]); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment