Skip to content

Instantly share code, notes, and snippets.

@coodoo
Created March 2, 2013 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coodoo/5069710 to your computer and use it in GitHub Desktop.
Save coodoo/5069710 to your computer and use it in GitHub Desktop.
get random color in hex or rgba format.
/**
* @param useRGB Boolean, true for rgba format, false will return color in hex format
* @param randomAlpha Boolean, when using rgba format, decide whether to randomnize alpha value too
*/
function getRandomColor( useRGB, randomAlpha ){
if( useRGB ){
return "rgba("+
Math.floor(Math.random()*256)+","+
Math.floor(Math.random()*256)+","+
Math.floor(Math.random()*256)+","+
(randomAlpha ? Math.random().toFixed(1) : 1) +")";
}
return Math.floor(Math.random() * 0xFFFFFF).toString(16);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment