Class: Tortoise::Presenter
- Inherits:
-
Object
- Object
- Tortoise::Presenter
- Defined in:
- lib/tortoise/presenter.rb
Constant Summary collapse
- PIXEL_SIZE =
5
Instance Attribute Summary collapse
-
#canvas ⇒ Object
readonly
Returns the value of attribute canvas.
Instance Method Summary collapse
-
#initialize(interpreter) ⇒ Presenter
constructor
A new instance of Presenter.
- #to_ascii ⇒ Object
- #to_html ⇒ Object
- #to_png ⇒ Object
Constructor Details
#initialize(interpreter) ⇒ Presenter
Returns a new instance of Presenter.
9 10 11 |
# File 'lib/tortoise/presenter.rb', line 9 def initialize(interpreter) @canvas = interpreter.canvas end |
Instance Attribute Details
#canvas ⇒ Object (readonly)
Returns the value of attribute canvas.
5 6 7 |
# File 'lib/tortoise/presenter.rb', line 5 def canvas @canvas end |
Instance Method Details
#to_ascii ⇒ Object
13 14 15 16 17 |
# File 'lib/tortoise/presenter.rb', line 13 def to_ascii oriented_canvas.inject('') do |ascii, column| ascii += column.map { |pixel| pixel ? 'X ' : '. ' }.join.strip + "\n" end end |
#to_html ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/tortoise/presenter.rb', line 19 def to_html <<-HTML <!DOCTYPE html> <html> #{html_head} <body> #{html_canvas} </body> </html> HTML end |
#to_png ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/tortoise/presenter.rb', line 31 def to_png white = ChunkyPNG::Color('white') black = ChunkyPNG::Color('black') png = ChunkyPNG::Image.new(@canvas.size, @canvas.size) @canvas.each_with_index do |column, x| column.reverse.each_with_index do |pixel, y| png[x, y] = pixel ? black : white end end png.to_blob end |