Last active
August 29, 2015 14:10
-
-
Save richardjpope/789debb25ae64a373d4c to your computer and use it in GitHub Desktop.
String to colour in python
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 string_to_colour(s): | |
s = s.lower() | |
r = ((ord(s[0])-96.0)/26)*255 | |
g = ((ord(s[int(len(s)/2)])-96.0)/26)*255 | |
b = ((ord(s[len(s)-1])-96.0)/26)*255 | |
return '#%02X%02X%02X' % (r,g,b) |
I managed to golf it down to
import hashlib
import binascii
def string_to_colour(a_string):
three_bytes = hashlib.sha256(a_string.encode("utf-8")).digest()[:3]
return b"#" + binascii.hexlify(three_bytes)
:)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not Python 3 compatible 😄