Convert Hex to RGB Values in Python – Simple Methods

Converting Hex To Rgb

At times the data which we have at our disposal ain’t really the data that we need. It really puts us in an awkward situation where we can’t readily deduce anything with what we have. Conversion of data from one format to another is synonymous with what translation is for humans.

Similar to how we feel comfy and warm to converse in our native lingua, so do the machines! Each of them has its own data format. But, the difference here is these machines only read the data formats for which they are programmed & show zero tolerance unlike us towards other lingua.

This article sets out to explore the ways in which a hexadecimal value can be converted into its RGB equivalent using Python programming. RGB stands for RED-BLUE-GREEN and is one of the predominantly used coding methods for identifying different colours in industries.


Define a Custom function for hex to RGB conversion

While there are no in-built functions in Python that can convert a given hexadecimal value into its RGB equivalent, a combination of those which are readily available would serve the purpose.

Let us bring this combination under a single umbrella – a customized function.

A hexadecimal value would typically have 6 digits from 0 to 9 or A to F or a combination of both. But RGB coding only has 3 entities, each indicating the spectrum of colour in Red, Blue & Green respectively. So, our objective while creating this customized function would be to convert the hexadecimal values into pairs.

To put it in simple terms, we shall take the first two digits of the hexadecimal value and convert it into its equivalent RGB value. The process shall be repeated in such a way that it covers the other 2 pairs of hexadecimal values too. What else shall we summon rather than the for loop in Python to run some iterations? Along with it, the following functions shall also be used for constructing the customization.

So, here we go creating a customized function using def( ) with the for loop incorporated within itself as shown below.

def Hex_RGB(ip):
return tuple(int(ip[i:i+2],16) for i in (0, 2, 4))

There is a lot happening with the above code. Let us break down what they are one after the other!

  • The int( ) function is used to convert a hexadecimal value into its corresponding decimal value by indicating that the base of numbering is 16. One can also observe that the values of ‘i’ are given as 0, 2 & 4.
  • Also, within the int( ) function too, ‘i’ is being incremented by ‘2’. This is due to the necessity to convert the given hexadecimal digits in pairs of 2.
  • When i=0, the first two digits of the hexadecimal value are converted and it occupies the first position in the tuple.
  • Thereafter the next value of ‘i’ is assigned and then incremented to convert the next two hex values.
  • After each conversion, the resultant decimal value shall occupy the next position to the previously converted value in the tuple.
  • Finally, the result shall be displayed in the form of a tuple with three values. Let us now feed in a hexadecimal value and run it through our customized function.
hex = 'D70A51'
print('RGB value for ',hex,' is',Hex_RGB(hex))
Hexadecimal Value Converted To RGB
Hexadecimal Value Converted To RGB

Conclusion

Now that we have reached the end of this article, hope it has elaborated on the conversion of a hexadecimal value into its RGB equivalent using Python. Here’s another article that details the usage of the i0( ) function from the numpy library in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Audere est facere!


Reference: