Hex to Decimal

Hex to RGB converter

Type a hex colour code to see its red, green and blue values. Each pair of hex digits becomes one number from 0 to 255. The sections below cover the method, shorthand and alpha codes, common colours, HSL and HSV, CSS syntax, code in eight languages and the reverse RGB-to-hex method.

Output

Channels pair × 16 + pair = 0 to 255

CSS
HSL
HSV / HSB
Whole number

What is a hex colour code?

A hex colour code is a six-digit hexadecimal number that represents a colour on screen. The six digits form three pairs, and each pair sets one channel: red, green and blue (RGB). The code starts with a # sign, so #FF5733 means red FF, green 57 and blue 33.

Each pair is one byte, with values from 00 (none of that colour) to FF (full intensity). The three bytes together define 256 × 256 × 256 = 16,777,216 possible colours, which matches the 24-bit colour depth of screens and image formats.

Hex colour codes have been part of CSS since its first version, and every modern browser, design tool and graphics library reads them. Move the sliders to mix a colour and watch the code change.

How to convert hex to RGB

Step-by-step method

  1. Remove the # prefix.
  2. Split the remaining digits into three pairs: the first pair is red, the second is green and the third is blue.
  3. Convert each pair from hexadecimal to decimal. Multiply the left digit by 16 and add the right digit.
  4. Write the three decimal values as rgb(R, G, B).

Conversion examples

Example 1: #2440C8

ChannelHex pairCalculationDecimal
Red242 × 16 + 436
Green404 × 16 + 064
BlueC812 × 16 + 8200

Result: rgb(36, 64, 200), a medium blue with a strong blue channel and low red.

Example 2: #FF5733

ChannelHex pairCalculationDecimal
RedFF15 × 16 + 15255
Green575 × 16 + 787
Blue333 × 16 + 351

Result: rgb(255, 87, 51), a vivid red-orange at full red intensity.

Example 3: #1E90FF (Dodger Blue)

ChannelHex pairCalculationDecimal
Red1E1 × 16 + 1430
Green909 × 16 + 0144
BlueFF15 × 16 + 15255

Result: rgb(30, 144, 255).

The formula

For any hex pair XY, the decimal value is decimal = X × 16 + Y. X and Y are the decimal values of the two hex digits (A = 10, B = 11, C = 12, D = 13, E = 14, F = 15). The converter above shows this working for every channel.

3-digit shorthand hex

CSS allows a 3-digit shorthand where each digit is doubled to form the full 6-digit code. #F0A expands to #FF00AA because F becomes FF, 0 becomes 00 and A becomes AA.

ShorthandExpands toRGB
#F00#FF0000rgb(255, 0, 0), red
#0F0#00FF00rgb(0, 255, 0), green
#00F#0000FFrgb(0, 0, 255), blue
#FFF#FFFFFFrgb(255, 255, 255), white
#000#000000rgb(0, 0, 0), black

The shorthand saves characters in stylesheets, but only works when both digits in every pair match. #2440C8 has no shorthand because 24, 40 and C8 are all mixed pairs. Test your own code in the visual.

8-digit hex and alpha transparency

An 8-digit hex code adds a fourth pair after the blue channel. This fourth pair is the alpha channel, which controls opacity. 00 is fully transparent, FF is fully opaque, and the values between set partial transparency.

#2440C880 has red 24 (36), green 40 (64), blue C8 (200) and alpha 80 (128). Since 128 ÷ 255 ≈ 0.50, the CSS function form is rgba(36, 64, 200, 0.50), where alpha is a fraction from 0 to 1. All current browsers accept 8-digit hex codes in stylesheets.

A 4-digit shorthand works the same way: #F00A expands to #FF0000AA, which is red at about 67% opacity.

Common alpha values

HexDecimalOpacity
FF255100% (solid)
BF19175%
8012850%
406425%
0000% (invisible)

RGBA and ARGB byte order

Both RGBA and ARGB carry the same four channels, but the alpha byte sits in a different place.

CSS uses RGBA order: #RRGGBBAA. The alpha pair comes last, so #2440C880 is rgba(36, 64, 200, 0.50). Android and many graphics APIs use ARGB order: #AARRGGBB. The alpha pair comes first, so the same colour becomes #802440C8.

FormatHex codeByte order
CSS (RGBA)#2440C880Red, Green, Blue, Alpha
Android (ARGB)#802440C8Alpha, Red, Green, Blue

Switch between RGB, RGBA and ARGB output with the buttons in the converter above, or flip the order below.

Common hex colour codes and RGB values

Tap a swatch to read its code.

