Last active
December 31, 2015 18:59
-
-
Save jurre/8030430 to your computer and use it in GitHub Desktop.
string to pastel color
This file contains hidden or 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 pastel_color_for_string(string) | |
start_color = 128 | |
total_offset = 64 | |
hex_value = [ | |
0, | |
string_to_integer_hash(string) % total_offset, | |
string_to_integer_hash(string.reverse) % total_offset, | |
total_offset | |
].sort.each_cons(2).map do |a, b| | |
"%02x" % (start_color + b - a) | |
end.join | |
"#" + hex_value | |
end | |
def string_to_integer_hash(string) | |
[Digest::SHA1.hexdigest(string)].pack("H*").unpack("l>").first | |
end | |
# irb(main):060:0> pastel_color_for_string("swag") | |
# => "#ac8f85" | |
# irb(main):061:0> pastel_color_for_string("yoloswag") | |
# => "#8a8aac" | |
# irb(main):063:0> pastel_color_for_string("#CHURCH") | |
# => "#8297a7" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this + hipster ipsum = ....