How to Convert Hex Color to RGB

#3498db = rgb(52, 152, 219). Formula: R=0x34=52, G=0x98=152, B=0xdb=219. Split the 6-digit hex code into three pairs of two characters, then convert each pair from hexadecimal (base-16) to decimal (base-10). Each value ranges from 0 to 255.

Step by Step

#3498db
 ││││││
 34│98│db
 │ │  │
 R G  B

R: 34 hex = (3 × 16) + 4 = 52
G: 98 hex = (9 × 16) + 8 = 152
B: db hex = (13 × 16) + 11 = 219

Result: rgb(52, 152, 219)
#3498db = rgb(52, 152, 219)

Code Examples

JavaScript — Hex to RGB

function hexToRgb(hex) {
  hex = hex.replace('#', '');
  return {
    r: parseInt(hex.substring(0, 2), 16),
    g: parseInt(hex.substring(2, 4), 16),
    b: parseInt(hex.substring(4, 6), 16)
  };
}
hexToRgb('#3498db');
// { r: 52, g: 152, b: 219 }

JavaScript — RGB to Hex

function rgbToHex(r, g, b) {
  return '#' + [r, g, b]
    .map(v => v.toString(16).padStart(2, '0'))
    .join('');
}
rgbToHex(52, 152, 219);
// "#3498db"

Python

# Hex to RGB
def hex_to_rgb(hex_color):
    hex_color = hex_color.lstrip('#')
    return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))

hex_to_rgb('#3498db')  # (52, 152, 219)

# RGB to Hex
def rgb_to_hex(r, g, b):
    return f'#{r:02x}{g:02x}{b:02x}'

rgb_to_hex(52, 152, 219)  # '#3498db'

Common Colors

ColorHexRGB
Red#ff0000rgb(255, 0, 0)
Green#00ff00rgb(0, 255, 0)
Blue#0000ffrgb(0, 0, 255)
Steel Blue#3498dbrgb(52, 152, 219)

Try It Yourself

Use our Hex to RGB Converter to convert colors instantly.

Frequently Asked Questions

How do I convert RGB back to hex?

Convert each R, G, B value from decimal to a 2-digit hex string, then concatenate with a # prefix. In JavaScript: '#' + [r, g, b].map(v => v.toString(16).padStart(2, '0')).join('').

What about 3-digit hex codes like #fff?

A 3-digit hex is shorthand: each digit is doubled. #fff = #ffffff = rgb(255, 255, 255). #39d = #3399dd = rgb(51, 153, 221).

Built by Michael Lip. 100% client-side — no data leaves your browser.