What are Hex Colors?

Raffaello Ippolito
4 min readAug 13, 2023

--

Anyone who has ever been involved in web development or used a graphics software has likely seen strange alphanumeric codes as color identifiers, but where do they come from? Why is #0000 black? Why is #FF0000 red? Why is #007BA7 cerulean?

In the previous article “Teaching to a computer what pictures are” we saw what an image is, in order to do so, we also had to define what a color is, using the RGB format as an example. Hex codes are nothing more than another way to represent colors in RGB format.

We know that a color can be seen as a combination of amounts of red, green and blue, if we look at it this way cerulean is defined by the triplet (000,123,167), so where does the code 007BA7 come from? Many will say “it is a hexadecimal number!” well not really, in fact it is a triplet of hexadecimal numbers (00,7B,A7). Some people may have already figured it out: the three numbers were simply converted from decimal to hexadecimal and lined up one after the other in a single string. But let’s take a step back.

What is a hexadecimal number?

The numbers we are used to are called decimal or base 10 numbers. The fact that a number system is base 10 means that it uses 10 digits, which in the case of the decimal number system are : 0,1,2,3,4,5,6,7,8 and 9. When we want to express a number greater than 9 we use multiple digits by placing them side by side, the digit in this case will be a multiplier of powers of the base of the number system, let us give an example:
Consider the number 255, in school they explained to us that it consists of 2 hundreds, 5 tens and 5 units, this wording is just a way of saying that 255 = 2*10² + 5*10¹ + 5*10⁰, that’s all.

Okay, if we have understood how our number system works, then we are ready to understand the others as well since the working principle is the same. Let’s take a relatively simple one: the binary or base 2 system. We have only two digits: 0 and 1, as before if we want to express a number greater than 1 we use more digits by placing them side by side, the digit in this case will be a multiplier of powers of the base of the number system, let’s give us an example:
Consider the number 11111111 = 1*2⁷ + 1*2⁶ + 1*2⁵+ 1*2⁴+ 1*2³+ 1*2²+ 1*2¹+ 1*2⁰ exactly as in the previous case. This number was not chosen at random 11111111 in binary corresponds to 255 in decimal, however different these two numbers represent the same quantity, only in different systems.

We can see that by using a smaller base, the number of digits needed to write the same number is increased, this is because the side-by-side of the digits depends on the powers of the base, in particular the largest number that can be written in a base n system with k digits is nᵏ-1 and in that case the k digits will all be equal to the digit with the highest value.

Ok then if I increase the number of digits I can get the same amount using fewer digits, let’s try this using a 16 digit number system, we’ll use as digits: 1,2,3,4,5,6,7,8,9,A,B,C,D,E and F this is the hexadecimal system! And how do we write 255 in hexadecimal? FF! in fact 255 is the largest quantity that can be written with 2 digits in hexadecimal since 1*6²-1 = 255.

Why the hexadecimal?

Okay then a cerulean color encoded in RGB as (000,123,167) becomes (00,7B,A7) and thus 007BA7, but why do we do that? If the goal was just to have a single string it would be enough to use 000123167 there must be something else and that something else is optimization.

Computers reason in binary by means of bits, bits however are organized in bytes which are sets of 8 bits, we would then like to use a byte to encode a number, with 8 bits (i.e. 8 digits in binary) we can encode the numbers from 0 to 255, to write this number in decimal we need 3 digits but with 3 digits in decimal the largest number that can be encoded is 999, which in binary is 1111100111 which requires 10 digits to be encoded i.e. 10 bits. This incompatibility leads to a redundant allocation of memory, meaning that more memory is allocated than necessary, conversion from binary to hexadecimal does not bring this problem, in fact the maximum number that can be written in hexadecimal with two digits is 255, the same as can be written in binary with 8 digits.

Conclusions

The reasons leading to the use of the hexadecimal format are purely technical and basically concern saving memory. In cases like these, memory saving is crucial, just think that an image in 1080p (Full HD) color is represented by a set of 6,220,800 colors, a small saving on each of them translates into an important saving on the whole image.

Beyond the technical aspects, knowing where the HEX code comes from allows us to get an idea of the color from the code alone. Of the cerulean 007BA7 for example, one can immediately see a total absence of red and a predominance of blue.

--

--

Raffaello Ippolito

Italian software developer and data analytics student. Graduated in Mathematics for Engineering talking about Big Data and Image Processing