Skip to content

Instantly share code, notes, and snippets.

@jurre
Last active December 31, 2015 18:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jurre/8030430 to your computer and use it in GitHub Desktop.
Save jurre/8030430 to your computer and use it in GitHub Desktop.
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"
@dkhgh
Copy link

dkhgh commented Dec 18, 2013

this + hipster ipsum = ....

@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