Skip to content

Instantly share code, notes, and snippets.

@KerryHalupka
Created July 25, 2020 03:10
Show Gist options
  • Save KerryHalupka/1fd31cd6cb3b1b5d7bef8c3ccb77d40c to your computer and use it in GitHub Desktop.
Save KerryHalupka/1fd31cd6cb3b1b5d7bef8c3ccb77d40c to your computer and use it in GitHub Desktop.
Functions to convert between different color formats
def hex_to_rgb(value):
'''
Converts hex to rgb colours
value: string of 6 characters representing a hex colour.
Returns: list length 3 of RGB values'''
value = value.strip("#") # removes hash symbol if present
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
def rgb_to_dec(value):
'''
Converts rgb to decimal colours (i.e. divides each value by 256)
value: list (length 3) of RGB values
Returns: list (length 3) of decimal values'''
return [v/256 for v in value]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment