FormatterHQ

RGB to HEX Converter

Turn red, green and blue values from 0 to 255 into six-digit hex color codes for CSS, HTML and brand guides. Works with rgb(), bare numbers and percentages.

Loading tool…
Ready.

How to use the RGB to HEX Converter

  1. Type or paste an RGB color into the RGB field as rgb(220, 38, 127), rgb(220 38 127) or just 220, 38, 127.
  2. The HEX field updates as you type and shows an uppercase #RRGGBB code.
  3. Copy the hex code with the button next to the field and paste it into your CSS, design tool or style guide.

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.

Frequently asked questions

How do I convert RGB to hex by hand?
Convert each channel separately: divide by 16, write the quotient and remainder as hex digits (A–F for 10–15), and pad to two digits. Then join the three pairs after a # sign, red first.
Why does my result have 8 digits?
The color has an alpha below 1: the Alpha (opacity) slider is under 100%, or you entered an rgba() value with a fourth number. The last two digits encode that opacity. Set the slider back to 100% for a six-digit code.
What is rgb(0, 0, 0) in hex?
It is #000000, black, which can also be written in shorthand as #000. White, rgb(255, 255, 255), is #FFFFFF or #FFF.
Should hex color codes be uppercase or lowercase?
Either works; browsers and design tools treat them identically. Pick one style and use it consistently so codes are easy to search for in your project.
What happens if a value is above 255?
It is clamped to 255, and negative values are clamped to 0. A channel can only hold one byte, so values outside 0–255 usually mean the numbers were copied from a different scale, such as 0–1 floats.

Last updated October 3, 2026

Related tools

HEX to RGB Converter Convert HEX colour values to RGB instantly, with a live colour swatch preview, colour picker and copy buttons. CMYK to HEX Converter Convert CMYK colour values to HEX instantly, with a live colour swatch preview, colour picker and copy buttons. CMYK to RGB Converter Convert CMYK colour values to RGB instantly, with a live colour swatch preview, colour picker and copy buttons. Colortone to HEX Converter Convert a named colour tone (e.g. steelblue, tomato) or any colour to HEX, with its tints, shades and tones. Colortone to RGB Converter Convert a named colour tone (e.g. steelblue, tomato) or any colour to RGB, with its tints, shades and tones. HEX to CMYK Converter Convert HEX colour values to CMYK instantly, with a live colour swatch preview, colour picker and copy buttons. RGB to HSL Converter Convert RGB colour values to HSL instantly, with a live colour swatch preview, colour picker and copy buttons. RGB to HSV Converter Convert RGB colour values to HSV instantly, with a live colour swatch preview, colour picker and copy buttons. RGB to RGBA Converter Convert RGB colour values to RGBA instantly, with a live colour swatch preview, colour picker and copy buttons. RGBA to CMYK Converter Convert RGBA colour values to CMYK instantly, with a live colour swatch preview, colour picker and copy buttons. RGBA to HEX Converter Convert RGBA colour values to HEX instantly, with a live colour swatch preview, colour picker and copy buttons. RGBA to HSL Converter Convert RGBA colour values to HSL instantly, with a live colour swatch preview, colour picker and copy buttons.