Class: ImageGenerator

Inherits:
Object
  • Object
show all
Includes:
CairoTools
Defined in:
lib/image_generator.rb

Instance Attribute Summary

Attributes included from CairoTools

#bottom_margin, #canvas_height, #canvas_width, #cr, #left_margin, #preview, #right_margin, #surface, #top_margin

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CairoTools

#circular_text, #clip!, #clouds, #create_text_box, #dimensions, #draw_image, #draw_text_box, #generate_image, #get_pixel, #gradient, #height, #linear_gradient, #load_image, #margin, #radial_gradient, #rounded_rectangle, #set_color, #shadow, #transform, #transparent!, #width

Methods included from Color

#hsl, #rgb

Class Method Details

.color(name, color) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/image_generator.rb', line 10

def self.color(name, color)
  color = Color::HSL.new(color)
  colors[name] = color
  self.class_eval <<-"end;"
    def #{name}
      self.class.colors[:#{name}]
    end
    def #{name}!(a=nil)
      set_color(a ? #{name}.a(a) : #{name})
    end
  end;
end

.colorsObject



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

def self.colors
  @@colors ||= {}
end

.generate(name, *options) ⇒ Object



41
42
43
# File 'lib/image_generator.rb', line 41

def self.generate(name, *options)
  generate_image(File.join(File.dirname(__FILE__), "../public/images", name), *options)
end

.generate_image(path, *options) ⇒ Object



37
38
39
# File 'lib/image_generator.rb', line 37

def self.generate_image(path, *options)
  new.generate_image(path, options)
end

.image(name, options = {:suite => true}, &block) ⇒ Object



57
58
59
60
# File 'lib/image_generator.rb', line 57

def self.image(name, options={:suite => true}, &block)
  images[name] = block
  suite_images << name if options[:suite]
end

.imagesObject



66
67
68
# File 'lib/image_generator.rb', line 66

def self.images
  @images ||= {}
end

.preview(*options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/image_generator.rb', line 45

def self.preview(*options)
  return unless $0.match(/#{name.underscore}.rb$/)
  path = File.join(File.dirname($0), "generated.png")
  options = Array(yield) if block_given?
  instance = self.new
  instance.preview = true
  instance.generate_image(path, options)
  `open #{path}`
  sleep 1
  File.unlink(path)
end

.shape(name, width, height, &block) ⇒ Object



30
31
32
33
34
35
# File 'lib/image_generator.rb', line 30

def self.shape(name, width, height, &block)
  shapes[name] = shape = Shape.new(name, width, height, block)
  define_method("draw_#{name}") do |x, y|
    shape.draw(cr, x, y)
  end
end

.shapesObject



26
27
28
# File 'lib/image_generator.rb', line 26

def self.shapes
  @@shapes ||= {}
end

.suite(prefix, *args) ⇒ Object



70
71
72
73
74
# File 'lib/image_generator.rb', line 70

def self.suite(prefix, *args)
  suite_images.each do |name|
    generate("#{prefix}_#{name}.png", name, *args)
  end
end

.suite_imagesObject



62
63
64
# File 'lib/image_generator.rb', line 62

def self.suite_images
  @suite_images ||= []
end

Instance Method Details

#draw(suffix, *args) ⇒ Object



76
77
78
# File 'lib/image_generator.rb', line 76

def draw(suffix, *args)
  instance_eval(*args, &self.class.images[suffix])
end