How do you convert RGB to hex in Python?
If you want to use colors in Python, you’ll need to know how to convert between the two color standards: RGB (the one used by monitors, TVs, etc.) and hex (used by HTML). There are a number of libraries that do this for you, but if you don’t have access to them (or just want a refresher), there’s a quick and simple way to do it using only Python. When using RGB to make a hex code, the first thing you need is a tuple of the values of red, green and blue (R, G and B), which is where the rgb() function comes in. It takes three arguments, each between 0 and 255:
(rgb(0, 0, 0)) returns black
(rgb(255, 0, 0)) returns white
So if I had an RGB value of (20, 25, 165), I’d call it like this:
print rgb(20, 25, 165)