GameParticles
Colors

How to Convert RGB to HEX (and HEX to RGB) by Hand

Convert RGB to HEX by turning each 0 to 255 channel into two base 16 digits. Worked examples, HEX to RGB, shorthand codes and 8-digit alpha hex explained.

Updated

To convert RGB to HEX, turn each of the three channel values (0 to 255) into a two-digit base 16 number and join them after a #. For example, rgb(255, 99, 71) becomes #ff6347. To convert HEX to RGB, split the code into three pairs and turn each pair back into a decimal number.

This guide covers the arithmetic only. For what the notations mean and how HSL fits in, read HEX, RGB and HSL color codes explained.

The RGB to HEX formula

Hexadecimal has sixteen digits: 0 to 9, then a to f for the values 10 to 15. One channel of an RGB color always fits in exactly two of them.

For each channel, divide the value by 16. The whole-number quotient is the first hex digit and the remainder is the second.

Worked example with rgb(255, 99, 71):

  1. Red: 255 divided by 16 is 15 remainder 15. Both digits are 15, which is f, so red is ff.
  2. Green: 99 divided by 16 is 6 remainder 3, so green is 63.
  3. Blue: 71 divided by 16 is 4 remainder 7, so blue is 47.
  4. Join the pairs in red, green, blue order: #ff6347.

The one mistake people make is dropping a leading zero. A channel value below 16 still needs two digits, so 10 is 0a, not a. That makes rgb(0, 10, 5) equal to #000a05. Without the padding you would get a four-digit code that means something else entirely.

The HEX to RGB formula

Going back is multiplication instead of division. Split the code into three pairs. For each pair, multiply the first digit by 16 and add the second.

Worked example with #1e90ff:

  1. 1e: 1 times 16 is 16, plus e (14) gives 30.
  2. 90: 9 times 16 is 144, plus 0 gives 144.
  3. ff: 15 times 16 is 240, plus 15 gives 255.

Result: rgb(30, 144, 255). Case does not matter, so #1E90FF converts the same way.

Quick reference: channel values

These values show up constantly, and knowing them lets you read many codes without any arithmetic.

DecimalHexShare of full intensity
0000%
513320%
644025%
1026640%
1288050%
1539960%
192c075%
204cc80%
255ff100%

And a few complete colors:

ColorRGBHEX
Blackrgb(0, 0, 0)#000000
Whitergb(255, 255, 255)#ffffff
Redrgb(255, 0, 0)#ff0000
Limergb(0, 255, 0)#00ff00
Bluergb(0, 0, 255)#0000ff
Yellowrgb(255, 255, 0)#ffff00
Grayrgb(128, 128, 128)#808080
Orangergb(255, 165, 0)#ffa500

The names in that table are CSS named colors. To look up the nearest name for any other code, use the Color Name Finder.

Shorthand hex: three digits instead of six

When both digits of every pair are the same, CSS lets you write one digit per channel. The browser doubles each digit when it reads the code:

  • #f60 expands to #ff6600.
  • #fff expands to #ffffff.
  • #09c expands to #0099cc.

Doubling is the rule, not padding with zeros. #f60 is not #f06000. A code such as #3b82f6 has no shorthand because its pairs do not repeat. To convert shorthand to RGB, expand it first, then use the normal formula: #09c is #0099cc, which is rgb(0, 153, 204).

Alpha: 8-digit and 4-digit hex

CSS accepts a fourth pair for opacity, written #RRGGBBAA. The alpha pair runs from 00 (fully transparent) to ff (fully opaque). To get it from a percentage, multiply the opacity by 255, round, and convert to hex. For 50 percent, 0.5 times 255 is 127.5, which rounds to 128, which is 80.

OpacityAlpha hex
100%ff
90%e6
80%cc
75%bf
60%99
50%80
40%66
25%40
20%33
10%1a
0%00

So tomato red at 50 percent opacity is #ff634780, the same color as rgb(255 99 71 / 50%). The shorthand works here too: a four-digit code such as #f608 expands to #ff660088.

One trap: CSS puts alpha last, but Android color resources use #AARRGGBB, with alpha first. If a color copied from an Android project looks wrong on the web, move the first pair to the end.

Converting in code

Most languages can do this in one line.

  • JavaScript, RGB to HEX: "#" + [r, g, b].map(v => v.toString(16).padStart(2, "0")).join("")
  • JavaScript, HEX to RGB: parseInt("ff", 16) returns 255. Run it on each pair.
  • Python, RGB to HEX: "#{:02x}{:02x}{:02x}".format(r, g, b)
  • Python, HEX to RGB: int("ff", 16)
  • Excel and Google Sheets: =DEC2HEX(255, 2) and =HEX2DEC("FF")

