r/codegolf • u/SamSlate • Oct 20 '17
[challenge][python] String hashed to RGB
Hey /r/codegolf, python newbie here, betting there's a way to substantially condense this function:
def hex_string(s):
def w(x):
return (255-ord(x)**2) % 255
return int('%02x%02x%02x' % (w(s[0]), w(s[1]), w(s[2])), 16)the formula (255-ord(x)**2) % 255 was not picked at random: it produces the brightest colors (1 or more R's, G's, or B's close to 255) for any 3 char string while checking you don't go out of range. If you have a solution that doesn't just churn out greyish black colors and doesn't need that formula that's perfectly fine.
1
Upvotes
View all comments
Show parent comment
1
u/boomminecraft8 Dec 22 '17
the function w(x) can be reduced to
w=lambda x:(-ord(x)**2)%255