Module: Colorizer
Overview
Instance Method Summary collapse
Instance Method Details
#colorize(object) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/colorizer.rb', line 5 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 |
#colorize_similarly(object, saturation, lightness) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/colorizer.rb', line 13 def colorize_similarly(object, saturation, lightness) # NOTE: We used to default to using XXhash and had it listed as a hard dependency for this gem. # In an effort to slim down our dependencies we're making it so that this can work without XXhash. # But since changing the way we calculate the seed _also_ changes the color that comes out the # other end, we're making it so that people can add `gem "xxhash"` to their `Gemfile` to preserve # the colors that were previously being generated. seed = if Object.const_defined?(:XXhash) XXhash.xxh64(object) else Digest::MD5.hexdigest(object).to_i(16) end rnd = ((seed * 7) % 100) * 0.01 hsl_to_rgb(rnd, saturation, lightness) end |