Class: AsciiRenderer::Html

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

Instance Method Summary collapse

Constructor Details

#initialize(img) ⇒ Html

Returns a new instance of Html.



3
4
5
6
# File 'lib/asciiart/html.rb', line 3

def initialize(img)
  @img = img
  @quantum_calc  = Magick::QuantumRange / Magick::QuantumPixel.to_i
end

Instance Method Details

#image_charsObject



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

def image_chars
  @image_chars ||= " .~:+=o*x^%\#@$WQ".chars.to_a.map{ |char| char == " " ? "&nbsp" : char}
end

#pixel_to_char(view, args = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/asciiart/html.rb', line 32

def pixel_to_char(view, args = {})
  pixel = view[args[:i]][args[:j]]
  character = image_chars[pixel.red/@quantum_calc]

  if (args[:color])

    pix = @color_img.pixel_color(args[:j],args[:i])
    color_string = "color: #{pix.to_color( Magick::AllCompliance,false,8, true)};"
  else
    color_string = ""
  end

  html_char(character, color_string)
end

#to_ascii_art(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/asciiart/html.rb', line 8

def to_ascii_art(options = {})
  width       = options[:width]
  scale       = (width.to_f / @img.columns)
  height      = ((@img.rows * scale) / 2).to_i
  
  img           = @img.resize(width, height)
  @color_img    = img.dup if options[:color]
  img           = img.quantize(image_chars.length, Magick::GRAYColorspace).normalize
  border = "+#{'-' * width}+<br/>"
  border = html_char(border)

  output = border.dup

  img.view(0, 0, width, height) do |view|
    height.times do |i|
      output << '|'
      width.times { |j| output << pixel_to_char(view, j: j, i: i, color: options[:color]) }
      output << "|<br/>"
    end
  end
  output + border
end