Class: PSD::Renderer::Blender

Inherits:
Object
  • Object
show all
Defined in:
lib/psd/renderer/blender.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fg, bg) ⇒ Blender

Takes a foreground Canvas and a background Canvas



7
8
9
10
11
12
13
14
# File 'lib/psd/renderer/blender.rb', line 7

def initialize(fg, bg)
  @fg = fg
  @bg = bg

  @opacity = @fg.opacity.to_i
  @fill_opacity = @fg.fill_opacity.to_i
  PSD.logger.debug "Blender: name = #{fg.node.name}, opacity = #{@opacity}, fill opacity = #{@fill_opacity}"
end

Instance Attribute Details

#bgObject (readonly)

Returns the value of attribute bg.



4
5
6
# File 'lib/psd/renderer/blender.rb', line 4

def bg
  @bg
end

#fgObject (readonly)

Returns the value of attribute fg.



4
5
6
# File 'lib/psd/renderer/blender.rb', line 4

def fg
  @fg
end

Instance Method Details

#compose!Object

Composes the foreground Canvas onto the background Canvas using the blending mode specified by the foreground.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/psd/renderer/blender.rb', line 18

def compose!
  PSD.logger.debug "#{fg.node.debug_name} -> #{bg.node.debug_name}: #{fg.node.blending_mode} blending"
  PSD.logger.debug "fg: (#{fg.left}, #{fg.top}) #{fg.width}x#{fg.height}; bg: (#{bg.left}, #{bg.top}) #{bg.width}x#{bg.height}"

  offset_x = fg.left - bg.left
  offset_y = fg.top - bg.top

  fg.height.times do |y|
    fg.width.times do |x|
      base_x = x + offset_x
      base_y = y + offset_y

      next if base_x < 0 || base_y < 0 || base_x >= bg.width || base_y >= bg.height

      color = Compose.send(
        fg.node.blending_mode,
        fg.get_pixel(x, y),
        bg.get_pixel(base_x, base_y),
        calculated_opacity
      )

      bg.set_pixel base_x, base_y, color
    end
  end
end