The padStart(2, "0") and 02x parts are the leading-zero rule from earlier. Leave them out and dark colors break.

Converting with a tool

For HEX to RGB, paste a six-digit code into the Color Converter. It returns the rgb() and hsl() forms, the bare channel values, a CSS variable line, and the eight-digit code at 50 percent opacity, each with a copy button. The field expects six digits, so expand a shorthand code such as #f60 before pasting it.

For RGB to HEX, the converter's text field takes a HEX code rather than three channel numbers. There are two straightforward routes. Do the division above, which takes a few seconds per channel, or convert each channel in the Base Converter by entering the decimal value and reading the hexadecimal result. Pad any single digit with a zero, join the three pairs, then paste the finished code into the Color Converter to confirm the swatch matches the color you expected.

More color utilities, including contrast checking and palette generation, are in the colors category.

Try it

Paste #ff6347 into the Color Converter and check the result against the worked example above: you should see rgb(255, 99, 71). Then click the swatch to pick any other color and read its HEX and RGB values side by side.

Tools in this guide

Frequently asked questions

How do I convert RGB to HEX manually?

Divide each channel value by 16. The whole-number result is the first hex digit and the remainder is the second, using a to f for 10 to 15. Join the three pairs after a hash sign, so rgb(255, 99, 71) becomes #ff6347.

How do I convert HEX to RGB?

Split the six digits into three pairs. For each pair, multiply the first digit by 16 and add the second, counting a to f as 10 to 15. The code #1e90ff gives 30, 144 and 255, which is rgb(30, 144, 255).

What is a 3-digit hex color code?

It is shorthand for a six-digit code in which both digits of every pair match. Each digit is doubled when the browser reads it, so #f60 means #ff6600 and #fff means #ffffff. Codes such as #3b82f6 cannot be shortened.

What do the last two digits of an 8-digit hex color mean?

They are the alpha channel, which sets opacity from 00 (fully transparent) to ff (fully opaque). Multiply the opacity by 255 and convert to hex, so 50 percent becomes 80 and #ff634780 is half-transparent tomato red.

More colors guides

JSON Formatter
Developer · Format, validate and minify JSON with syntax highlighting.
↵
Base64 Encode / Decode
Developer · Encode text to Base64 or decode Base64 back to plain text.
↵
URL Encoder / Decoder
Developer · Encode or decode URL components and query strings.
↵
Hash Generator
Developer · Generate MD5, SHA-1, SHA-256 and SHA-512 hashes.
↵
UUID Generator
Developer · Generate UUIDs v4 (random) in bulk with one click.
↵
Regex Tester
Developer · Test regular expressions against text with live match highlighting.
↵
Word Counter
Text · Count words, characters, sentences, paragraphs and reading time.
↵
Case Converter
Text · Convert text between UPPER, lower, Title, camelCase, snake_case and more.
↵
List Sorter
Text · Sort, reverse, deduplicate and shuffle lists of text.
↵
Markdown Preview
Text · Write Markdown and see a live rendered preview side by side.
↵
Percentage Calculator
Numbers · Calculate percentages, increases, decreases and what % one number is of another.
↵
Color Converter
Colors · Convert colors between HEX, RGB, HSL and HSV instantly.
↵
Snake
Arcade · Eat food, grow longer, don't hit the walls. A timeless classic.
↵
2048
Puzzle · Slide tiles and combine them to reach the legendary 2048 tile.
↵
Word Grid
Puzzle · Guess the hidden five letter word in six tries using color clues.
↵
Sudoku
Puzzle · Fill the 9x9 grid with logic. Four difficulties from easy to expert.
↵
Minesweeper
Strategy · Use number clues to clear the minefield. Classic logic, three levels.
↵
Breakout
Arcade · Smash every brick with paddle and ball. Power-ups, combos, endless levels.
↵
Typing Practice
Casual · Build your WPM and accuracy with timed drills from 30 seconds to 5 minutes. Find your weak keys.
↵
TypeRunner
Arcade · Multiplayer typing race. Race friends by link, beat computer runners, or chase your own ghost.
↵
Typing Blocks
Arcade · Type each word before its wooden block lands. Miss, and the pile climbs toward the line.
↵
Bubble Shooter
Arcade · Aim, bank shots off the walls, and pop groups of three or more bubbles.
↵
Road Rush
Arcade · Race seven rivals through traffic and punch your way to the front.
↵