Short answer
RGB to HEX conversion turns three decimal color values into one six-digit hex code. Take each channel from 0 to 255, write it as two hexadecimal digits, and join the three pairs after a #. For example, rgb(255, 87, 51) becomes #FF5733. The color stays the same; only the writing changes, so you can paste the HEX code straight into CSS or a design tool.
Last verified against the sources listed below.
Key takeaways
- RGB and HEX describe the same sRGB color; HEX is just the three 0-255 channel values written in base 16.
- Each channel becomes two hex digits, so rgb(255, 87, 51) converts to #FF5733.
- Values below 16 need a leading zero: rgb(0, 5, 9) becomes #000509, not #059.
- HEX letters are not case sensitive: #ff5733 and #FF5733 name the same color.
- An 8-digit HEX code adds an alpha pair at the end for transparency, such as #FF573380.
- Conversion is exact and lossless in both directions, with no rounding.
You have an RGB value from a design file, and your code editor wants a HEX code. The two formats look different, but they name the same color. RGB writes each channel as a decimal number from 0 to 255. HEX writes the same three numbers in base 16, with two digits per channel.
This guide shows the conversion step by step, with worked examples you can check by hand. It also covers when HEX is the better choice, how to go back from HEX to RGB, and the mistakes that trip people up.
If you want the answer at once, the free RGB to HEX Converter does the math for you in your browser.
What does RGB to HEX mean?
RGB to HEX means rewriting an RGB color value, such as rgb(255, 87, 51), as a six-digit hexadecimal code, such as #FF5733.
RGB stands for red, green and blue. Each channel holds a number from 0 to 255, and the three numbers together set the color on a screen. The W3C CSS Color Module Level 4 defines rgb() as a function that sets the red, green and blue channels directly, with 0 as the lowest value and 255 as the highest.
HEX, short for hexadecimal, writes the same three channel values in base 16. The W3C spec says: “The CSS hex color notation allows an sRGB color to be specified by giving the channels as hexadecimal numbers.” Each channel takes two hex digits, so the full code has six digits after the # sign.
Base 16 uses the digits 0 to 9 and the letters A to F. The letter A stands for 10, B for 11, C for 12, D for 13, E for 14 and F for 15. That is why a channel value of 255 becomes FF: 15 times 16 plus 15 equals 255.
The six digits always run in the same order:
- The # sign marks the start of a hex color code.
- The first two digits set the red channel.
- The middle two digits set the green channel.
- The last two digits set the blue channel.
How does RGB to HEX conversion work?
Conversion works channel by channel: divide each 0-255 value by 16, take the whole-number part as the first hex digit, and use the remainder as the second digit.
Take 87 as an example. Divide 87 by 16 to get 5 with a remainder of 7, so 87 becomes 57 in hex. The number 255 divides by 16 to give 15 with a remainder of 15, and 15 in hex is F, so 255 becomes FF. The number 51 gives 3 with a remainder of 3, so it becomes 33.
Values below 16 need a leading zero so each channel keeps two digits. The value 5 becomes 05, and the value 0 becomes 00. Without the zero, the code would be too short and a browser would read it wrong.

