Created
April 3, 2011 00:07
-
-
Save senorprogrammer/900031 to your computer and use it in GitHub Desktop.
Converts strings to hex codes in Ruby
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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