Skip to content

Instantly share code, notes, and snippets.

@richardjpope
Last active August 29, 2015 14:10
Show Gist options
  • Save richardjpope/789debb25ae64a373d4c to your computer and use it in GitHub Desktop.
Save richardjpope/789debb25ae64a373d4c to your computer and use it in GitHub Desktop.
String to colour in python
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)
@jabley
Copy link

jabley commented Nov 23, 2014

Needs tests 😜

@yamatt
Copy link

yamatt commented Nov 23, 2014

Not Python 3 compatible 😄

@calpaterson
Copy link

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)

@richardjpope
Copy link
Author

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment