The short answer
RGB and HEX are not different colour systems. Both describe a colour as amounts of red, green and blue light, on the same scale, in the same colour space (sRGB, the default for the web). The only difference is how the numbers are written. rgb(99, 102, 241) uses ordinary decimal numbers; #6366f1 writes the same three numbers in hexadecimal, or base 16. Any colour you can write in one, you can write in the other, with no loss.
So the question is not which is more accurate, but which is more convenient for the job in front of you.
How RGB works
Screens make colour by mixing red, green and blue light. This is additive mixing: full red, green and blue together give white, and none at all gives black. In standard web colour each channel is stored in 8 bits, which allows 256 levels, from 0 to 255. Three channels with 256 levels each give 256 × 256 × 256 = 16,777,216 possible colours.
rgb(255, 0, 0)is pure red.rgb(255, 255, 0)is yellow, because red and green light together look yellow.rgb(128, 128, 128)is a mid grey. Equal values on all three channels always give a neutral grey.
How HEX works
Hexadecimal counts in sixteens. It uses the digits 0 to 9 and then the letters A to F for the values 10 to 15. Two hexadecimal digits can represent 16 × 16 = 256 values, from 00 to FF, which is exactly the range of one 8-bit channel. A HEX colour is just the three channels written one after another as pairs: #RRGGBB.
That neat fit is why HEX became the standard shorthand. Six characters describe any of the 16.7 million colours, and the code is easy to copy, paste and search for in a style sheet.
Converting HEX to RGB by hand
Split the code into three pairs. For each pair, multiply the first digit by 16 and add the second.
Worked example: #6366F1
- Red
63: 6 × 16 + 3 = 99 - Green
66: 6 × 16 + 6 = 102 - Blue
F1: F means 15, so 15 × 16 + 1 = 241
Result: rgb(99, 102, 241).
Converting RGB to HEX by hand
Divide each channel by 16. The whole number is the first digit and the remainder is the second. Convert any value from 10 to 15 into its letter.
Worked example: rgb(255, 165, 0) (the CSS colour orange)
- Red 255: 255 ÷ 16 = 15 remainder 15, so
FF - Green 165: 165 ÷ 16 = 10 remainder 5, so
A5 - Blue 0:
00
Result: #FFA500.
A common slip is forgetting the leading zero. A channel value of 9 is 09, not 9; leave it out and the code shifts every digit after it. Our HEX to RGB converter shows this working for any colour, so you can check your arithmetic.
Shorthand, transparency and case
Three-digit shorthand
If each pair is a repeated digit, CSS lets you write one digit per channel. The browser doubles each one: #F80 means #FF8800, and #FFF means #FFFFFF. Only 4,096 colours (16 × 16 × 16) can be written this way, so most brand colours cannot be shortened.
Transparency (alpha)
Modern CSS accepts an eight-digit HEX code, #RRGGBBAA, where the last pair is opacity. FF is fully opaque and 00 is fully transparent. Alpha in HEX is awkward to read, though: 80 is 128 out of 255, which is about 50.2%, not exactly half. RGB makes this clearer:
- the modern syntax is
rgb(99 102 241 / 50%), with spaces and a slash before the alpha; - the older, comma-separated
rgba(99, 102, 241, 0.5)still works everywhere.
Upper or lower case
HEX is not case-sensitive. #6366F1 and #6366f1 are identical. Pick one style for a project, mainly so that searching your code base finds every use of a colour.
When to use HEX
- Design hand-off and brand guidelines. Most design tools display and accept HEX, and it is compact enough to put in a style guide or a ticket.
- Solid colours in CSS. For an opaque colour that will not change, HEX is short and universally understood.
- Searching and consistency. A single string is easier to search for than three numbers that might be written with or without spaces.
When to use RGB
- Transparency.
rgb(0 0 0 / 40%)is easier to read and adjust than#00000066. - Calculations in code. If JavaScript or a build script is mixing, lightening or animating colours, you need the separate numbers anyway.
- Reusing a colour at different opacities. A common pattern stores the channels in a CSS custom property, such as
--brand-rgb: 99 102 241, and usesrgb(var(--brand-rgb) / 15%)for a tinted background. Newer CSS features such ascolor-mix()can do similar jobs in current browsers.
If you mainly want to make a colour lighter or darker, consider HSL, which describes colour by hue, saturation and lightness. It covers exactly the same colours, but adjusting one number gives a predictable result. The colour picker shows all three formats side by side.
Mistakes to avoid
- Mixing up scales. CSS uses 0 to 255 per channel, but some software and programming libraries use 0 to 1, or percentages.
rgb(1, 0.5, 0)in CSS is almost black, not orange. - Expecting HEX to guarantee an appearance. The code is exact, but the screen is not. Displays differ in brightness, calibration and colour gamut, and features such as night mode shift colours deliberately.
- Ignoring image colour profiles. CSS colours are sRGB. A photo exported in a wider colour space, such as Display P3 or Adobe RGB, may look different next to a CSS colour with the "same" numbers if the profile is stripped or ignored.
- Round-tripping through HSL. HSL values are usually rounded to whole numbers, so converting HEX to HSL and back can move a channel by one or two steps. Keep the HEX or RGB value as the source of truth.
A note on "web-safe" colours
You may still see references to the 216 "web-safe" colours. These come from a time when many computers could show only 256 colours at once. Using only the values 00, 33, 66, 99, CC and FF on each channel (6 × 6 × 6 = 216 combinations) avoided dithering on those displays. Modern screens show millions of colours, so there is no technical need to restrict yourself to this palette today.
Quick reference
| Colour | HEX | RGB | Shorthand? |
|---|---|---|---|
| White | #FFFFFF | rgb(255, 255, 255) | #FFF |
| Black | #000000 | rgb(0, 0, 0) | #000 |
| Mid grey | #808080 | rgb(128, 128, 128) | No |
Orange (CSS orange) | #FFA500 | rgb(255, 165, 0) | No |
| Orange | #FF8800 | rgb(255, 136, 0) | #F80 |
| Indigo-blue (ColourPickr default) | #6366F1 | rgb(99, 102, 241) | No |
Next, read RGB vs CMYK to find out what happens when screen colours are printed, or check your colours for readability with the contrast checker.
Frequently asked questions
Is HEX more accurate than RGB?
No. They are the same values written in different bases, so they describe exactly the same colours.
Can every RGB colour be written as HEX?
Yes, as long as the channels are whole numbers from 0 to 255, which is the case for standard web colours.
Which should I use in CSS?
Either works. HEX is compact for solid colours; RGB is clearer when you need transparency or calculations.