Class: Phlox::Graphics

Inherits:
Object
  • Object
show all
Defined in:
lib/phlox/graphics.rb

Instance Method Summary collapse

Constructor Details

#initialize(width = 255, height = 255) ⇒ Graphics

Returns a new instance of Graphics.



5
6
7
8
# File 'lib/phlox/graphics.rb', line 5

def initialize(width=255, height=255)
  @width, @height = width, height
  @img = Magick::Image.new @width, @height
end

Instance Method Details

#fill(grad_prc) ⇒ Object

Fill the image with a pixel gradient determined by the proc argument. The proc should yield a 3-element list of [r, g, b] values for each location.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/phlox/graphics.rb', line 12

def fill grad_prc
  # make an array to hold the values
  data = Array.new(@width) do |x|
    Array.new(@height) do |y|
      grad_prc.call x, y
    end
  end

  # write the color values into the image
  data.each_with_index do |row, y|
    row.each_with_index do |val, x|
      @img.pixel_color y, x, "rgb(#{val.join ', '})"
    end
  end
end

#write(path, scale = 1) ⇒ Object

Write the image file to ‘path` with optional scaling.



29
30
31
# File 'lib/phlox/graphics.rb', line 29

def write path, scale=1
  @img.scale(scale).write path
end