Random color generator
Generate a random color and get its hex, RGB and HSL codes. Choose any color, light, dark, pastel, vivid or muted, and keep a history of the ones you liked.
#7C9A6E
#111412 text · 5.9:1
Recent colors
Eight more to choose from
The kinds of color
Any color samples the whole RGB cube. The other kinds are ranges I chose for this tool, not standards, so a Pastel here may not match another site's pastel.
| Kind | HSL saturation | HSL lightness |
|---|---|---|
| Light | 20–100% | 70–95% |
| Dark | 20–100% | 8–30% |
| Pastel | 40–90% | 78–92% |
| Vivid | 85–100% | 45–60% |
| Muted | 15–40% | 35–65% |
Generate a random hex color in JavaScript
'#' + Math.floor(Math.random() * 0x1000000).toString(16).padStart(6, '0');
This is the same idea as the Any color mode, in one line. For a narrower range, choose the hue, saturation and lightness separately and convert with the converter.
Using a random color
- Check the text. The line under the hex shows the more legible text color and its contrast. The contrast checker tests any pair.
- Build around it. Open the color in the color wheel for harmonies, or in the palette generator for a full set.
- Name it. The color picker shows the nearest CSS color name for any hex.
Frequently asked questions
How does the random color generator work?
In Any color mode it draws three random bytes for red, green and blue, so every one of the 16,777,216 colors a six-digit hex code can name is equally likely. The other kinds pick a random hue and then a saturation and lightness inside a range.
Is it truly random?
It uses the browser's crypto.getRandomValues, a cryptographically strong generator, rather than Math.random. For choosing a color the difference does not matter, but it means there is no pattern to the sequence.
How do I generate a random hex color in JavaScript?
One line works: '#' + Math.floor(Math.random() * 0x1000000).toString(16).padStart(6, '0'); Math.random() gives a number from 0 up to but not including 1, the multiplication spreads it over 0 to 16,777,215, and padStart keeps the six digits.
Why do random colors often look muddy?
Most colors in the full RGB range are mid-tones. The Light, Dark, Pastel and Vivid kinds narrow the range so more of the results are usable as they come.
Can I get a color back after generating a new one?
Yes. The last twelve colors appear under Recent colors, and clicking one brings it back. The address bar also updates with the color, so copying the page link saves it.
Content updated