HEX to RGB Formula Explained (With Examples)

Featured graphic for “HEX to RGB Formula Explained (With Examples)” showing color swatches and codes

To convert HEX to RGB, split the six-digit code into three pairs and convert each pair from hexadecimal (base 16) to decimal: value = first digit × 16 + second digit. For example, #6C5CE7 becomes 6C, 5C and E7, which equal 108, 92 and 231 — so the color is rgb(108, 92, 231). Below is the full method, with shortcuts and edge cases.

Understanding Hexadecimal

Hexadecimal uses 16 symbols: the digits 0–9 and the letters A–F.

Hex0–9ABCDEF
Decimal0–9101112131415

A two-digit hex number can represent 16 × 16 = 256 values, from 00 (0) to FF (255) — exactly the range of one RGB channel.

The HEX to RGB Formula

For a code written as #RRGGBB:

R = (first R digit × 16) + second R digit
G = (first G digit × 16) + second G digit
B = (first B digit × 16) + second B digit

Worked Examples

Example 1: #6C5CE7

  • R = 6C = 6 × 16 + 12 = 108
  • G = 5C = 5 × 16 + 12 = 92
  • B = E7 = 14 × 16 + 7 = 231

Result: rgb(108, 92, 231).

Example 2: #0F766E

  • R = 0F = 0 × 16 + 15 = 15
  • G = 76 = 7 × 16 + 6 = 118
  • B = 6E = 6 × 16 + 14 = 110

Result: rgb(15, 118, 110).

Example 3: #FFFFFF and #000000

FF = 15 × 16 + 15 = 255, so white is rgb(255, 255, 255). 00 = 0, so black is rgb(0, 0, 0).

Short (3-Digit) HEX Codes

A three-digit code doubles each digit: #F80 means #FF8800. Then apply the formula: FF = 255, 88 = 136, 00 = 0 → rgb(255, 136, 0).

8-Digit HEX Codes (Transparency)

An eight-digit code adds an alpha pair at the end: #RRGGBBAA. Convert the alpha pair to decimal and divide by 255 to get the CSS alpha value.

Example: #6C5CE780 → alpha 80 = 8 × 16 + 0 = 128, and 128 ÷ 255 ≈ 0.5. The result is rgba(108, 92, 231, 0.5).

Converting in Code

function hexToRgb(hex) {
  let h = hex.replace('#', '');
  if (h.length === 3) h = h.split('').map(c => c + c).join('');
  const n = parseInt(h.slice(0, 6), 16);
  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
}
hexToRgb('#6C5CE7'); // [108, 92, 231]

Common Mistakes

  • Mixing up the letter values. Remember A=10 through F=15.
  • Forgetting to expand short codes. #F80 is not #F80000.
  • Reading alpha as a percentage. 80 in hex is 128 of 255 (about 50%), not 80%.

The Reverse: RGB to HEX Formula

Once you understand HEX to RGB, converting in the other direction is straightforward. For each RGB channel, divide the value by 16. The whole number result is the first hexadecimal digit, and the remainder is the second digit.

first digit  = floor(value / 16)
second digit = value mod 16

Worked example: rgb(220, 38, 127)

  • Red 220: 220 ÷ 16 = 13 remainder 12. 13 = D, 12 = C, so red is DC.
  • Green 38: 38 ÷ 16 = 2 remainder 6, so green is 26.
  • Blue 127: 127 ÷ 16 = 7 remainder 15. 15 = F, so blue is 7F.

The result is #DC267F. Remember that values below 16 need a leading zero. For example, a channel value of 9 becomes 09, not just 9, otherwise the code will have the wrong length.

Hexadecimal Digit Reference

Keeping this table in mind makes manual conversion much faster:

  • 0 = 0, 1 = 1, 2 = 2, 3 = 3, 4 = 4
  • 5 = 5, 6 = 6, 7 = 7, 8 = 8, 9 = 9
  • A = 10, B = 11, C = 12, D = 13, E = 14, F = 15

Useful two-digit values to memorize

  • 00 = 0
  • 33 = 51
  • 40 = 64
  • 66 = 102
  • 80 = 128
  • 99 = 153
  • BF = 191
  • CC = 204
  • E6 = 230
  • FF = 255

A neat pattern: when both digits are the same, the decimal value is the digit multiplied by 17. For example, 33 = 3 × 17 = 51, and CC = 12 × 17 = 204. This is also why 3-digit shorthand codes work: each digit is simply doubled.

Mental Math Shortcuts