HexRGBColour
#000000rgb(0, 0, 0)Black
#FFFFFFrgb(255, 255, 255)White
#FF0000rgb(255, 0, 0)Red
#00FF00rgb(0, 255, 0)Lime (green)
#0000FFrgb(0, 0, 255)Blue
#FFFF00rgb(255, 255, 0)Yellow
#00FFFFrgb(0, 255, 255)Cyan
#FF00FFrgb(255, 0, 255)Magenta
HexRGBColour
#808080rgb(128, 128, 128)Grey
#FFA500rgb(255, 165, 0)Orange
#800080rgb(128, 0, 128)Purple
#008080rgb(0, 128, 128)Teal
#FFC0CBrgb(255, 192, 203)Pink
#A52A2Argb(165, 42, 42)Brown
#1E90FFrgb(30, 144, 255)Dodger Blue
#FFD700rgb(255, 215, 0)Gold

Greys have three equal channels. The primary colours (red, green, blue) each max out one channel and zero the other two. The secondary colours (yellow, cyan, magenta) max out two channels.

Hex to HSL

HSL (hue, saturation, lightness) describes the same colour as a position on the colour wheel, a saturation percentage and a lightness percentage. Convert hex to RGB first, then derive HSL from the three channel values.

HSL conversion steps

  1. Scale each RGB channel to the range 0–1 by dividing by 255.
  2. Find the maximum channel (Cmax) and the minimum channel (Cmin). Calculate delta = Cmax − Cmin.
  3. Lightness = (Cmax + Cmin) / 2.
  4. Saturation = delta / (1 − |2 × Lightness − 1|) when delta > 0, or 0 when delta = 0.
  5. Hue depends on the highest channel. If red: 60 × ((G − B) / delta mod 6). If green: 60 × ((B − R) / delta + 2). If blue: 60 × ((R − G) / delta + 4).

HSL example: #2440C8

RGB is 36, 64, 200, which scales to R = 0.141, G = 0.251 and B = 0.784. Cmax = 0.784 (blue), Cmin = 0.141 and delta = 0.643.

  • Lightness = (0.784 + 0.141) / 2 = 46%.
  • Saturation = 0.643 / (1 − |2 × 0.463 − 1|) = 69%.
  • Blue is highest, so hue = 60 × ((0.141 − 0.251) / 0.643 + 4) = 230°.

Result: hsl(230, 69%, 46%). HSL is handy in CSS because lightness and saturation are easy to adjust: raise lightness for a tint, lower it for a shade.

Hex to HSV (HSB)

HSV (hue, saturation, value) is the colour model that design tools such as Figma and Adobe Photoshop label as HSB (hue, saturation, brightness). Hue is the same as in HSL, but saturation and the third component differ.

  • Value = Cmax (the brightest channel, as a percentage of 255).
  • Saturation = delta / Cmax when Cmax > 0, or 0 when Cmax = 0.

For #2440C8: value = 0.784, so 78%. Saturation = 0.643 / 0.784, so 82%. Hue = 230°.

Format#2440C8Built from
RGBrgb(36, 64, 200)Each hex pair converted to decimal
HSLhsl(230, 69%, 46%)Lightness is the average of the brightest and dimmest channels
HSV / HSBhsv(230, 82%, 78%)Value is the brightest channel as a percentage

HSL and HSV saturation usually differ for the same colour, because HSV measures it against the brightest channel while HSL measures it against lightness.

Hex to RGB in CSS and HTML

CSS accepts hex colour codes wherever a colour value is allowed: color, background-color, border-color, box-shadow and more. Pick a syntax to see the same colour written each way.

