Class: ImageVise::BackgroundFill

Inherits:
Object
  • Object
show all
Defined in:
lib/image_vise/operators/background_fill.rb

Overview

Applies a background fill color. Can handle most ‘word’ colors and hex color codes but not RGB values.

The corresponding Pipeline method is ‘background_fill`.

Instance Method Summary collapse

Constructor Details

#initializeBackgroundFill

Returns a new instance of BackgroundFill.

Raises:

  • (ArgumentError)


6
7
8
9
10
# File 'lib/image_vise/operators/background_fill.rb', line 6

def initialize(*)
  super
  self.color = color.to_s
  raise ArgumentError, "the :color parameter must be present and not empty" if self.color.empty?
end

Instance Method Details

#apply!(image) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/image_vise/operators/background_fill.rb', line 12

def apply!(image)
  # Create an image filled with our color, preserving the size, color class and dimensions
  fill_image = image.copy
  fill_image.color_reset!(color)

  # Composite our actual image _on top_ of it, using the standard over composite operator.
  # This way we don't have to look for "UnderCompositeOp" within the bowels of RMagick.
  fill_image.composite!(image, x_off=0, y_off=0, Magick::OverCompositeOp)
  # ..and move the resulting pixels into our original images, replacing everything
  image.composite!(fill_image, x_off=0, y_off=0, Magick::CopyCompositeOp)
  image.alpha(Magick::DeactivateAlphaChannel)
ensure
  ImageVise.destroy(fill_image)
end