halostatue / color

Color tools for Ruby.
Other
133 stars 41 forks source link

Generate color based on the given string #23

Closed stereobooster closed 6 years ago

stereobooster commented 9 years ago

Basically port https://github.com/zenozeng/color-hash from JS to ruby

stereobooster commented 9 years ago

Or other solution found here http://makandracards.com/makandra/24449-hash-any-ruby-object-into-an-rgb-color

    def colorize(object)
      # Inspired by Jeremy Ruten (http://stackoverflow.com/questions/1698318/ruby-generate-a-random-hex-color)
      hash = object.hash # hash an object, returns a Fixnum
      trimmed_hash = hash & 0xffffff # trim the hash to the size of 6 hex digits (& is bit-wise AND)
      hex_code = "%06x" % trimmed_hash # format as at least 6 hex digits, pad with zeros
      "##{hex_code}"
    end
halostatue commented 9 years ago

Interesting idea. I’ll have to see how I want to do this. As I understand this, this would be the colour equivalent of an identicon?

stereobooster commented 9 years ago

Yes. I want to generate avatars like mobile gmail does. Initials plus color.