From three numbers to one code
RGB describes a colour as three amounts of light (red, green and blue) each from 0 to 255. A hex code says the same thing more compactly by writing each of those numbers in base 16, where two digits cover exactly 0 to 255. So rgb(255, 87, 51) and #ff5733 are not merely similar, they’re the same colour written two ways, and converting between them loses nothing at all.
Doing it by hand
Take each channel separately. Divide the number by 16: the whole part is the first digit and the remainder is the second, with 10 to 15 written as a to f. For 87, that’s 5 remainder 7, so 57. For 51, it’s 3 remainder 3, so 33. For 255, it’s 15 remainder 15, so ff. Write the three pairs in order after a # and you have the code. The only thing that catches people out is a channel below 16, which still needs both digits: 5 becomes 05, not 5.
The short form, and when it isn’t available
A 3-digit code is shorthand for a 6-digit one where each digit is doubled: #0a7 means #00aa77. That makes it available for only a small fraction of colours: both digits of every channel have to match. #ffaa77 shortens to #fa7; #ff5733 can’t shorten at all, because 57 isn’t a doubled digit. The tool shows the short form only when it’s genuinely equivalent, so there’s no risk of shipping a colour that’s slightly off.
Adding opacity
Two more digits on the end of a hex code carry alpha, on the same 0-to-255 scale as the colour channels. To work one out by hand, multiply the decimal alpha by 255 and convert: 0.5 becomes 128, which is 80, so half-transparent #ff5733 is #ff573380. A few are worth memorising: ff solid, cc 80%, bf 75%, 80 roughly half, 40 a quarter, 00 invisible. Note that CSS puts alpha last while Android puts it first, so the same eight digits can mean different colours on different platforms.
Out-of-range and fractional values
Channels outside 0 to 255 are clamped rather than rejected, matching what a browser does with an impossible value: 300 becomes ff and −20 becomes 00. Fractional channels, which can appear when a colour has been scaled or blended, are rounded to the nearest whole number, because a hex pair has no way to store a fraction. The tool shows you the result of those adjustments rather than hiding them, so you can see whether the colour you got is the colour you meant.
Converted on your device
There’s no server involved. The conversion is a few lines of arithmetic that run in the page as you type, so nothing you enter is sent, stored or logged anywhere.