Class: FictionArt
- Inherits:
-
Object
- Object
- FictionArt
- Defined in:
- lib/fictionArt.rb
Instance Attribute Summary collapse
-
#image_chars ⇒ Object
Returns the value of attribute image_chars.
-
#status ⇒ Object
for tests.
Instance Method Summary collapse
-
#check_possible(text) ⇒ Object
Checking matching characters from character inputted.
-
#createAscii(options = {}) ⇒ Object
Create ascii Art of given fictional character.
-
#initialize(text = "nothing") ⇒ FictionArt
constructor
Constructor function of FictionArt class.
-
#list_all ⇒ Object
Listing all currently possible characters.
-
#unified_rgb_value(number) ⇒ Object
Converting into unified rgb value from 0-255.
Constructor Details
#initialize(text = "nothing") ⇒ FictionArt
Constructor function of FictionArt class
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/fictionArt.rb', line 13 def initialize(text = "nothing") @image_chars ||= ' .~:+=o*x^%#@'.chars.to_a check = Dir.glob("#{__dir__}/../images/#{text}.jpg") if check.length == 0 if text == "nothing" @status = false # for tests elsif text.nil? @status = false # for tests abort("Nothing inputted") else @status = false # for tests puts check_possible(text) end else open(check[0]) { |file| @data = file.read } @status = true # for tests end end |
Instance Attribute Details
#image_chars ⇒ Object
Returns the value of attribute image_chars.
9 10 11 |
# File 'lib/fictionArt.rb', line 9 def image_chars @image_chars end |
#status ⇒ Object
for tests
10 11 12 |
# File 'lib/fictionArt.rb', line 10 def status @status end |
Instance Method Details
#check_possible(text) ⇒ Object
Checking matching characters from character inputted
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/fictionArt.rb', line 108 def check_possible(text) puts "Possible Characters:" list = Dir.glob("#{__dir__}/../images/*.jpg") list.each do |character| character = character[/.*images\/(.*).jpg/,1] if character.downcase.include? text.downcase puts character elsif text.downcase.include? character.downcase puts character end end end |
#createAscii(options = {}) ⇒ Object
Create ascii Art of given fictional character
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/fictionArt.rb', line 38 def createAscii( = {}) if @status == true = {"width" => 90, "color" => true}.merge() img = Magick::Image.from_blob(@data).first width = ["width"] scale = (width.to_f / img.columns) height = ((img.rows * scale) / 2).to_i # puts height if height >= 100 height = 80 end border = "+#{'-' * width}+\n" output = border.dup img.resize!(width, height) color_image = img.dup if ["color"] img = img.quantize(@image_chars.length, Magick::GRAYColorspace).normalize quantum_calc = Magick::QuantumRange / (@image_chars.length - 1) img.view(0, 0, width, height) do |view| height.times do |i| output << '|' width.times do |j| character = @image_chars[view[i][j].red/quantum_calc] if ["color"] pix = color_image.pixel_color(j,i) character = character.color(unified_rgb_value(pix.red), unified_rgb_value(pix.green), unified_rgb_value(pix.blue)) end output << character end output << "|\n" end end output << border return output end end |
#list_all ⇒ Object
Listing all currently possible characters
99 100 101 102 103 104 |
# File 'lib/fictionArt.rb', line 99 def list_all list = Dir.glob("#{__dir__}/../images/*.jpg") list.each do |character| puts character[/.*images\/(.*).jpg/,1] end end |
#unified_rgb_value(number) ⇒ Object
Converting into unified rgb value from 0-255
89 90 91 92 93 94 95 |
# File 'lib/fictionArt.rb', line 89 def unified_rgb_value(number) if defined?(Magick::QuantumDepth) return (Magick::QuantumDepth == 16) ? (number / 256) : number else return (Magick::QuantumRange == 65535) ? (number / 256) : number end end |