With a little practice you can estimate RGB values from a HEX code without a calculator.

  1. Use the first digit for a quick estimate. The first digit times 16 gives you a value within 15 of the real number. For example, A4 is roughly 10 × 16 = 160 (the exact value is 164).
  2. Think in percentages. 80 is about 50%, C0 is about 75%, and 40 is about 25% of the maximum.
  3. Look for patterns. If all three pairs are equal, the color is gray. If one pair is FF and the others are 00, the color is a pure primary.
  4. Compare pairs. The largest pair tells you the dominant channel and therefore the general hue.

These shortcuts are perfect for quickly understanding a color while reading code, even if you still use a converter for exact values.

From RGB to Other Values

RGB values are often the starting point for further calculations.

Percentages

Some tools ask for RGB as percentages. Divide each value by 255 and multiply by 100. For rgb(58, 134, 255): 22.7%, 52.5% and 100%.

Decimal values from 0 to 1

Graphics programming, shaders and some design tools use values between 0 and 1. Divide each channel by 255: rgb(58, 134, 255) becomes (0.227, 0.525, 1.0).

HSL

To get HSL, convert the channels to the 0–1 range, find the maximum and minimum, and calculate lightness as their average. Saturation and hue need a few more steps, which is why most people use a converter for HSL.

Relative luminance

Accessibility contrast calculations start with RGB values, linearize them and combine them as 0.2126 × R + 0.7152 × G + 0.0722 × B. This tells you how bright a color appears and is used to compute contrast ratios.

More Worked Examples

Example: #F97316 (orange)

  • F9 → 15 × 16 + 9 = 249
  • 73 → 7 × 16 + 3 = 115
  • 16 → 1 × 16 + 6 = 22

Result: rgb(249, 115, 22). Red is highest and blue is lowest, which confirms the warm orange hue.

Example: #0F766E (teal)

  • 0F → 0 × 16 + 15 = 15
  • 76 → 7 × 16 + 6 = 118
  • 6E → 6 × 16 + 14 = 110

Result: rgb(15, 118, 110). Green and blue are nearly equal and red is very low, which produces teal.

Example: #7C3AED (violet)

  • 7C → 7 × 16 + 12 = 124
  • 3A → 3 × 16 + 10 = 58
  • ED → 14 × 16 + 13 = 237

Result: rgb(124, 58, 237). Strong blue with a good amount of red creates a vivid violet.

Example: #F5F5F4 (warm off-white)

  • F5 → 245
  • F5 → 245
  • F4 → 244

Result: rgb(245, 245, 244). Blue is one step lower than red and green, which gives the white a very slight warm tint.

Practice Problems

Try converting these HEX codes yourself, then check the answers below.

  1. #22C55E
  2. #E11D48
  3. #64748B
  4. #FACC15
  5. #1E293B

Answers

  1. #22C55E = rgb(34, 197, 94)
  2. #E11D48 = rgb(225, 29, 72)
  3. #64748B = rgb(100, 116, 139)
  4. #FACC15 = rgb(250, 204, 21)
  5. #1E293B = rgb(30, 41, 59)

If you got them all right, you understand the formula well enough to convert any color by hand.

Where HEX to RGB Conversion Is Needed

  • Transparency in older code: before 8-digit HEX was widely supported, developers converted HEX to RGB so they could write rgba() with an alpha value.
  • JavaScript and canvas: drawing APIs, image filters and animations often work with numeric RGB values.
  • Video and streaming software: overlay and lower-third tools frequently ask for RGB inputs.
  • Smart lighting and LEDs: RGB light strips and smart bulbs accept red, green and blue values from 0 to 255.
  • Game development and 3D: engines often expect colors as decimals between 0 and 1, which start from RGB.
  • Office software: some versions of presentation and spreadsheet apps accept RGB but not HEX in their custom color dialogs.
  • Accessibility tools: contrast calculations start from RGB values.

Why Computers Use Hexadecimal for Colors

Computers store data in bits, which are ones and zeros. Eight bits form a byte, and one byte can hold 256 different values, from 0 to 255. That is exactly the range of each RGB channel. Hexadecimal is a convenient way to write bytes because every byte fits into exactly two hex digits, and each hex digit represents exactly four bits.

Writing colors in decimal would require up to three digits per channel and separators between them. Writing them in binary would need 24 ones and zeros. Hexadecimal sits in a sweet spot: compact, fixed-length and easy for programmers to read. That is why HEX became the standard for colors on the web from the early days of HTML.

In spreadsheets

