Class: Quilt::ImageRmagick

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

Instance Method Summary collapse

Constructor Details

#initialize(width, height, opt = {}) ⇒ ImageRmagick

Returns a new instance of ImageRmagick.



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/quilt.rb', line 65

def initialize width, height, opt = {}
  begin
    require 'rmagick'
  rescue LoadError
    require 'RMagick'
  rescue LoadError
    puts "WARNING: Failed to require rmagick, image generation will fail"
  end
  @image = Magick::Image.new width, height do |c|
    c.background_color = 'Transparent' if opt[:transparent]
  end
  @image.format = 'png'
end

Instance Method Details

#color(r, g, b) ⇒ Object



79
80
81
# File 'lib/quilt.rb', line 79

def color r, g, b
  sprintf('#%02x%02x%02x', r, g, b)
end

#fill_rect(x, y, _x, _y, color) ⇒ Object



87
88
89
90
91
92
# File 'lib/quilt.rb', line 87

def fill_rect x, y, _x, _y, color
  dr = Magick::Draw.new
  dr.fill color
  dr.rectangle x, y, _x, _y
  dr.draw @image
end

#polygon(points, color) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/quilt.rb', line 94

def polygon points, color
  unless points.empty?
    dr = Magick::Draw.new
    dr.fill color
    dr.polygon *(points.flatten)
    dr.draw @image
  end
end

#polygon_clip(points) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/quilt.rb', line 103

def polygon_clip points
  unless points.empty?
    clip_img = Magick::Image.new(@image.rows, @image.rows)
    dr = Magick::Draw.new
    dr.polygon(*points.flatten)
    dr.draw(clip_img)
    clip_img_t = clip_img.transparent('white', Magick::TransparentOpacity)
    f = @image.format
    @image = clip_img_t.composite(@image,
      Magick::CenterGravity, Magick::OutCompositeOp)
    @image.format = f
  end
end

#resize(size) ⇒ Object



125
126
127
# File 'lib/quilt.rb', line 125

def resize size
  @image.resize! size, size
end

#to_blobObject



121
122
123
# File 'lib/quilt.rb', line 121

def to_blob
  @image.to_blob
end

#transparent(color) ⇒ Object



83
84
85
# File 'lib/quilt.rb', line 83

def transparent color
  @image.transparent color
end

#write(path) ⇒ Object



117
118
119
# File 'lib/quilt.rb', line 117

def write path
  open(path, 'wb') {|f| f.write @image.to_blob }
end