How this calculator works
An sRGB colour is stored as three channels, each an 8-bit number from 0 to 255. A hex code writes each channel as two base-16 digits in the order red, green and blue. Three-digit shorthand repeats each digit in both positions of its pair, so it can only represent channels whose value is a multiple of 17, such as 0, 17, 34 up to 255. This page always computes the six-digit form and offers the short form only when it would expand back to exactly the same channels.
Formula
Each channel is converted from decimal to two hexadecimal digits. For example 255 = FF, 165 = A5 and 0 = 00, giving FFA500. A channel c can be written as one digit only when c is divisible by 17; the digit is then c / 17, so 255 becomes F and 0 becomes 0.
Worked example
Red 255, green 165 and blue 0 give FFA500, which is the CSS orange used for the default. Red 0, green 128 and blue 0 give 008000. Red 255, green 0 and blue 0 give F00 in three-digit notation and FF0000 in six-digit notation.
Advertisement
Frequently asked questions
Why is only some of the time a three-digit code possible?
Three-digit hex repeats each digit, so each channel must be 00, 11, 22 and so on in hex, which means a decimal value divisible by 17. Most colours need all six digits to keep their exact channel values.
Should the code include the number sign?
CSS requires the number sign, and this page includes it. In HTML attributes and many graphics programs the same code is often written without it.
Are channel values required to be whole numbers?
Yes on this page. An 8-bit sRGB channel is a whole number between 0 and 255. A fractional value would have to be rounded, and rounding changes the colour, so it is rejected instead.