Skip to content

Instantly share code, notes, and snippets.

@sameerjoshi
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save sameerjoshi/ae5fa913087acfb46fb7 to your computer and use it in GitHub Desktop.

Select an option

Save sameerjoshi/ae5fa913087acfb46fb7 to your computer and use it in GitHub Desktop.
JS Random color generator
//Ref: http://www.paulirish.com/2009/random-hex-color-code-snippets/
//1
'#' + (function co(lor){ return (lor +=
[0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f'][Math.floor(Math.random()*16)])
&& (lor.length == 6) ? lor : co(lor); })('');
//2
(function(m,s,c){return (c ? arguments.callee(m,s,c-1) : '#') +
s[m.floor(m.random() * s.length)]})(Math,'0123456789ABCDEF',5)
//3
'#'+(function lol(m,s,c){return s[m.floor(m.random() * s.length)] +
(c && lol(m,s,c-1));})(Math,'0123456789ABCDEF',4)
//4
'#'+'0123456789abcdef'.split('').map(function(v,i,a){
return i>5 ? null : a[Math.floor(Math.random()*16)] }).join('');
//5 - Perfect
'#'+Math.floor(Math.random()*16777215).toString(16);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment