Convert RGB to HEX color codes
The RGB to HEX converter turns red, green and blue values from 0 to 255 into a compact six-digit hex code for CSS, HTML, brand guides and design tools. It accepts rgb(220, 38, 127), the space-separated rgb(220 38 127) syntax, or three bare numbers such as 220, 38, 127.
How each channel becomes two hex digits
Every channel is converted from decimal to base 16 on its own and padded to two digits. Divide the value by 16: the whole-number quotient is the first digit and the remainder is the second, with 10–15 written as A–F.
rgb(220, 38, 127)
220 ÷ 16 = 13 remainder 12 → D C → DC
38 ÷ 16 = 2 remainder 6 → 2 6 → 26
127 ÷ 16 = 7 remainder 15 → 7 F → 7F
→ #DC267F
Padding matters. Fifteen is 0F, not F, so rgb(15, 118, 110) is #0F766E. Drop that zero and you get a five-digit code that no browser accepts. The decimal to hex converter applies the same division method to larger numbers.
What the tool accepts
- Percentages.
rgb(100%, 50%, 0%)is scaled to 0–255 first. Half of 255 is 127.5, which rounds to 128, so the result is#FF8000. - Decimals. Fractional channels are rounded to the nearest integer:
rgb(12.6, 99.5, 200.4)becomes#0D64C8. - Out-of-range values are clamped, so anything above 255 becomes 255 and negative numbers become 0.
- Alpha comes from the Alpha (opacity) slider or from an
rgba()value with a fourth number. If it is below 1, two more digits are appended (the RGBA to HEX page explains that math); otherwise you get a plain six-digit code.
Output is always uppercase and always six digits for opaque colors. Hex is case-insensitive, so #DC267F and #dc267f are the same color; lowercase is just a common style convention in stylesheets.
Shortening a hex code
When each pair consists of two identical digits, CSS lets you write only three: #AABBCC can become #ABC and #FF6600 can become #F60. The tool always prints the full form, which is valid everywhere. Shorten it yourself only when all three pairs repeat; #DC267F cannot be shortened.
Why hex codes are everywhere
Hex codes are short, copy cleanly as a single token and are understood by every browser, design tool and brand guideline. That makes them a natural choice for design tokens and style guides. Their weakness is readability: nobody can tell at a glance that #DC267F is a raspberry pink, so keep an RGB or HSL version alongside when you document a palette.
Need to decode a code instead? The HEX to RGB converter takes you back.
Convert RGB to HEX in Python
In Python, RGB to HEX needs no import: an f-string with the 02X format code writes each channel as two uppercase hex digits (02x gives lowercase). The width is the part people leave out. hex(8) is 0x8, so joining unpadded digits turns (0, 8, 255) into the four-character #08ff instead of #0008FF. Channels must also be integers from 0 to 255; values from a library that works in 0–1 floats have to be multiplied by 255 and rounded first.
def rgb_to_hex(r, g, b):
for channel in (r, g, b):
if not 0 <= channel <= 255:
raise ValueError(f"channel out of range: {channel}")
return f"#{r:02X}{g:02X}{b:02X}"
print(rgb_to_hex(255, 99, 71)) # #FF6347
print(rgb_to_hex(70, 130, 180)) # #4682B4
print(rgb_to_hex(0, 8, 255)) # #0008FF
# Without the 02 width the zeros vanish
print("#" + "".join(hex(c)[2:] for c in (0, 8, 255))) # #08ff
# HEX back to RGB
code = "#4682B4".lstrip("#")
print(tuple(int(code[i:i + 2], 16) for i in (0, 2, 4))) # (70, 130, 180)
Use the converter above to check a result, or HEX to RGB for the reverse direction shown in the last lines of the snippet.