Skip to content

Instantly share code, notes, and snippets.

@jurre
Last active Dec 31, 2015
Embed
What would you like to do?
string to pastel color
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"
@jejote
Copy link

jejote commented Dec 19, 2013

Happy Unicorn-text!

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