RGB to Hex Converter
RGB to hex converter: each of the red, green and blue values, 0 to 255, becomes two hex digits and the three pairs follow a #, so rgb(255, 136, 0) is #FF8800. Paste rgb(), plain numbers or percentages, one color per line, and see the swatch, HSL and contrast.
| Format | Value |
|---|---|
| Hex | #FF8800 |
| RGB | rgb(255, 136, 0) |
| HSL | hsl(32, 100%, 50%) |
| CMYK (approximate) | cmyk(0%, 47%, 100%, 0%) |
| Contrast on white | 2.39 : 1 |
| Contrast on black | 8.77 : 1 |
How an RGB color becomes a hex code
An RGB color is three numbers, one each for the amount of red, green and blue light, and each runs from 0 to 255. A hex color code is the same three numbers written in base 16, two digits per channel, behind a hash sign. Converting RGB to hex therefore means converting three small numbers to hexadecimal and joining the results. The channel order never changes: red first, then green, then blue.
In the picture the red channel is 255, which is F and F in hex, the green channel 136 is 8 and 8, and the blue channel 0 is 0 and 0. The three pairs make #FF8800, an orange. The conversion is exact and loses nothing, because two hex digits can hold exactly the 256 values a channel can take. Type or paste values into the converter above and the hex code, a swatch and the HSL form update as you go.
Converting one channel by hand
For a single channel the method is division by 16. The whole-number quotient is the first hex digit, and the remainder is the second. For 136, 16 goes into it 8 times (8 × 16 = 128) with 8 left over, so the digits are 8 and 8. For 255, 16 goes 15 times with 15 left, and 15 is the digit F, so the pair is FF. Remember that the digits 10 to 15 are written A to F: a quotient or remainder of 11 is B, and 14 is E.
| Decimal | Sixteens × 16 + ones | Hex |
|---|---|---|
| 0 | 0 × 16 + 0 | 00 |
| 16 | 1 × 16 + 0 | 10 |
| 32 | 2 × 16 + 0 | 20 |
| 64 | 4 × 16 + 0 | 40 |
| 128 | 8 × 16 + 0 | 80 |
| 192 | 12 × 16 + 0 | C0 |
| 240 | 15 × 16 + 0 | F0 |
| 255 | 15 × 16 + 15 | FF |
Two shortcuts help when you read colors. A channel that is a multiple of 16 always ends in 0 in hex: 64 is 40 and 192 is C0. A value that is one less than a multiple of 16 ends in F, so 31 is 1F and 255 is FF. The hex to decimal converter runs the other way for a single number, and the hex to RGB page converts a whole color code back into its three channels.
Accepted RGB notations
RGB is written in several ways, and the converter reads all the usual ones so that you can paste straight from a design tool or a style sheet. Each of the lines below gives the same result, #FF8800.
rgb(255, 136, 0)
rgb(255 136 0)
255, 136, 0
255 136 0
100% 53.3% 0%Put one color on each line to convert a list. A channel above 255 is reported by name, for example that the red value is 300 and must run from 0 to 255, rather than clamped silently, since a clamped value would give a color you did not ask for. Percentages are converted to the 0 to 255 scale first. The three number boxes above the output show the channels of the current color and change it as you type in them, and the color picker sets the value from a swatch.
RGB with transparency
The rgba() form adds a fourth number, the alpha, which is the opacity from 0 for fully transparent to 1 for fully opaque. Hex has an eight-digit form for it, #RRGGBBAA, where the last pair is the alpha scaled to 0 to 255. The scaling is a multiplication by 255 and a rounding to the nearest whole number. Half transparent, 0.5, is 127.5, which rounds to 128, or 80 in hex. Note that the alpha comes last in CSS hex, while some other systems, Android's ARGB integers for instance, put it first.
| Opacity | Times 255 | Hex pair |
|---|---|---|
| 100% | 255 | FF |
| 90% | 230 | E6 |
| 75% | 191 | BF |
| 50% | 128 | 80 |
| 25% | 64 | 40 |
| 10% | 26 | 1A |
| 0% | 0 | 00 |
For example rgba(255, 0, 0, 0.5) becomes #FF000080. Modern browsers read eight-digit codes in CSS. If you need to support software that does not, keep the three-pair code and set the opacity separately. Percent alpha values are accepted as well, so rgba(255 0 0 / 50%) gives the same result as 0.5.
Three-digit hex
When each of the three pairs has two equal digits, the code can be written with one digit per channel. #FF8800 has the pairs FF, 88 and 00, all doubled, so it shortens to #F80. The short form is read back by repeating each digit, which means #F80 is #FF8800 exactly and not an approximation. A color such as #FF8801 cannot be shortened, because 01 is not a doubled pair, and rounding it to #F80 would change the color slightly.
Shortening does not make the page lighter in any way that matters, and it is a matter of taste whether your project uses it. The same logic applies to the four-digit form #RGBA. Many style guides prefer full six-digit codes because they are easier to search for. The converter always prints the full form so that the output can be compared with other tools.
Common colors in RGB and hex
These twelve colors cover most of the values you meet. Their names are the CSS color keywords, so each one also works in a style sheet by name.
| Name | RGB | Hex |
|---|---|---|
| Black | rgb(0, 0, 0) | #000000 |
| White | rgb(255, 255, 255) | #FFFFFF |
| Red | rgb(255, 0, 0) | #FF0000 |
| Lime | rgb(0, 255, 0) | #00FF00 |
| Blue | rgb(0, 0, 255) | #0000FF |
| Yellow | rgb(255, 255, 0) | #FFFF00 |
| Cyan | rgb(0, 255, 255) | #00FFFF |
| Magenta | rgb(255, 0, 255) | #FF00FF |
| Orange | rgb(255, 165, 0) | #FFA500 |
| Purple | rgb(128, 0, 128) | #800080 |
| Teal | rgb(0, 128, 128) | #008080 |
| Gray | rgb(128, 128, 128) | #808080 |
The pattern in the table explains most hex colors you will see. The six corners of the color cube (red, lime, blue, yellow, cyan, magenta) use only 00 and FF. Grays have three equal channels, so #808080 or #333333 always repeat one pair. A color that is close to a gray has three nearly equal pairs, and a strongly saturated color has at least one pair near 00 and one near FF.
Rounding when the input is not whole
RGB values from design software are sometimes decimals or percentages, for example 49.8% or 127.5. Hex cannot hold a fraction, so the value is rounded to the nearest whole number first. The converter rounds halves up, so 127.5 is 128 and 50% of 255 is 128, hex 80. The result can differ by one step from a tool that truncates instead of rounding. If two tools disagree by 01 in one channel, rounding is almost always the reason.
Round-trip safety is worth knowing. A whole-number RGB color converts to hex and back without any change. A color converted from percentages may not come back as the same percentages, because 255 is not divisible by 100, so 20% becomes 51 and returns as exactly 20%, while 33% becomes 84, which is 32.9%.
Why each channel runs from 0 to 255
Eight bits can hold 256 different values, so a channel is stored in one byte, 0 to 255. Three bytes make a 24-bit color, which has 256 × 256 × 256 = 16,777,216 possible values. Hex is a natural fit because one byte is exactly two hex digits, which is the reason color codes have six digits and why every hex pair stays between 00 and FF. The relation between bytes and hex digits is covered on the hexadecimal page.
RGB to hex in code
The conversion is one line in most languages. These samples were run and give the same code for the same input.
const toHex = (r, g, b) =>
"#" + [r, g, b].map((v) => v.toString(16).padStart(2, "0")).join("").toUpperCase();
console.log(toHex(255, 136, 0)); // #FF8800printf '#%02X%02X%02X\n' 255 136 0
# #FF8800"#{0:X2}{1:X2}{2:X2}" -f 255, 136, 0
# #FF8800The padStart(2, "0") in the JavaScript line matters: without it a channel below 16 produces a single digit, and rgb(255, 8, 0) would come out as #FF80 instead of #FF0800. The same care is needed in any language that formats numbers without a fixed width.
A note on color spaces
A hex code or RGB triple does not say by itself which colors it means. On the web both are read as sRGB unless something says otherwise, so the conversion on this page is purely a change of notation and keeps you in the same space. Converting between color spaces, such as sRGB to Display P3 or to a print profile, changes the numbers and is a different job. The CMYK values in the format table on the hex to RGB page are the simple formula and not a printer profile, and the page says so.
After the conversion
The result is ready to paste into a style sheet, a design tool or a configuration file. If the color is for text or a button, check the contrast against its background: the format table for a single color shows the contrast ratios against white and black, and the WCAG minimum for normal body text is 4.5 to 1. To go the other way and read a hex code as channels, HSL and CMYK, use the hex to RGB converter. For a color that arrives as a raw hex string inside binary data, the hex decoder shows the bytes first, and the decimal to hex converter is useful for a channel that you have as one long number.
Questions about RGB to hex
How do I convert RGB to hex?
Convert each of the three numbers, red, green and blue, to a two-digit hex number and write them one after another behind a #. Divide a channel by 16 for the first digit and take the remainder for the second: 136 is 8 × 16 + 8, so it is 88. rgb(255, 136, 0) becomes #FF8800.
What is the hex code for rgb(255, 255, 255)?
#FFFFFF, which is white. Every channel is at its maximum of 255, written FF in hex. Black, with every channel at 0, is #000000.
Can I convert RGB values with a decimal point, such as 127.5?
A hex color channel is a whole number from 0 to 255, so the value has to be rounded first. The converter rounds a percentage channel to the nearest whole number, with halves rounding up, so 50% becomes 128 and then 80 in hex.
How is the alpha channel written in hex?
As a fourth pair of digits at the end. An alpha of 0 to 1 is multiplied by 255 and rounded, so 0.5 becomes 80 and 1 becomes FF. rgba(255, 0, 0, 0.5) converts to the eight-digit code #FF000080.
Does the hex code have to be upper case?
No. #ff8800 and #FF8800 are the same color in CSS and everywhere else. The converter prints upper case because it is easier to compare with design tools, and you can lower-case the result if your style guide asks for it.
