Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I randomly generate HTML hex color codes using JavaScript? [duplicate]

Tags:

javascript

Possible Duplicate:
Random Color generator in Javascript

I have some data I'd like to display in different colors and I want to generate the colors randomly if possible. How can I generate the Hex Color Code using JavaScript?

like image 408
Achilles Avatar asked Feb 23 '11 15:02

Achilles


People also ask

How do I randomize a color in JavaScript?

var randomColor = Math. floor(Math. random()*16777215).


1 Answers

This will generate a random number within the bounds and convert it to hexadecimal. It is then padded with zeros so that it's always a valid six-digit hex code.

'#'+(Math.random() * 0xFFFFFF << 0).toString(16).padStart(6, '0'); 
like image 182
DanS Avatar answered Oct 16 '22 21:10

DanS