ColorDice

HEX Color Generator — Instant HEX, RGB & HSL Codes

Click once to get a random HEX color code with its RGB and HSL equivalents, a full-size color swatch, and a one-click copy button — ready to paste into CSS, Tailwind, or any design tool.

Generating your first color…

Values will appear here.

Everything you need to know about HEX color codes

What is a HEX color code?

A HEX color code is a six-character string — like #3A8FC2 — that encodes a color as three pairs of hexadecimal digits. The first pair represents the red channel, the second the green channel, and the third the blue channel. Each pair runs from 00 (zero intensity) to FF (full intensity), giving 256 levels per channel and 256 × 256 × 256 = 16,777,216 total possible colors. The hash symbol at the front is a CSS convention rather than part of the number itself; HTML, SVG, and most design apps accept the code with or without it, though CSS properties require the hash.

The hex numeral system uses sixteen symbols — digits 0–9 plus letters A–F — so two hex digits can represent any number from 0 to 255 in just two characters. That compactness is why HEX became the web standard: a full color fits in a short string that is easy to copy, easy to type into a style sheet, and easy to spot in a diff. Compare it to the equivalent CSS RGB notation, rgb(58, 143, 194), which is longer and requires remembering the function syntax. HEX wins on brevity almost every time.

How to read and decode a HEX color

Split #3A8FC2 into its three pairs: 3A, 8F, C2. Convert each from hex to decimal. In hex, 3A is (3 × 16) + 10 = 58; 8F is (8 × 16) + 15 = 143; C2 is (12 × 16) + 2 = 194. So the color is rgb(58, 143, 194) — a medium sky blue. You do not need to do this arithmetic by hand in practice, but knowing the mapping helps you read codes intuitively. Codes close to #000000 are dark; codes close to #FFFFFF are light. Codes where two channels dominate and the third is low produce saturated hues: #FF0080 is a vivid pink because red is maxed, green is zero, and blue is at half.

There is also a three-character shorthand: #3AC expands to #33AACC by doubling each digit. Shorthand only works when both digits in each pair are identical. It is common in CSS source code to save characters, though most generators output the full six-digit form to avoid ambiguity.

HEX vs RGB vs HSL — which format should you use?

All three formats describe the same 16.7 million colors; the difference is which representation is easiest to work with at a given moment. The table below summarizes the trade-offs:

FormatExampleBest forWeakness
HEX#3A8FC2CSS, HTML, design tokensHard to adjust mentally
RGBrgb(58,143,194)Canvas, WebGL, animationsVerbose in source
HSLhsl(204,54%,49%)Tint ramps, theme systemsLess tool support

HSL (hue, saturation, lightness) is the most human-readable format for creating color ramps. If you want five shades of the same blue running from near-white to near-navy, you keep the hue constant at 204° and step the lightness from 90% down to 20%. Doing the same manipulation in HEX requires mental arithmetic across all three pairs at once. Most modern CSS supports all three notations, so choose the format that matches your task: HEX for copying a specific color, HSL for building a system around it.

How to convert HEX to RGB and HSL step by step

HEX to RGB: Parse each two-character pair as a base-16 integer. For #B245E7: red = 0xB2 = 178, green = 0x45 = 69, blue = 0xE7 = 231. Result: rgb(178, 69, 231).

RGB to HSL takes a few more steps. Normalize each channel to the 0–1 range by dividing by 255. Find the maximum (M) and minimum (m) values. Lightness L = (M + m) / 2. Saturation S = (M − m) / (1 − |2L − 1|) when L ≠ 0 and L ≠ 1. Hue H depends on which channel is the max:

  • Red is max: H = 60 × ((G − B) / (M − m) mod 6)
  • Green is max: H = 60 × ((B − R) / (M − m) + 2)
  • Blue is max: H = 60 × ((R − G) / (M − m) + 4)

Multiply L and S by 100 to get percentages. For rgb(178, 69, 231): normalized = (0.698, 0.271, 0.906), M = 0.906, m = 0.271, L = 0.588, S = 0.756, H = 286°. Result: hsl(286, 76%, 59%) — a medium violet.

Using a random HEX generator in real design work

