Method: Montage::Sprite#write

Defined in:
lib/montage/sprite.rb

#writeObject

Uses RMagick to creates a 8-bit (with alpha) PNG containing all of the source files.

If a file exists at the output path, it will be overwritten.

Raises:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/montage/sprite.rb', line 86

def write
  unless @save_path.dirname.writable?
    raise TargetNotWritable, <<-MESSAGE
      Montage can't save the sprite in `#{@save_path.dirname.to_s}'
      as it isn't writable.
    MESSAGE
  end

  list = Magick::ImageList.new

  @sources.each do |source|
    list << source.image

    if @padding and @padding > 0
      list << Magick::Image.new(1, @padding) do
        self.background_color = '#FFF0'
      end
    end
  end

  # RMagick uses instance_eval, @set isn't available in the block below.
  sources_length = @sources.length

  montage = list.montage do
    self.gravity = Magick::NorthWestGravity
    # Transparent background.
    self.background_color = '#FFF0'
    # Allow each image to take up as much space as it needs.
    self.geometry = '+0+0'
    # columns=1, rows=Sources plus padding.
    self.tile = Magick::Geometry.new(1, sources_length * 2)
  end

  # Remove the blank space from the bottom of the image.
  montage.crop!(0, 0, 0, (montage.first.rows) - @padding)
  montage.write("PNG32:#{@save_path}")
end