Sha256: 651ab7d412ae34b84f5aa23dfce3b14e4c11b0bb71658567fd434cfdb23dc3e6

Contents?: true

Size: 944 Bytes

Versions: 1

Compression:

Stored size: 944 Bytes

Contents

class PSD
  class Slice
    attr_reader :id, :group_id, :origin, :associated_layer_id, :name, :type,
                :bounds, :url, :target, :message, :alt, :cell_text_is_html,
                :cell_text, :horizontal_alignment, :vertical_alignment,
                :color

    def initialize(psd, data)
      @psd = psd
      @keys = data.keys
      data.each do |k, v|
        instance_variable_set("@#{k}", v)
      end
    end

    [:left, :top, :right, :bottom].each do |dir|
      define_method(dir) { @bounds[dir] }
    end

    def width
      right - left
    end

    def height
      bottom - top
    end

    def associated_layer
      @psd.tree.find_by_id(associated_layer_id)
    end

    def to_png
      @png ||= @psd.image.to_png.crop(left, top, width, height)
    end

    def save_as_png(file)
      @png.save(file, :fast_rgba)
    end

    def to_hash
      Hash[@keys.map { |k| [k.to_sym, self.send(k)] }]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
psd-3.3.2 lib/psd/slice.rb