[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Is there some type of hex codes to rgb converter or something?



Lamonte Harris wrote:
I like using HTML hex, is there a library or something on pygame or python it self to convert hex to rgb
HTML hex colors are defined as such:
color = "#DEFFC3"
I.E. a pound sign, then 2 hex digits for red, 2 for green, and 2 for blue.
This is very easy to convert between.
First let's get rid of the # sign
color = color[1:]
Next let's separate these into 3 different strings.
red = color[0:2]
green = color[2:4]
blue = color[4:6]

Next let's convert these all to integers
red = int(red, 16)
green = int(green, 16)
blue = int(blue, 16)

I'll leave it as an exercise for the reader to simplify this (I.E. I have food on the stove and I have to go take it off.)
HTH,
-Luke