Permalink
Browse files

Gem-ified

  • Loading branch information...
0 parents commit 4475c24b57dfa17ca7edbcd6bcc2567e29df5ffe @dira committed Mar 19, 2012
Showing with 184 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +4 −0 Gemfile
  3. +15 −0 README.mkd
  4. +1 −0 Rakefile
  5. +12 −0 lib/monster_id.rb
  6. +66 −0 lib/monster_id/monster.rb
  7. BIN lib/monster_id/parts/arms_1.png
  8. BIN lib/monster_id/parts/arms_2.png
  9. BIN lib/monster_id/parts/arms_3.png
  10. BIN lib/monster_id/parts/arms_4.png
  11. BIN lib/monster_id/parts/arms_5.png
  12. BIN lib/monster_id/parts/body_1.png
  13. BIN lib/monster_id/parts/body_10.png
  14. BIN lib/monster_id/parts/body_11.png
  15. BIN lib/monster_id/parts/body_12.png
  16. BIN lib/monster_id/parts/body_13.png
  17. BIN lib/monster_id/parts/body_14.png
  18. BIN lib/monster_id/parts/body_15.png
  19. BIN lib/monster_id/parts/body_2.png
  20. BIN lib/monster_id/parts/body_3.png
  21. BIN lib/monster_id/parts/body_4.png
  22. BIN lib/monster_id/parts/body_5.png
  23. BIN lib/monster_id/parts/body_6.png
  24. BIN lib/monster_id/parts/body_7.png
  25. BIN lib/monster_id/parts/body_8.png
  26. BIN lib/monster_id/parts/body_9.png
  27. BIN lib/monster_id/parts/eyes_1.png
  28. BIN lib/monster_id/parts/eyes_10.png
  29. BIN lib/monster_id/parts/eyes_11.png
  30. BIN lib/monster_id/parts/eyes_12.png
  31. BIN lib/monster_id/parts/eyes_13.png
  32. BIN lib/monster_id/parts/eyes_14.png
  33. BIN lib/monster_id/parts/eyes_15.png
  34. BIN lib/monster_id/parts/eyes_2.png
  35. BIN lib/monster_id/parts/eyes_3.png
  36. BIN lib/monster_id/parts/eyes_4.png
  37. BIN lib/monster_id/parts/eyes_5.png
  38. BIN lib/monster_id/parts/eyes_6.png
  39. BIN lib/monster_id/parts/eyes_7.png
  40. BIN lib/monster_id/parts/eyes_8.png
  41. BIN lib/monster_id/parts/eyes_9.png
  42. BIN lib/monster_id/parts/hair_1.png
  43. BIN lib/monster_id/parts/hair_2.png
  44. BIN lib/monster_id/parts/hair_3.png
  45. BIN lib/monster_id/parts/hair_4.png
  46. BIN lib/monster_id/parts/hair_5.png
  47. +57 −0 lib/monster_id/parts/image.info
  48. BIN lib/monster_id/parts/legs_1.png
  49. BIN lib/monster_id/parts/legs_2.png
  50. BIN lib/monster_id/parts/legs_3.png
  51. BIN lib/monster_id/parts/legs_4.png
  52. BIN lib/monster_id/parts/legs_5.png
  53. BIN lib/monster_id/parts/mouth_1.png
  54. BIN lib/monster_id/parts/mouth_10.png
  55. BIN lib/monster_id/parts/mouth_2.png
  56. BIN lib/monster_id/parts/mouth_3.png
  57. BIN lib/monster_id/parts/mouth_4.png
  58. BIN lib/monster_id/parts/mouth_5.png
  59. BIN lib/monster_id/parts/mouth_6.png
  60. BIN lib/monster_id/parts/mouth_7.png
  61. BIN lib/monster_id/parts/mouth_8.png
  62. BIN lib/monster_id/parts/mouth_9.png
  63. +3 −0 lib/monster_id/version.rb
  64. +22 −0 monster_id.gemspec
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in monster_id.gemspec
+gemspec
@@ -0,0 +1,15 @@
+A Ruby port of [monsterid](http://www.splitbrain.org/go/monsterid).
+
+## Usage
+
+Save a monster in a file:
+
+ MonsterID.new('your_seed').save('monster.png')
+
+ MonsterID.new('a_seed').to_data_uri
+
+ MonsterID.new('your_seed').to_s # raw png
+
+### License
+
+License: [CC BY 3.0](http://creativecommons.org/licenses/by/3.0/)
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
@@ -0,0 +1,12 @@
+require "monster_id/version"
+require "monster_id/monster"
+
+##
+# A Ruby port of http://www.splitbrain.org/projects/monsterid
+module MonsterId
+
+ def self.generate(seed, size=nil)
+ Monster.new(seed, size)
+ end
+
+end
@@ -0,0 +1,66 @@
+require 'chunky_png'
+require 'digest'
+require 'base64'
+
+module MonsterId
+
+ class Monster
+ SIZE = 120
+
+ 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
+
+ def to_s
+ @image.to_datastream.to_s
+ end
+
+ def inspect
+ ''
+ end
+
+ def to_data_uri
+ 'data:image/png;base64,' + Base64.encode64(@image.to_s).gsub(/\n/, '')
+ end
+
+ def save(path)
+ @image.save(path)
+ end
+ end
+
+end
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
@@ -0,0 +1,57 @@
+image 120x120 /var/www/splitbrain/_static/monsterid/parts/monsterparts.xcf
+layer 120x120 0,0 Mouth_10.png
+layer 120x120 0,0 Mouth_9.png
+layer 120x120 0,0 Mouth_8.png
+layer 120x120 0,0 Mouth_7.png
+layer 120x120 0,0 Mouth_6.png
+layer 120x120 0,0 Mouth_5.png
+layer 120x120 0,0 Mouth_4.png
+layer 120x120 0,0 Mouth_3.png
+layer 120x120 0,0 Mouth_2.png
+layer 120x120 0,0 Mouth_1.png
+layer 120x120 0,0 Eyes_15.png
+layer 120x120 0,0 Eyes_14.png
+layer 120x120 0,0 Eyes_13.png
+layer 120x120 0,0 Eyes_12.png
+layer 120x120 0,0 Eyes_11.png
+layer 120x120 0,0 Eyes_10.png
+layer 120x120 0,0 Eyes_9.png
+layer 120x120 0,0 Eyes_8.png
+layer 120x120 0,0 Eyes_7.png
+layer 120x120 0,0 Eyes_6.png
+layer 120x120 0,0 Eyes_5.png
+layer 120x120 0,0 Eyes_4.png
+layer 120x120 0,0 Eyes_3.png
+layer 120x120 0,0 Eyes_2.png
+layer 120x120 0,0 Eyes_1.png
+layer 120x120 0,0 Body_15.png
+layer 120x120 0,0 Body_14.png
+layer 120x120 0,0 Body_13.png
+layer 120x120 0,0 Body_12.png
+layer 120x120 0,0 Body_11.png
+layer 120x120 0,0 Body_10.png
+layer 120x120 0,0 Body_9.png
+layer 120x120 0,0 Body_8.png
+layer 120x120 0,0 Body_7.png
+layer 120x120 0,0 Body_6.png
+layer 120x120 0,0 Body_5.png
+layer 120x120 0,0 Body_4.png
+layer 120x120 0,0 Body_3.png
+layer 120x120 0,0 Body_2.png
+layer 120x120 0,0 Body_1.png
+layer 120x120 0,0 Arms_5.png
+layer 120x120 0,0 Arms_4.png
+layer 120x120 0,0 Arms_3.png
+layer 120x120 0,0 Arms_2.png
+layer 120x120 0,0 Arms_1.png
+layer 120x120 0,0 Hair_5.png
+layer 120x120 0,0 Hair_4.png
+layer 120x120 0,0 Hair_3.png
+layer 120x120 0,0 Hair_2.png
+layer 120x120 0,0 Hair_1.png
+layer 120x120 0,0 Legs_5.png
+layer 120x120 0,0 Legs_4.png
+layer 120x120 0,0 Legs_3.png
+layer 120x120 0,0 Legs_2.png
+layer 120x120 0,0 Legs_1.png
+layer 120x120 0,0 Background
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
@@ -0,0 +1,3 @@
+module MonsterId
+ VERSION = "0.0.1"
+end
@@ -0,0 +1,22 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "monster_id/version"
+
+Gem::Specification.new do |s|
+ s.name = "monster_id"
+ s.version = MonsterId::VERSION
+ s.authors = ["Irina Dumitrascu"]
+ s.email = ["me@dira.ro"]
+ s.homepage = ""
+ s.summary = %q{Generate random monster images.}
+ s.description = %q{A Ruby port of the MonsterID PHP script, using ChunkyPNG. http://www.splitbrain.org/projects/monsterid}
+
+ s.rubyforge_project = "monster_id"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_runtime_dependency "oily_png"
+end

0 comments on commit 4475c24

Please sign in to comment.