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
- Remove the # prefix.
- Split the remaining digits into three pairs: the first pair is red, the second is green and the third is blue.
- Convert each pair from hexadecimal to decimal. Multiply the left digit by 16 and add the right digit.
- Write the three decimal values as rgb(R, G, B).
Conversion examples
Example 1: #2440C8
| Channel | Hex pair | Calculation | Decimal |
|---|---|---|---|
| Red | 24 | 2 × 16 + 4 | 36 |
| Green | 40 | 4 × 16 + 0 | 64 |
| Blue | C8 | 12 × 16 + 8 | 200 |
Result: rgb(36, 64, 200), a medium blue with a strong blue channel and low red.
Example 2: #FF5733
| Channel | Hex pair | Calculation | Decimal |
|---|---|---|---|
| Red | FF | 15 × 16 + 15 | 255 |
| Green | 57 | 5 × 16 + 7 | 87 |
| Blue | 33 | 3 × 16 + 3 | 51 |
Result: rgb(255, 87, 51), a vivid red-orange at full red intensity.
Example 3: #1E90FF (Dodger Blue)
| Channel | Hex pair | Calculation | Decimal |
|---|---|---|---|
| Red | 1E | 1 × 16 + 14 | 30 |
| Green | 90 | 9 × 16 + 0 | 144 |
| Blue | FF | 15 × 16 + 15 | 255 |
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.
| Shorthand | Expands to | RGB |
|---|---|---|
#F00 | #FF0000 | rgb(255, 0, 0), red |
#0F0 | #00FF00 | rgb(0, 255, 0), green |
#00F | #0000FF | rgb(0, 0, 255), blue |
#FFF | #FFFFFF | rgb(255, 255, 255), white |
#000 | #000000 | rgb(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
| Hex | Decimal | Opacity |
|---|---|---|
FF | 255 | 100% (solid) |
BF | 191 | 75% |
80 | 128 | 50% |
40 | 64 | 25% |
00 | 0 | 0% (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.
| Format | Hex code | Byte order |
|---|---|---|
| CSS (RGBA) | #2440C880 | Red, Green, Blue, Alpha |
| Android (ARGB) | #802440C8 | Alpha, 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.
| Hex | RGB | Colour |
|---|---|---|
| #000000 | rgb(0, 0, 0) | Black |
| #FFFFFF | rgb(255, 255, 255) | White |
| #FF0000 | rgb(255, 0, 0) | Red |
| #00FF00 | rgb(0, 255, 0) | Lime (green) |
| #0000FF | rgb(0, 0, 255) | Blue |
| #FFFF00 | rgb(255, 255, 0) | Yellow |
| #00FFFF | rgb(0, 255, 255) | Cyan |
| #FF00FF | rgb(255, 0, 255) | Magenta |
| Hex | RGB | Colour |
|---|---|---|
| #808080 | rgb(128, 128, 128) | Grey |
| #FFA500 | rgb(255, 165, 0) | Orange |
| #800080 | rgb(128, 0, 128) | Purple |
| #008080 | rgb(0, 128, 128) | Teal |
| #FFC0CB | rgb(255, 192, 203) | Pink |
| #A52A2A | rgb(165, 42, 42) | Brown |
| #1E90FF | rgb(30, 144, 255) | Dodger Blue |
| #FFD700 | rgb(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
- Scale each RGB channel to the range 0–1 by dividing by 255.
- Find the maximum channel (Cmax) and the minimum channel (Cmin). Calculate delta = Cmax − Cmin.
- Lightness = (Cmax + Cmin) / 2.
- Saturation = delta / (1 − |2 × Lightness − 1|) when delta > 0, or 0 when delta = 0.
- 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 | #2440C8 | Built from |
|---|---|---|
| RGB | rgb(36, 64, 200) | Each hex pair converted to decimal |
| HSL | hsl(230, 69%, 46%) | Lightness is the average of the brightest and dimmest channels |
| HSV / HSB | hsv(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
| Language | Method | Example |
|---|---|---|
| Java | Integer.parseInt(pair, 16) | Integer.parseInt("FF", 16) → 255 |
| Excel | =HEX2DEC() on each pair | =HEX2DEC("FF") → 255 |
| Bash | printf '%d' 0xRR | printf '%d' 0xFF → 255 |
| Swift | UInt8(pair, radix: 16) | UInt8("FF", radix: 16) → 255 |
| PHP | hexdec(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
- Take the red, green and blue values (each 0 to 255).
- Divide each value by 16. The quotient is the left hex digit and the remainder is the right hex digit.
- Write the three pairs side by side with a # prefix.
Example: rgb(255, 87, 51) to hex
| Channel | Value | ÷ 16 | Quotient | Remainder | Hex pair |
|---|---|---|---|---|---|
| Red | 255 | 255 ÷ 16 | 15 (F) | 15 (F) | FF |
| Green | 87 | 87 ÷ 16 | 5 | 7 | 57 |
| Blue | 51 | 51 ÷ 16 | 3 | 3 | 33 |
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?
What is #FF0000 in RGB?
What is the difference between hex and RGB?
What is RGBA?
What is the difference between RGBA and ARGB?
How does 3-digit hex shorthand work?
What is HSL?
What is the difference between HSL and HSV?
Can a hex colour code have more than 6 digits?
Why do screens use RGB?
How many colours can hex represent?
Related tools
- Hex to decimal converter: convert any hex number to decimal with digit-by-digit steps.
- Decimal to hex converter: convert a decimal value to hex.
- Hex to binary converter: see the bit pattern of any hex value.
- Hex to text converter: decode hex bytes into ASCII or UTF-8 text.
- Hex to octal converter: convert hex to base 8.