Convert Hex To RGB In Python
Looking for a way to convert Hex to RGB in python then you are at the right place, today in this python tutorial I will show you the python program to convert Hex to RGB color code so follow me till the end.
Hex is a color code format which uses hexadecimal values to indicate a color, for example #fffff will result in white color.
RGB is another color code which stands for red, green and blue. They each have a unique value like this (255,255,255) which results in white color.
I hope you understood what RGB and Hex are. Now let’s see how to convert a Hex color code to RGB color code in python.
Python Code To Convert Hex To RGB Color
hex = input('Enter HEX value: ').lstrip('#') print('RGB value =', tuple(int(hex[i:i+2], 16) for i in (0, 2, 4)))
Above is the python program to convert Hex to RGB. You can run this code and see the result. To run this program you need to have python installed, if you don’t have, then read this: Install and setup python.
Follow this steps to run this program:
- First, create a new folder for this program.
- Open it in a code editor of your choice.
- Create a python file with .py extension.
- Copy and paste the above code in the file.
- Open a command prompt at the program folder.
- To run, use this command, python filename.py.
Follow all the above steps if you don’t know how to run a python program. You can also run this program by using this online python compiler. After running the program, you will see the Hex code to an RGB Color Code like below.
Enter HEX value: #eeeeee RGB value = (238, 238, 238)
As you can see, we have successfully converted Hex Color to RGB in python. I hope you ran this program successfully.
Want to know how to convert RGB to Hex color code then read this tutorial: Convert RGB To Hex In Python.
This was the tutorial on converting Hex to RGB in python. I hope you found this tutorial helpful and useful. Do share this tutorial with your friends who might need this or find it helpful.
Here are some more python programs you will find helpful:
- Convert GPX to CSV file in python.
- Convert IPYNB file to python file.
- Convert Wav to mp3 format in python.
- Convert Miles to feet in python.
I hope you found what you were looking for from this python tutorial, and if you want more python tutorials like this, do join our Telegram channel to get updated.
Thanks for reading, have a nice day 🙂