Every dayRandom Generators

Random Color
generator.

Draw random colors for a palette, lock the ones you like, and see which text reads on each.

How it's worked out

With the Any color style, red, green and blue are each a whole number from 0 to 255, drawn on their own. That makes 16,777,216 possible colors, and each one is equally likely.

colors\displaystyle \text{colors}=2563\displaystyle {}= 256^{3}
2563\displaystyle 256^{3}=16,777,216\displaystyle {}= 16{,}777{,}216

The numbers come from your browser’s cryptographic generator, crypto.getRandomValues. A plain remainder would make some values a little more common than others, so a raw draw that falls in the uneven top slice is thrown away and drawn again. This is called rejection sampling, and it leaves no bias.

The other styles work in HSL: hue, saturation and lightness. Hue is any whole degree from 0 to 359, while saturation and lightness stay inside the style’s range. Pastel keeps lightness from 80% to 90%, Vivid from 45% to 60% and Dark from 12% to 30%. That is why a style feels consistent even though every hue can turn up.

For text, the tool uses relative luminance from WCAG, which weights green far more than blue because our eyes do. The contrast ratio compares the lighter color with the darker one.

contrast ratio\displaystyle \text{contrast ratio}=L1+0.05L2+0.05\displaystyle {}= \frac{L_1 + 0.05}{L_2 + 0.05}

Here L1L_1 is the luminance of the lighter color and L2L_2 of the darker one, each from 0 for black to 1 for white. Each swatch shows whichever of black or white text scores higher. Where the two scores meet, the ratio is the square root of 21, so black or white text never gets less than 4.58:1 on any color.

Worked examples

Maya keeps one red and redraws the rest for a poster

Maya draws five vivid colors: #9EF702, #EF3E58, #EF2C1A, #E81721, #7BEB47. She likes the second one, #EF3E58, a red, so she locks it and draws again. The new set is #DB530F, #EF3E58, #47EB89, #1CD981, #2BE33A, with her red still in place.

Every color takes black or white text at AA or better. The weakest is #DB530F, with black text at 5.2:1 (AA). Inside the palette, though, the strongest pair is #DB530F and #47EB89 at only 2.5:1, which fails even the large text level. Bright, saturated colors rarely differ enough in luminance for that, so she should set her headline in black or white, not in one palette color on another.

Open this example in the tool

Leo picks eight pastel colors for tags

Leo needs eight soft colors for task labels. The Pastel style gives #F4EBB3, #D4F3CE, #F2ABB8, #DBD7F4, #BCE6D4, #F6C5C1, #B2D3EB, #B5F2EE.

All eight take black text. The lowest score is #F2ABB8, a light red, with black text at 11.2:1 (AAA). Pastels are light by design, so dark text on them is easy to read, and the label names stay sharp even at small sizes.

Open this example in the tool

Four dark colors for a dark theme

The Dark style draws hsl(84, 61%, 12%), hsl(306, 69%, 14%), hsl(106, 57%, 12%), hsl(78, 41%, 27%). Every one takes white text.

HSL lightness and contrast do not line up. hsl(306, 69%, 14%) has the higher lightness, 14%, yet gets 16.2:1 with white. hsl(84, 61%, 12%), at 12%, gets 13.6:1. The first color looks darker to the eye because it holds less green, and the luminance formula counts that while HSL does not.

Open this example in the tool

Reading your result

WCAG 2.2 contrast levels for text
LevelNormal textLarge text
AA4.5:13:1
AAA7:14.5:1

Large text means at least 18 point, or 14 point in bold. In CSS pixels that is about 24 px, or about 18.5 px in bold. Icons and borders that people need to see should reach 3:1 against what is next to them.

Since every color passes AA with black or white, the levels that tell colors apart are AAA and the pairs inside the palette. Mid-tone colors, such as a pure red, sit near the 4.58:1 floor. Very light and very dark colors pass 7:1 with ease.

The ratio is cut, not rounded. WCAG compares the exact value, so 4.499:1 fails a 4.5:1 rule even though it looks like 4.5 when rounded. The figures here are cut to one place for the same reason.

The name is a rough guide. Names such as light blue or dark green come from the hue and lightness. They help you talk about a color, but the hex code is the exact value to share.

Common mistakes

  • Trusting HSL lightness for text. Pure yellow and pure blue are both 50% lightness in HSL. Yet yellow takes black text at 19.5:1 (AAA), and blue takes white text at 8.5:1 (AAA). Check the ratio, not the lightness number.
  • Putting one palette color on another. Random colors often share a similar luminance. The pairs table shows which two colors, if any, can carry text on each other.
  • Writing your own random hex code with a shortcut. A common snippet multiplies a random fraction by 16,777,215 and converts it to hex. It can never give white, FFFFFF, and it drops leading zeros, so some codes come out with five digits.
  • Expecting the same color everywhere. A hex code names an sRGB color. Screens differ in brightness and range, and print uses inks that cannot match every bright screen color, so check a brand palette on the devices that matter.
  • Sending a screenshot instead of the codes. A screenshot can shift colors through compression and display settings. Send the share link or the exported table.

Questions people ask

Copy the share link. It holds the hex codes of every color, so anyone who opens it sees the same palette, with your locked colors still marked. The draws themselves are not seeded, because the browser’s cryptographic generator cannot be replayed, so the link is the way to keep a palette.

Sources