Skip to content

Instantly share code, notes, and snippets.

@senorprogrammer
Created April 3, 2011 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save senorprogrammer/900031 to your computer and use it in GitHub Desktop.
Save senorprogrammer/900031 to your computer and use it in GitHub Desktop.
Converts strings to hex codes in Ruby
class String
def hexcode
hash = 0
self.each_byte do |chr|
hash = chr + (( hash << 5 ) - hash )
end
code = ((hash>>24)&0xFF).to_s(16) + ((hash>>16)&0xFF).to_s(16) + ((hash>>8)&0xFF).to_s(16) + (hash&0xFF).to_s(16)
return code[0..5]
end
end
@senorprogrammer
Copy link
Author

This is a direct ruby port of this JS code that purports to be a port from Java: http://stackoverflow.com/questions/3426404/create-a-hexadecimal-colour-based-on-a-string-with-jquery-javascript

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