RGB to HEX Converter

Optional shortcut — sets the fields below. No transparency support (a browser limitation of this control), so use the HEX/RGB/HSL fields directly for a translucent color.

#RGB, #RGBA, #RRGGBB, or #RRGGBBAA — the # is optional

rgb(R, G, B) or rgba(R, G, B, A) — R/G/B 0-255, A 0-1

hsl(H, S%, L%) or hsla(H, S%, L%, A) — H 0-360, A 0-1

Going from RGB to hex is the direction most people hit after picking a color visually rather than typing one in: a design tool’s eyedropper or slider hands you three numbers — red, green, blue, each 0-255 — and the next step is almost always turning that into the compact hex string CSS, config files, and style guides actually expect. rgb(220, 38, 38) and #dc2626 are the exact same color; hex is just a shorter way to write the same three numbers.

The arithmetic runs in the opposite direction from parsing hex: instead of splitting a string into pairs and reading each as a base-16 number, each 0-255 channel gets converted to a 2-digit base-16 (hexadecimal) pair and the three pairs are joined together. 220 in hexadecimal is dc; 38 is 26; concatenating dc + 26 + 26 gives #dc2626. Every possible 0-255 value has exactly one 2-digit hex representation, so unlike converting through HSL (where hue/saturation/lightness get rounded to whole numbers for display), this conversion loses no precision in either direction.

One detail worth knowing when you’re staring at the result: not every hex code can be shortened to the familiar 3-digit form. That shorthand only exists when each channel’s two digits happen to be identical — #33aaff can shorten to #3af because 3=3, a=a, f=f, but a color like #dc2626 can’t, because dc in the first pair. Most real-world colors picked from a design tool land on non-repeating digit pairs and simply don’t have a shorter form — that’s not a bug in a particular converter, it’s just how often the coincidence of matching digits actually happens (1 in 16 per channel).

Converting RGB to hex programmatically

JavaScript — the same technique browser DevTools and design tools use internally to display a picked color as hex:

function rgbToHex(r, g, b) {
  const toHex = (n) => Math.max(0, Math.min(255, n)).toString(16).padStart(2, "0");
  return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
}

console.log(rgbToHex(220, 38, 38));
// "#dc2626"

Python, using an f-string’s built-in hex formatting instead of a manual toString:

def rgb_to_hex(r, g, b):
    r, g, b = (max(0, min(255, v)) for v in (r, g, b))
    return f"#{r:02x}{g:02x}{b:02x}"

print(rgb_to_hex(220, 38, 38))
# "#dc2626"

Frequently asked questions

Why do design tools show RGB, but CSS and style guides usually store hex?
Color pickers in tools like Figma, Photoshop, and browser DevTools often default to showing RGB sliders (0-255 per channel) because that maps directly onto how a screen's pixels work — three separate light-intensity values. CSS and most codebases store hex instead because it's shorter to write and paste (7 characters vs. the ~18 of rgb(220, 38, 38)), and it's the format nearly every design-token spec, CSS variable, and style guide already expects.
Does every RGB value have an exact hex equivalent?
Yes, always — the conversion is exact with no rounding involved, unlike RGB-to-HSL, where hue/saturation/lightness are typically displayed rounded to whole numbers. Each 0-255 channel maps to exactly one 2-digit hex pair and back, with nothing lost in either direction.
Can this specific color use the 3-digit hex shorthand?
No — rgb(220, 38, 38) converts to #dc2626, and the 3-digit shorthand only works when each channel's two hex digits are identical to each other (like #33aaff shortening to #3af). Here, dc has two different digits (d and c), so this color has no shorter form — it needs the full 6 digits.
What happens if my R, G, or B value is outside 0-255?
It gets clamped to the valid range before converting — a channel of 300 is treated as 255, and a negative value is treated as 0 — rather than producing an error or an invalid hex digit pair.
Is the color I enter sent to a server?
No. The conversion runs locally in your browser using plain arithmetic — nothing is uploaded, logged, or stored.

Related Color Converter pages

Looking for a different pair or algorithm? Here are the other specific versions, plus the general tool for everything else: