Class: MonsterId::Monster
- Inherits:
-
Object
- Object
- MonsterId::Monster
- Defined in:
- lib/monster_id/monster.rb
Constant Summary collapse
- SIZE =
120
Instance Method Summary collapse
-
#initialize(seed, size = nil) ⇒ Monster
constructor
A new instance of Monster.
- #inspect ⇒ Object
- #save(path) ⇒ Object
- #to_data_uri ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(seed, size = nil) ⇒ Monster
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/monster_id/monster.rb', line 10 def initialize(seed, size=nil) srand Digest::MD5.hexdigest(seed.to_s).to_i(16) # throw the dice for body parts parts = { legs: rand(5), hair: rand(5), arms: rand(5), body: rand(15), eyes: rand(15), mouth: rand(10), } @image = ChunkyPNG::Image.new SIZE, SIZE, ChunkyPNG::Color::TRANSPARENT parts.each do |name, number| path = File.join File.dirname(__FILE__), 'parts', "#{name}_#{number + 1}.png" part = ChunkyPNG::Image.from_file path if name == :body # random body color w, h = part.width, part.height r, g, b = rand(215) + 20, rand(215) + 20, rand(215) + 20 body_color = r * 256 * 256 * 256 + g * 256 * 256 + b * 256 + 255 part.pixels.each_with_index do |color, i| unless color == 0 || color == 255 part[i % w, (i / w).to_i] = body_color end end end @image.compose!(part, 0, 0) end @image.resample_bilinear!(size, size) unless size == nil or size == SIZE srand end |
Instance Method Details
#inspect ⇒ Object
53 54 55 |
# File 'lib/monster_id/monster.rb', line 53 def inspect '' end |
#save(path) ⇒ Object
61 62 63 |
# File 'lib/monster_id/monster.rb', line 61 def save(path) @image.save(path) end |
#to_data_uri ⇒ Object
57 58 59 |
# File 'lib/monster_id/monster.rb', line 57 def to_data_uri 'data:image/png;base64,' + Base64.encode64(@image.to_s).gsub(/\n/, '') end |
#to_s ⇒ Object
49 50 51 |
# File 'lib/monster_id/monster.rb', line 49 def to_s @image.to_datastream.to_s end |