Skip to content

Instantly share code, notes, and snippets.

@neilgupta
Created April 12, 2020 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neilgupta/28f5b91780a34a9892d5db5a7cf4297a to your computer and use it in GitHub Desktop.
Save neilgupta/28f5b91780a34a9892d5db5a7cf4297a to your computer and use it in GitHub Desktop.
Consistently convert any string into a color hex
def colorize(input):
# convert string to base36 and then scientific notation
a = '%E' % float(int(input, 36))
scinot = a.split('E')[0].rstrip('0').rstrip('.') + 'E' + a.split('E')[1]
# convert decimal portion of scientific notation back to hex and pad to 6 values
return "#" + "{0:x}".format(int(scinot[2:-5]) & 0xFFFFFF)[-6:].zfill(6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment