Class: AsciiArt

Inherits:
Object
  • Object
show all
Defined in:
lib/asciiart.rb,
lib/asciiart/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_to_file) ⇒ AsciiArt

Returns a new instance of AsciiArt.



9
10
11
# File 'lib/asciiart.rb', line 9

def initialize(path_to_file)
  @file = File.new(path_to_file, "r")
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



6
7
8
# File 'lib/asciiart.rb', line 6

def file
  @file
end

#image_charsObject



46
47
48
# File 'lib/asciiart.rb', line 46

def image_chars
  @image_chars ||= [" ", ".", "~", ":", "+", "=", "o", "*", "x", "^", "%", "#", "@", "$", "M", "W"]
end

Instance Method Details

#to_ascii_artObject



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
# File 'lib/asciiart.rb', line 13

def to_ascii_art

  img = Magick::Image.read(file).first

  new_width   = 100
  scale       = (new_width.to_f / img.columns)
  new_height  = ((img.rows * scale) / 2).to_i

  img.resize!(new_width, new_height)

  img = img.quantize(image_chars.length, Magick::GRAYColorspace)
  img = img.normalize

  quantum_calc = Magick::QuantumRange / Magick::QuantumPixel.to_i
  
  output = ""
  
  border = '+' + ('-' * new_width) + '+' + "\n"
  output << border  

  img.view(0, 0, new_width, new_height) do |view|
    new_height.times do |i|
    output << '|'
    new_width.times { |j| output << image_chars[view[i][j].red/quantum_calc] }
    output << "|\n"
  end
  end
  
  output << border

  output
end