Skip to content

Instantly share code, notes, and snippets.

@stefanocudini
Created May 31, 2012 17:11
Show Gist options
  • Select an option

  • Save stefanocudini/2844814 to your computer and use it in GitHub Desktop.

Select an option

Save stefanocudini/2844814 to your computer and use it in GitHub Desktop.
random hex color javascript
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