1

i using gtk3 i found that it use rgba for representing color, but the (red,green,blue,alpha) are not integer between 0-255 but floating point number between 0-1.0, so i don't know how to convert from rgba to hex and vice-versa

i have tried this code but its seem to not work :

def convert_to_hex(rgba_color) :
red = str(hex(int(rgba_color.red*255)))[2:].capitalize()
green = str(hex(int(rgba_color.green*255)))[2:].capitalize()
blue = str(hex(int(rgba_color.blue*255)))[2:].capitalize()

return '0x' + red + green + blue
KarimS
  • 3,812
  • 9
  • 41
  • 64
  • "its seem to not work" - could you add more information? Do you get errors (provide full traceback)? Do you get unexpected outputs (provide inputs and expected and actual outputs)? – jonrsharpe Feb 22 '14 at 13:45
  • i receive a good hex number, but when i try the hex color online, it give me another color – KarimS Feb 22 '14 at 13:55
  • Then please provide example inputs and outputs – jonrsharpe Feb 22 '14 at 13:56

1 Answers1

6

Assuming the problem is that the number should have leading zeros when they are only 1 digit. Here is a solution for that.

def convert_to_hex(rgba_color) :
    red = int(rgba_color.red*255)
    green = int(rgba_color.green*255)
    blue = int(rgba_color.blue*255)
    return '0x{r:02x}{g:02x}{b:02x}'.format(r=red,g=green,b=blue)
M4rtini
  • 13,186
  • 4
  • 35
  • 42
  • its still not working, its give me a hex number, but when i try the hex online it give another color – KarimS Feb 22 '14 at 13:55