A random HEX generator is most useful when you need a color fast and do not yet have a palette constraint. Common practical applications:

  • Placeholder UI: Drop a random HEX into a CSS custom property like --accent while the real brand color is being finalized. The randomness makes the placeholder visually obvious in screenshots and handoffs.
  • Chart and map categories: Need five distinguishable series colors quickly? Generate five codes, check that adjacent pairs have sufficient hue contrast, done. Refine later.
  • Stress testing layouts: Paste an unexpected HEX into a component that normally uses a soft brand color and confirm the layout does not break on a vivid magenta or near-black.
  • Creative prompts: Commit to a random color for 30 minutes of design work. The constraint forces solutions you would never reach deliberating over a color picker.
  • Tailwind arbitrary values: Copy a HEX code straight into a Tailwind utility — bg-[#4C9AC4] — without opening any palette documentation.

Accessibility: what to check before shipping a HEX color

WCAG 2.1 Level AA requires a contrast ratio of at least 4.5:1 between normal body text and its background, and 3:1 for large text (18 pt or 14 pt bold) and UI components. Contrast ratio is calculated from relative luminance: a color's contribution to brightness on a linear scale from 0 (black) to 1 (white). The formula for relative luminance from RGB values is:

L = 0.2126 × R_lin + 0.7152 × G_lin + 0.0722 × B_lin, where each channel is linearized: if the 0–1 value v ≤ 0.04045, v_lin = v / 12.92; otherwise v_lin = ((v + 0.055) / 1.055)^2.4. The contrast ratio between two luminances L1 (lighter) and L2 (darker) is (L1 + 0.05) / (L2 + 0.05).

In practice: if the HEX you generated will sit under text, calculate its contrast against black and white before shipping. A color in the mid-gray range — roughly #606060 to #A0A0A0 — fails both, which means it needs dark text on a light background adjustment before it can be used accessibly for body copy. Vivid saturated colors often pass against white if they are dark enough, or against black if they are light enough. Run the check; do not guess.

Building a full palette from one HEX code

A single random HEX code is a starting point, not a finished palette. Here is a repeatable workflow to build a usable five-step tint ramp from any HEX:

  1. Convert the HEX to HSL. This gives you the base hue (H) and saturation (S) as stable values.
  2. Create five steps at lightness values 20%, 35%, 50%, 65%, 80%, keeping H and S constant. These are your dark, mid-dark, base, mid-light, and light variants.
  3. Check each step's contrast ratio against your text colors. Adjust lightness up or down by 5% increments until all text-bearing steps clear the 4.5:1 threshold.
  4. Convert the final HSL values back to HEX for the design token file.
  5. Find the complementary hue at H + 180° for an accent color, and run the same lightness steps if needed.

This process takes under ten minutes with any basic calculator and turns a single random HEX into a production-ready scale. The generator on this page gives you the seed; the process above gives you the system.

HEX color codes in CSS — syntax reference

CSS accepts HEX colors in three places: property values, custom properties, and the color() function. The most common patterns:

UsageExample
Direct property valuecolor: #3A8FC2;
CSS custom property--accent: #3A8FC2;
Short notationbackground: #3AC;
With alpha (8-digit)border: 1px solid #3A8FC280;
Tailwind arbitrarybg-[#3A8FC2]

The eight-digit HEX form adds an alpha channel as the final pair:#RRGGBBAA. A value of FF is fully opaque, 80 is 50% transparent (128 ÷ 255 ≈ 0.50), and 00 is invisible. This is equivalent to the fourth argument in rgba() but expressed in hex, which keeps all color information in a single uniform token.

HEX color psychology and choosing colors that communicate intent

Every HEX code you pick carries psychological weight that shapes how users feel when they land on your page or open your app. Color psychology is not mysticism — it is a body of research spanning decades of consumer studies, and understanding it helps you make deliberate choices rather than random ones. The hue component (the H in HSL) is the strongest signal. Reds and oranges in the 0–40° range read as urgency, energy, and appetite — which is why fast-food brands and call-to-action buttons lean heavily on #FF3B30 and #FF6A00. Blues from 190° to 240° communicate trust, reliability, and calm — the reason nearly every bank, healthcare provider, and enterprise SaaS company anchors its palette somewhere between #0047AB and #5BC0EB.

Saturation and lightness modulate that emotional signal. A fully saturated red at #FF0000 shouts; the same hue desaturated to #C97B7B whispers. High-lightness, low-saturation tints like #EAF4FB feel airy and minimal — popular in dashboard backgrounds where users spend hours staring at the screen. Low-lightness, high-saturation shades like #1A237E feel authoritative and premium. When you generate a random HEX and decide whether to keep it, run a quick mental check: what does this hue say about the content it will surround? A vivid lime at roughly #76FF03 is perfect for a gaming stat badge but would undermine a medical portal's credibility instantly. Context always beats aesthetics in production design decisions.

Cultural context adds another layer. White backgrounds read as clean and modern in most Western markets but carry associations with mourning in parts of East Asia. Red triggers luck and celebration in Chinese cultural contexts but aggression in some Northern European contexts. If your product ships globally, audit your HEX palette against at least three regional cultural norms before locking a brand color. The effort is small — roughly 30 minutes of desk research — and prevents embarrassing missteps in important markets. The generator on this page can give you a starting point; cultural research turns that starting point into a defensible choice.

Programmatic HEX color generation — techniques and use cases

Sometimes you need more than a single random color — you need a whole family of colors generated on the fly. Programmatic HEX generation covers a wide range of tasks: producing unique avatar backgrounds for thousands of user accounts, generating chart series colors that never clash, or building a procedural world where every biome gets a distinct but coherent color scheme. The core technique is always the same: choose a mathematical model that produces perceptually uniform output, then convert to HEX for display.

The most reliable approach for distinguishable series colors is the golden-angle method. The golden angle is approximately 137.508°. If you start at any hue and add 137.508° for each successive color, you cycle through the hue wheel in a pattern that maximizes the minimum distance between any two chosen hues. In JavaScript: hue = (index * 137.508) % 360. Set saturation to 65% and lightness to 55% for decent contrast against white backgrounds across all hues. Convert each HSL triplet to HEX using the standard formula and you have an infinite series of distinguishable colors with zero collisions, no manual tuning, and consistent vibrancy. For 10 series you get hues roughly at 0°, 138°, 275°, 53°, 191°, 329°, 106°, 244°, 22°, 160° — well spread across the wheel.

For avatar backgrounds keyed to a user's name, hash the name string into an integer (a 32-bit FNV-1a hash works well and is 10 lines of code), then map the integer to a hue with hue = hash % 360. Two users with similar names will usually get different hues, and the same user always gets the same color — deterministic without storing anything. You then fix saturation at around 50% and lightness at 60% so the background is always light enough to carry dark text at a contrast ratio above 4.5:1. This pattern scales to millions of users with zero database overhead for color storage: the HEX is computed at render time in under a microsecond.

For procedural world-building or game maps, consider LCH (Lightness-Chroma-Hue) color space rather than HSL. LCH is perceptually uniform — equal numerical steps in chroma or lightness produce equal-looking visual steps across all hues, which HSL does not guarantee. Modern browsers support lch() in CSS natively as of 2023, but if you need wide compatibility, convert LCH to sRGB and then to HEX using a well-tested library. The colordice toolset focuses on the sRGB-HEX pipeline that covers 99% of web and app use cases.

HEX colors and accessibility contrast ratios

Picking a random HEX color is easy; picking one that passes accessibility contrast requirements is harder. WCAG 2.1 defines two thresholds for text on a background: AA requires a contrast ratio of at least 4.5:1 for normal-sized text (below 18pt regular or 14pt bold), and AAA requires 7:1. The contrast ratio formula uses relative luminance — a value derived from the linearised R, G, and B channels of each color. For a HEX like #3A8FC2 the steps are: divide each 8-bit channel by 255, apply the linearisation curve (c / 12.92 for values below 0.04045, otherwise((c + 0.055) / 1.055) ^ 2.4), then combine as0.2126·R + 0.7152·G + 0.0722·B. That gives the relative luminance L. The ratio between two luminances L1 and L2 (L1 ≥ L2) is(L1 + 0.05) / (L2 + 0.05).

In practice, mid-range HEX values — those with all three channels between about #55 and #AA — tend to fail contrast against both pure white (#FFFFFF) and pure black (#000000). A random generator that simply picks three uniform random bytes will produce accessible text pairings only about 30% of the time. To guarantee a usable palette, constrain randomness: generate the background in the dark range (L < 0.18) and use white text, or generate it in the light range (L > 0.50) and use black text. For example, clamping HSL lightness to either 15–35% or 65–85% before converting to HEX yields backgrounds that almost always pass AA at 4.5:1 against their optimal text color without any post-hoc contrast check.

Real-world reference: Material Design 3 enforces a minimum contrast of 4.5:1 for body copy and 3:1 for large headings. Apple's Human Interface Guidelines recommend 7:1 for critical UI labels on iOS. The EU Accessibility Act (EAA), enforceable from June 2025, requires AA compliance for digital products sold in the EU. If you are generating color palettes for a product, run every foreground-background HEX pair through a contrast check before shipping — tools like the WebAIM Contrast Checker accept HEX input directly, and most design tokens pipelines can automate this as a CI step.

Using random HEX colors in a design system

Design systems live and die by consistency, which seems at odds with random color generation — but the two work together well once you separate thepalette definition step from the token assignment step. The typical workflow is: generate a set of candidate HEX values, evaluate them against the system's constraints (contrast ratios, brand proximity, hue diversity), promote the survivors to named design tokens, and then reference only tokens in component code. The random generator is a discovery tool, not a final authority.

Concretely, a mid-size design system might define a palette of 10–14 named semantic colors: primary, primary-subtle,surface, surface-raised, on-surface,success, warning, danger, and a few neutrals. When refreshing or extending the palette, designers use a random HEX generator to explore the space quickly — generating 20–30 candidates in a hue range, discarding those that fail contrast or clash with locked brand colors, and picking the best survivors. Figma's variables panel, Storybook's theme addon, and Tailwind's theme.extend.colors map all accept HEX values directly, so moving from the generator to the system is a one-paste operation.

For dark-mode support, a useful rule of thumb is the 15/85 split: your light-mode surface HEX should have an HSL lightness between 92% and 98%, and your dark-mode surface between 8% and 14%. This narrow band keeps the surface neutral enough that any accent color (generated or chosen) reads clearly against it in both modes. If you generate a random accent HEX and its HSL lightness falls between 40% and 65%, adjust it: push it to 55–70% for dark mode and 35–50% for light mode. The hue and saturation stay the same; only the lightness shifts to maintain contrast. Locking this adjustment in your token pipeline means one generated HEX seed can automatically produce a complete, accessible dark/light token pair without manual tuning.

Version control is the final piece. Design tokens stored as HEX strings in a JSON or YAML file are diff-friendly: a one-line change from #4A90D9to #3A8FC2 is immediately visible in a pull request, reviewable, and revertable. Teams at companies like Shopify (Polaris), IBM (Carbon), and Atlassian (Atlassian Design System) all store their color tokens as HEX in version-controlled files, which makes color changes as auditable as code changes. If your team is not yet doing this, moving your colors from design-tool variables to a committed JSON file is one of the highest-leverage steps you can take toward a maintainable design system.

Frequently Asked Questions

What does a HEX color code look like?+

A HEX color code is a hash symbol followed by six hexadecimal characters, such as #FF5733 or #0047AB. Each pair of characters represents the red, green, and blue channel respectively, with values from 00 (minimum) to FF (maximum). Some tools also support a three-character shorthand like #F53 when both digits in each pair are the same.

How do I convert a HEX code to RGB?+

Split the six-digit code into three two-character pairs and convert each from base 16 to base 10. For #FF5733: FF = 255 (red), 57 = 87 (green), 33 = 51 (blue), giving rgb(255, 87, 51). Most code editors, design tools, and browser DevTools do this conversion automatically — paste the HEX value and switch the color format selector.

Is a random HEX color safe to use for text?+

Not necessarily. A random color from the full 16.7 million color space has roughly a 60–70% chance of falling in the mid-lightness range where it fails WCAG 2.1 contrast requirements against both black and white text. Before using a randomly generated HEX under text, calculate its relative luminance and check the contrast ratio against your text color. A ratio of 4.5:1 or higher passes for normal body text.

Can I use HEX colors in Tailwind CSS?+

Yes. Tailwind supports arbitrary values with square bracket notation: bg-[#3A8FC2], text-[#FF5733], border-[#0047AB]. You can paste any HEX code directly between the brackets without adding it to your config. For colors you use repeatedly across a project, add them to the theme.extend.colors section of tailwind.config.js and give them a semantic name.

What is the difference between HSL and HEX for building color palettes?+

HEX is the most compact and paste-ready format, but it gives you no intuitive control over how a color changes when you lighten or darken it. HSL (hue, saturation, lightness) separates the three perceptual dimensions, making it straightforward to build a tint ramp by stepping the lightness value while keeping the hue and saturation constant. The typical workflow is: generate or choose a HEX code, convert it to HSL, build your ramp in HSL, then convert the final values back to HEX for your token file.

How many colors can a HEX code represent, and is that enough for modern screens?+

A standard 6-digit HEX code represents exactly 16,777,216 colors — 256 levels per channel across red, green, and blue. Modern wide-gamut displays (P3, Rec. 2020) can render colors outside this sRGB range, but CSS HEX notation is locked to sRGB. For wide-gamut use cases you would use the CSS color() function with a display-p3 profile instead of HEX. For the vast majority of web and app work, 16.7 million sRGB colors remains more than sufficient.

What is the fastest way to generate a random HEX color in JavaScript?+

The one-liner is: '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, '0'). It generates a random integer from 0 to 16777215, converts it to a 6-character hex string (padding with leading zeros if needed), and prepends the hash symbol. For cryptographically random colors use crypto.getRandomValues() instead of Math.random() — replace the Math.floor line with: '#' + [...crypto.getRandomValues(new Uint8Array(3))].map(b => b.toString(16).padStart(2,'0')).join('').