The full worked example is rgb(255, 87, 51). Red 255 becomes FF, green 87 becomes 57, and blue 51 becomes 33. Join the three pairs after a # sign to get #FF5733. The diagram above shows each step, with the division for each channel on the right.
The result is exact. MDN notes that the hex syntax is case-insensitive, so #ff5733 and #FF5733 are the same color.
RGB to HEX examples
These examples show the same steps applied to colors you will meet often.
| RGB value | HEX code | Color |
|---|---|---|
| rgb(255, 0, 0) | #FF0000 | Pure red |
| rgb(0, 255, 0) | #00FF00 | Lime green |
| rgb(0, 0, 255) | #0000FF | Pure blue |
| rgb(255, 255, 255) | #FFFFFF | White |
| rgb(0, 0, 0) | #000000 | Black |
| rgb(0, 128, 255) | #0080FF | Bright blue |
| rgb(34, 139, 34) | #228B22 | Forest green |
| rgb(255, 87, 51) | #FF5733 | Warm orange-red |
Check the bright blue row: 0 becomes 00, 128 becomes 80 because 8 times 16 is 128, and 255 becomes FF. Forest green checks out too: 34 becomes 22, 139 becomes 8B because 8 times 16 plus 11 is 139, and the last 34 becomes 22 again.
With 256 levels per channel, the system covers 16,777,216 colors, which is 256 times 256 times 256. That count comes from the web colors reference on Wikipedia, and it is why HEX can name every color in the sRGB range.
Browse the full set of color tools on this site to check, pick and test any of these values.
When should you use HEX instead of RGB?
Use HEX when you need a short, copy-ready color value, and use RGB when you need to read or change a single channel at a glance.
| Situation | Best pick | Why |
|---|---|---|
| CSS color values | HEX | Short, and every browser and design tool accepts it |
| Sharing a color in a message | HEX | One short string, easy to copy and paste |
| Reading a channel value | RGB | Decimal numbers are easier to read than hex pairs |
| Setting transparency in CSS | RGB with alpha | The alpha number reads as a plain decimal, such as 0.5 |
| Design tool color fields | HEX | Illustrator shows a # field in its Color Picker |
| Print work | Neither | Print uses CMYK, not screen color |
HEX wins for CSS because it is short. A six-character code fits in a style sheet, a chat message or a brand guide without extra syntax. RGB wins when you tune one channel, because a decimal number shows the level at once.
HEX has one more edge: it is the default in most design tools. Adobe’s Color Picker shows the six-digit hex value below the RGB values, so designers copy it straight from Illustrator into code.
Neither format changes the color. If a value looks wrong after you paste it, the code was typed wrong or the channel order was mixed up, not the format itself.
How to convert RGB to HEX (and back)
Convert RGB to HEX in three moves: split the value into its three channels, turn each channel into two hex digits, and join the pairs after a # sign.
- Write down the three channel values from the rgb() value. For rgb(255, 87, 51), the channels are 255, 87 and 51.
- Turn each channel into hex. Divide by 16, take the whole part as the first digit and the remainder as the second. Use A to F for remainders 10 to 15, and add a leading zero when the value is below 16.
- Join the three pairs after a # sign. For the example, FF, 57 and 33 join to form #FF5733.
- Paste the code into your CSS or design tool and check that the color looks right.
The fastest route is the free RGB to HEX Converter: type the three numbers and copy the HEX code. It runs in your browser, so nothing is uploaded.
To go the other way, split a HEX code into its three pairs and read each pair as a base-16 number. The code #1B7F80 splits into 1B, 7F and 80, which are 27, 127 and 128 in decimal, so it equals rgb(27, 127, 128). The free HEX to RGB Converter does this step for you.
You can also grab a color straight from your screen. The Color Picker shows the HEX code for any color you pick.
Common RGB to HEX mistakes
Most RGB to HEX mistakes come from dropped digits, mixed-up channel order, or a missing # sign.
- Dropping a leading zero. The value 5 must become 05, so rgb(0, 5, 9) is #000509. Without the zeros the code is too short and means a different color.
- Mixing up the channel order. HEX always runs red, green, blue, so #5733FF is not the same color as #FF5733. Check the order before you paste.
- Forgetting the # sign. CSS reads FF5733 without a hash as an invalid value, so the rule is ignored and the color falls back to the default.
- Reading a 3-digit code as 6 digits. The shorthand #F53 expands by doubling each digit to #FF5533, not by adding zeros. MDN notes that a single digit is duplicated, so 1 means 11.
- Using HEX for transparency by mistake. A 6-digit HEX code has no alpha, so a see-through color needs an 8-digit code such as #FF573380, or an rgb() value with an alpha number.
- Trusting a color from memory. Two codes that look alike can be far apart on screen, so always convert and check instead of guessing.
If a converted color looks wrong, recheck one channel at a time against the steps above. A single wrong digit changes the color, so the fix is usually one pair, not the whole code.
Frequently asked questions
How do I convert RGB to HEX by hand?
Split the RGB value into its three channels, then turn each 0-255 number into two hex digits. Divide the number by 16: the whole part is the first digit and the remainder is the second, with A to F standing for 10 to 15. Add a leading zero when the value is below 16, then join the three pairs after a # sign.
What is the HEX code for rgb(255, 87, 51)?
The HEX code is #FF5733. Red 255 becomes FF because 15 times 16 plus 15 equals 255. Green 87 becomes 57 because 5 times 16 plus 7 equals 87. Blue 51 becomes 33 because 3 times 16 plus 3 equals 51. Join the three pairs after a # sign to get #FF5733.
How do I convert HEX to RGB?
Split the six-digit HEX code into three pairs and read each pair as a base-16 number. For #1B7F80, the pairs 1B, 7F and 80 equal 27, 127 and 128 in decimal, so the color is rgb(27, 127, 128). A 3-digit code such as #F53 expands first by doubling each digit to #FF5533.
Is HEX the same as RGB?
Yes, they name the same color in different writing. RGB writes each channel as a decimal number from 0 to 255, while HEX writes the same three numbers in base 16 with two digits per channel. Both describe an sRGB color, and converting between them changes nothing about the color itself.
What does the # sign mean in a HEX code?
The # sign marks the start of a hex color code in CSS and design tools. After it come three pairs of hex digits for red, green and blue, or four pairs when an alpha value is added. Without the # sign, a browser or design tool cannot tell that the six characters are meant as a color.
Can a HEX code include transparency?
Yes, with an 8-digit HEX code. The first six digits set the red, green and blue channels, and the last two digits set the alpha channel from 00 for fully clear to FF for fully solid. For example, #FF573380 is the same orange-red as #FF5733 at about half opacity.
Sources
- CSS Color Module Level 4 · W3C, 2026
- <hex-color> CSS type · MDN, 2026
- CSS color values · MDN, 2026
- Use Color Picker in Illustrator · Adobe, 2026
- Web colors · Wikipedia, 2026