/* 6-digit hex */
.card { background-color: #2440C8; }

/* 3-digit shorthand */
.error { color: #F00; }

/* 8-digit hex with alpha */
.overlay { background-color: #2440C880; }

/* Equivalent rgb() and rgba() */
.card { background-color: rgb(36, 64, 200); }
.overlay { background-color: rgba(36, 64, 200, 0.5); }

Modern CSS (the Level 4 Color specification) also accepts the space-separated form rgb(36 64 200 / 0.5), which replaces the older rgba() function. Both forms work in all current browsers.

Old HTML presentation attributes such as bgcolor also took hex codes, but they are obsolete. Use CSS instead.

Hex to RGB in programming

Every language converts a hex pair to a decimal channel with its built-in hex parser. Pick a language to see the call and its result for your colour.

Python

h = 'FF5733'
r, g, b = int(h[0:2], 16), int(h[2:4], 16), int(h[4:6], 16)
# r=255, g=87, b=51

# Or in one line
rgb = tuple(int(h[i:i+2], 16) for i in (0, 2, 4))

Python’s int() with base 16 converts each hex pair. For the full conversion guide, see hex to decimal in Python.

JavaScript

const h = 'FF5733';
const r = parseInt(h.substring(0, 2), 16); // 255
const g = parseInt(h.substring(2, 4), 16); // 87
const b = parseInt(h.substring(4, 6), 16); // 51

// Or use a regex
const [, r2, g2, b2] = '#FF5733'.match(/^#?([\da-f]{2})([\da-f]{2})([\da-f]{2})$/i).map(x => parseInt(x, 16));

For the full guide, see hex to decimal in JavaScript.

C

#include <stdio.h>
int main(void) {
    unsigned hex = 0xFF5733;
    int r = (hex >> 16) & 0xFF;  /* 255 */
    int g = (hex >> 8)  & 0xFF;  /*  87 */
    int b =  hex        & 0xFF;  /*  51 */
    printf("rgb(%d, %d, %d)\n", r, g, b);
    return 0;
}

Bit shifting extracts each byte: shift right by 16 for red and by 8 for green, and mask with 0xFF to keep only the lowest byte. For the full guide, see hex to decimal in C.

Other languages

LanguageMethodExample
JavaInteger.parseInt(pair, 16)Integer.parseInt("FF", 16) → 255
Excel=HEX2DEC() on each pair=HEX2DEC("FF") → 255
Bashprintf '%d' 0xRRprintf '%d' 0xFF → 255
SwiftUInt8(pair, radix: 16)UInt8("FF", radix: 16) → 255
PHPhexdec(pair)hexdec("FF") → 255

Each language converts one hex pair to a decimal channel value. Repeat for all three pairs to get the full RGB triplet.

RGB to hex conversion

To reverse the process, convert each decimal channel value to a two-digit hex number and join the three pairs.

Steps

  1. Take the red, green and blue values (each 0 to 255).
  2. Divide each value by 16. The quotient is the left hex digit and the remainder is the right hex digit.
  3. Write the three pairs side by side with a # prefix.

Example: rgb(255, 87, 51) to hex

ChannelValue÷ 16QuotientRemainderHex pair
Red255255 ÷ 1615 (F)15 (F)FF
Green8787 ÷ 165757
Blue5151 ÷ 163333

Result: #FF5733. Most languages format this directly: Python’s f'{255:02X}' returns 'FF', and JavaScript’s (255).toString(16) returns 'ff'. Try your own values below.

Frequently asked questions

How do you convert hex to RGB?
Split the six hex digits into three pairs and convert each pair to decimal. Multiply the left digit by 16 and add the right digit. The three results are the red, green and blue values, each from 0 to 255.
What is #FF0000 in RGB?
#FF0000 is rgb(255, 0, 0), which is pure red. The red channel is at maximum (FF = 255), and the green and blue channels are both zero.
What is the difference between hex and RGB?
Hex and RGB describe the same colour in different number systems. Hex uses base 16 (two digits per channel), and RGB uses base 10 (a number from 0 to 255 per channel). #FF5733 and rgb(255, 87, 51) are the same colour.
What is RGBA?
RGBA adds an alpha channel to RGB, which gives four values: red, green, blue and alpha. Alpha controls opacity from 0 (fully transparent) to 1 in CSS, or from 0 to 255 as a raw byte. The 8-digit hex code #2440C880 is rgba(36, 64, 200, 0.50).
What is the difference between RGBA and ARGB?
RGBA puts alpha after the colour channels (RRGGBBAA), and ARGB puts it before them (AARRGGBB). CSS and web standards use RGBA order. Android, .NET and some graphics libraries use ARGB order.
How does 3-digit hex shorthand work?
Each digit is doubled to form the full 6-digit code. #F0A becomes #FF00AA. The shorthand is valid only when every pair has two identical digits.
What is HSL?
HSL stands for hue, saturation and lightness. Hue is an angle from 0 to 360 on the colour wheel, saturation is a percentage from grey to vivid, and lightness is a percentage from black to white. Convert hex to RGB first, then calculate HSL from the three channel values.
What is the difference between HSL and HSV?
HSL and HSV share the same hue, but their saturation and third component differ. HSL lightness is the midpoint of the brightest and dimmest channels, and HSL saturation measures colourfulness relative to that lightness. HSV value is the brightest channel alone, and HSV saturation measures colourfulness relative to that peak. Design tools usually use HSV (labelled HSB), while CSS uses HSL.
Can a hex colour code have more than 6 digits?
Yes. An 8-digit hex code adds a two-digit alpha channel for transparency. #2440C880 is the colour #2440C8 at 50% opacity. CSS also accepts a 4-digit shorthand (#RGBA), which expands the same way as the 3-digit form.
Why do screens use RGB?
Each pixel on an LCD or OLED screen has a red, a green and a blue sub-pixel. Mixing those three lights at different intensities produces every visible colour. RGB matches the hardware directly, so hex codes (which store RGB as bytes) map to pixel values with no extra conversion.
How many colours can hex represent?
A 6-digit hex code represents 16,777,216 colours (256³). Each of the three channels has 256 levels (00 to FF), so the total is 256 × 256 × 256. This is 24-bit colour, which covers the sRGB range that most screens display.