Spreadsheet apps can convert HEX pairs with the HEX2DEC function. If cell A1 contains 3A86FF, you can calculate the three channels with:

=HEX2DEC(MID(A1,1,2))   → 58
=HEX2DEC(MID(A1,3,2))   → 134
=HEX2DEC(MID(A1,5,2))   → 255

This is handy when you need to convert a long list of brand or product colors at once.

In Python

hex_code = "3A86FF"
r, g, b = (int(hex_code[i:i+2], 16) for i in (0, 2, 4))
print(r, g, b)  # 58 134 255

In design software

Most design tools convert automatically. Paste a HEX code into the color field and switch the display mode to RGB to read the values. This is the fastest method when you only need one or two colors.

In the browser

Open developer tools, inspect any element and click a color swatch. Many browsers let you click the format label to cycle between HEX, RGB and HSL.

Understanding Color Depth

Standard HEX and RGB colors use 8 bits per channel, which is called 24-bit color. That gives 16,777,216 possible colors, often described as “true color”. It is more than enough for almost all websites and everyday design work.

Professional photography, video and high dynamic range (HDR) displays can use 10, 12 or even 16 bits per channel, allowing billions of colors and smoother gradients. These higher bit depths cannot be written in normal 6-digit HEX codes. On the web, newer CSS color functions and wide-gamut color spaces are starting to support some of this extra range, but HEX and RGB remain the everyday standard.

Validating a HEX Code Before Converting

Before converting, it is worth confirming that the HEX code is valid. Many conversion errors come from small typing mistakes rather than from the formula itself. A valid CSS HEX color:

  • Starts with a hash symbol (#) when used in CSS.
  • Has exactly 3, 4, 6 or 8 characters after the hash.
  • Uses only the digits 0–9 and the letters A–F (uppercase or lowercase).

In code, a simple regular expression can check this: /^#([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i. If the pattern does not match, the browser would ignore the color, so it is better to catch the problem early and show a helpful message.

Look out for look-alike characters. The letter O is often typed instead of the number 0, and the letter I or l instead of the number 1. Neither O, I nor l is a valid hexadecimal digit, so any code containing them is broken.

HEX and RGB in Brand Guidelines

Professional brand guidelines usually list every color in several formats side by side: HEX for websites, RGB for digital tools and video, CMYK for print and sometimes Pantone for spot colors. Listing HEX and RGB together saves designers and developers from converting colors themselves and reduces the risk of small errors.

If you create a style guide for a client or your own brand, include both values for every color, plus a short note on where each color should be used. For example: “Primary Blue — HEX #2563EB — RGB 37, 99, 235 — buttons, links and key highlights.” This small effort prevents inconsistent colors across websites, presentations, social media graphics and printed materials.

A Short History of Web Colors

In the early days of the web, many computers could display only 256 colors at once. To make sure websites looked the same everywhere, designers used a “web-safe” palette of 216 colors. Every web-safe color used only the values 00, 33, 66, 99, CC and FF for each channel, such as #3366CC or #FF9900. That is why so many older websites share a similar look.

Today almost every screen can display millions of colors, so the web-safe palette is no longer necessary. However, the HEX notation chosen in those early years has remained the most familiar way to write colors, and understanding how it maps to RGB is still one of the most useful basic skills for anyone working with digital design.

Key Takeaways

  • A HEX code is three pairs of hexadecimal digits for red, green and blue.
  • Each pair converts to decimal with: first digit × 16 + second digit.
  • The result is always between 0 and 255 for each channel.
  • Shorthand codes double each digit; 8-digit codes add an alpha pair.
  • Converting back uses division by 16 and the remainder.

Frequently Asked Questions

Is HEX to RGB conversion exact?

Yes. Each hex pair maps to exactly one whole number from 0 to 255.

Are HEX codes case-sensitive?

No. #6c5ce7 and #6C5CE7 are the same color.

How do I convert RGB back to HEX?

Divide each value by 16: the whole number is the first digit and the remainder is the second. The RGB to HEX Converter does this instantly.

What is the RGB value of #808080?

80 = 128, so #808080 is rgb(128, 128, 128), a medium gray.

Final Thoughts

HEX and RGB describe the same colors in different number systems. Split the HEX code into pairs, convert each pair from base 16, and you have the RGB value. For instant results, use the HEX to RGB Converter; for background on all formats, read HEX vs RGB vs HSL.

Written by the Mage Color Picker Team

We build free online color tools and write practical guides on color theory, color codes and accessible design for designers and developers.

Scroll to Top