Sha256: 19dae6d152c213a5693097d5306990e65a726ec4d3d2269b2ec5ba348a64b51f

Contents?: true

Size: 1.62 KB

Versions: 18

Compression:

Stored size: 1.62 KB

Contents

class PSD
  class Renderer
    class Blender
      attr_reader :fg, :bg

      # Takes a foreground Canvas and a background Canvas
      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

      # Composes the foreground Canvas onto the background Canvas using the
      # blending mode specified by the foreground.
      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

      private

      def compose_options
        {
          opacity: @opacity,
          fill_opacity: @fill_opacity
        }
      end

      def calculated_opacity
        @calculated_opacity ||= compose_options[:opacity] * compose_options[:fill_opacity] / 255
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
psd-3.9.0 lib/psd/renderer/blender.rb
psd-3.8.0 lib/psd/renderer/blender.rb
psd-3.7.0 lib/psd/renderer/blender.rb
psd-3.6.0 lib/psd/renderer/blender.rb
psd-3.5.0 lib/psd/renderer/blender.rb
psd-3.4.0 lib/psd/renderer/blender.rb
psd-3.3.3 lib/psd/renderer/blender.rb
psd-3.3.2 lib/psd/renderer/blender.rb
psd-3.3.1 lib/psd/renderer/blender.rb
psd-3.2.4 lib/psd/renderer/blender.rb
psd-3.2.3 lib/psd/renderer/blender.rb
psd-3.2.2 lib/psd/renderer/blender.rb
psd-3.2.1 lib/psd/renderer/blender.rb
psd-3.2.0 lib/psd/renderer/blender.rb
psd-3.1.5 lib/psd/renderer/blender.rb
psd-3.1.4 lib/psd/renderer/blender.rb
psd-3.1.3 lib/psd/renderer/blender.rb
psd-3.1.2 lib/psd/renderer/blender.rb