Class: AsciiArt

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

Constant Summary collapse

VERSION =
"0.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_to_file) ⇒ AsciiArt

Returns a new instance of AsciiArt.



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

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

  self
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

#image_charsObject



48
49
50
# File 'lib/asciiart.rb', line 48

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

Instance Method Details

#to_ascii_artObject



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

def to_ascii_art
  img = Magick::Image.from_blob(@data).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