Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/hexify.py
Last active April 26, 2016 12:38
Show Gist options
  • Select an option

  • Save zeffii/5691091 to your computer and use it in GitHub Desktop.

Select an option

Save zeffii/5691091 to your computer and use it in GitHub Desktop.
# python's hex function
import struct
def hex_to_rgb(rgb_str):
int_tuple = struct.unpack('BBB', bytes.fromhex(rgb_str))
return tuple([val/255 for val in int_tuple])
print(hex_to_rgb('f8ff35'))
def hexify(color):
strip_n_pad = lambda stp: str(stp[2:]).zfill(2)
return "#" + "".join([strip_n_pad(hex(col)) for col in color])
print(hexify((128,128,128)))
# wow! this one makes me feel stupid for doing it any other way.
def rgb_to_hex(rgb):
return '#%02x%02x%02x' % rgb
print(rgb_to_hex((123, 23,34)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment