Class: OfIf::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/of_if/image.rb

Overview

Image renders a source file only by its indentation and obfuscates the code by displaying only color blocks.

Constant Summary collapse

DEFAULTS =
{
  margin: 100,
  char_width: 10,
  line_height: 20,
  minimal_width: 480
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeImage

Returns a new instance of Image.



18
19
20
21
22
# File 'lib/of_if/image.rb', line 18

def initialize
  @width = minimal_width
  @image_operations = []
  @current_line_index = 0
end

Instance Attribute Details

#widthObject (readonly)

Returns the value of attribute width.



16
17
18
# File 'lib/of_if/image.rb', line 16

def width
  @width
end

Class Method Details

.from_sourcefile(sourcefile) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/of_if/image.rb', line 24

def self.from_sourcefile(sourcefile)
  file = File.new sourcefile

  new.tap do |image|
    file.lines.each { |line| image << line }
  end
end

Instance Method Details

#<<(line) ⇒ Object



32
33
34
35
36
# File 'lib/of_if/image.rb', line 32

def <<(line)
  @image_operations << create_image_operation(line, @current_line_index)
  update_width line.length
  @current_line_index += 1
end

#heightObject



38
# File 'lib/of_if/image.rb', line 38

def height = offset + (@current_line_index * line_height)

#write(filename) ⇒ Object



40
41
42
43
44
# File 'lib/of_if/image.rb', line 40

def write(filename)
  png = ChunkyPNG::Image.new width, height, ChunkyPNG::Color::WHITE
  @image_operations.each { |op| op.call png }
  png.save filename
end