-
-
Save zeffii/5691091 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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