Skip to content

Instantly share code, notes, and snippets.

@pythonjunkie
Created August 29, 2012 18:56
Show Gist options
  • Select an option

  • Save pythonjunkie/3517137 to your computer and use it in GitHub Desktop.

Select an option

Save pythonjunkie/3517137 to your computer and use it in GitHub Desktop.
Convert hex value to rgb
>>> def hex_to_rgb(value):
... lv = len(value)
... return tuple(int(value[i:i+lv/3], 16) for i in range(0, lv, lv/3))
...
>>>
>>> hex_to_rgb("FFFFFF")
(255, 255, 255)
>>> def rgb_to_hex(rgb):
... return '%02x%02x%02x' % rgb
...
>>> rgb_to_hex((255, 255, 255))
'ffffff'
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment