Created
April 12, 2020 18:22
-
-
Save neilgupta/28f5b91780a34a9892d5db5a7cf4297a to your computer and use it in GitHub Desktop.
Consistently convert any string into a color hex
This file contains 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
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