Class: RComposite::Layer

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

Direct Known Subclasses

AdjustmentLayer, FillLayer, LayerMask

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Layer

Returns a new instance of Layer.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rcomposite/layer.rb', line 34

def initialize(options = {})
  if options[:file]
    @image = Magick::Image.read(options[:file]).first
    @image.background_color = 'transparent'
  elsif options[:blob]
    @image = Magick::Image.from_blob(options[:blob]).first
    @image.background_color = 'transparent'
  elsif options[:image]
    if options[:image].is_a? Magick::Image
      @image = options[:image]
    end
  end

  if @image
    @image.matte = true
    @mode = Normal
    @offset_x = 0
    @offset_y = 0
    @opacity_percent = 100
    @layer_mask = nil
  else
    raise "Layer not created -- no layer source."
  end
end

Instance Attribute Details

#imageObject

Returns the value of attribute image.



31
32
33
# File 'lib/rcomposite/layer.rb', line 31

def image
  @image
end

#offset_xObject (readonly)

Returns the value of attribute offset_x.



32
33
34
# File 'lib/rcomposite/layer.rb', line 32

def offset_x
  @offset_x
end

#offset_yObject (readonly)

Returns the value of attribute offset_y.



32
33
34
# File 'lib/rcomposite/layer.rb', line 32

def offset_y
  @offset_y
end

Instance Method Details

#heightObject



74
75
76
# File 'lib/rcomposite/layer.rb', line 74

def height
  @image.rows
end

#join_set(set) ⇒ Object



109
110
111
# File 'lib/rcomposite/layer.rb', line 109

def join_set(set)
  set.add_layer(self)
end

#layer_mask(options, &block) ⇒ Object



59
60
61
62
63
# File 'lib/rcomposite/layer.rb', line 59

def layer_mask(options, &block)
  @layer_mask = LayerMask.new options
  
  @layer_mask.instance_eval &block if block_given?
end

#leave_set(set) ⇒ Object



113
114
115
# File 'lib/rcomposite/layer.rb', line 113

def leave_set(set)
  set.remove_layer(self)
end

#merge_down(image) ⇒ Object



65
66
67
68
# File 'lib/rcomposite/layer.rb', line 65

def merge_down(image)
  @layer_mask.apply self if @layer_mask
  image.composite!(@image, @offset_x, @offset_y, @mode)
end

#mode(mode = nil) ⇒ Object



100
101
102
103
# File 'lib/rcomposite/layer.rb', line 100

def mode(mode = nil)
  return @mode if mode.nil?
  @mode = mode
end

#offset(x, y) ⇒ Object



78
79
80
81
# File 'lib/rcomposite/layer.rb', line 78

def offset(x, y)
  @offset_x = x
  @offset_y = y
end

#opacity(percent) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rcomposite/layer.rb', line 87

def opacity(percent)
  @opacity_percent = percent
  # intercept original alpha channel with pixel intensity
  alpha_channel = @image.channel(Magick::AlphaChannel).negate
  intensity = (Magick::MaxRGB * (percent/100.0)).round
  alpha_channel.composite!(Magick::Image.new(width, height) { self.background_color = Magick::Pixel.new(intensity,intensity,intensity) }, Magick::CenterGravity, Multiply)
  alpha_channel.matte = false
  
  @image.composite!(alpha_channel, Magick::CenterGravity, Magick::CopyOpacityCompositeOp)
  return true
end

#rotate(degrees) ⇒ Object



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

def rotate(degrees)
  @image.rotate!(degrees)
end

#save_as(filename) ⇒ Object



105
106
107
# File 'lib/rcomposite/layer.rb', line 105

def save_as(filename)
  @image.write(filename)
end

#widthObject



70
71
72
# File 'lib/rcomposite/layer.rb', line 70

def width
  @image.columns
end