Module: RubyIdenticon
- Defined in:
- lib/ruby_identicon.rb,
lib/ruby_identicon/version.rb
Constant Summary collapse
- DEFAULT_PARAMETERS =
the default paramters used when creating identicons
background_color: (Integer, default 0) the background color of the identicon in rgba notation (e.g. xffffffff for white) border_size: (Integer, default 35) the size in pixels to leave as an empty border around the identicon image grid_size: (Integer, default 7) the number of rows and columns in the identicon, minimum 4, maximum 9 square_size: (Integer, default 50) the size in pixels of each square that makes up the identicon key: (String) a 16 byte key used by siphash when calculating the hash value (see note below)
varying the key ensures uniqueness of an identicon for a given title, it is assumed desirable for different applications to use a different key.
{ border_size: 35, square_size: 50, grid_size: 7, background_color: ChunkyPNG::Color::TRANSPARENT, key: "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF" }
- VERSION =
"0.0.2"
Class Method Summary collapse
-
.create(title, options = {}) ⇒ Object
create an identicon png and return it as a binary string.
-
.create_and_save(title, filename, options = {}) ⇒ Object
create an identicon png and save it to the given filename.
Class Method Details
.create(title, options = {}) ⇒ Object
create an identicon png and return it as a binary string
Example:
>> RubyIdenticon.create("identicons are great!")
=> binary blob (String)
Arguments:
title: (String)
options: (Hash)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/ruby_identicon.rb', line 99 def self.create(title, = {}) = DEFAULT_PARAMETERS.merge() raise "title cannot be nil" if title == nil raise "key is nil or less than 16 bytes" if [:key] == nil || [:key].length < 16 raise "grid_size must be between 4 and 9" if [:grid_size] < 4 || [:grid_size] > 9 raise "invalid border size" if [:border_size] < 0 raise "invalid square size" if [:square_size] < 0 hash = SipHash.digest([:key], title) png = ChunkyPNG::Image.new(([:border_size] * 2) + ([:square_size] * [:grid_size]), ([:border_size] * 2) + ([:square_size] * [:grid_size]), [:background_color]) # set the foreground color by using the first three bytes of the hash value color = ChunkyPNG::Color.rgba((hash & 0xff), ((hash >> 8) & 0xff), ((hash >> 16) & 0xff), 0xff) # remove the first three bytes that were used for the foreground color hash >>= 24 sqx = sqy = 0 for i in 0..([:grid_size] * (([:grid_size]+1) / 2)) if hash & 1 == 1 x = [:border_size] + (sqx * [:square_size]) y = [:border_size] + (sqy * [:square_size]) # left hand side png.rect(x, y, x + [:square_size], y + [:square_size], color, color) # mirror right hand side x = [:border_size] + (([:grid_size] - 1 - sqx) * [:square_size]) png.rect(x, y, x + [:square_size], y + [:square_size], color, color) end hash >>= 1 sqy += 1 if sqy == [:grid_size] sqy = 0 sqx += 1 end end png.to_blob :color_mode => ChunkyPNG::COLOR_INDEXED end |
.create_and_save(title, filename, options = {}) ⇒ Object
create an identicon png and save it to the given filename
Example:
>> RubyIdenticon.create_and_save("identicons are great!", "test_identicon.png")
=> result (Boolean)
Arguments:
title: (String)
filename: (String)
options: (Hash)
80 81 82 83 84 85 86 87 |
# File 'lib/ruby_identicon.rb', line 80 def self.create_and_save(title, filename, = {}) raise "filename cannot be nil" if filename == nil blob = create(title, ) return false if blob == nil File.open(filename, "wb") do |f| f.write(blob) end end |