module ChunkyPNG::Canvas::Masking

def change_mask_color!(new_color)

Other tags:
    See: #extract_mask -
    See: #change_theme_color! -

Raises:
  • (ChunkyPNG::ExpectationFailed) - when this canvas is not a mask image, i.e. its palette

Parameters:
  • new_color (Integer) -- The color to replace the original mask color with.
def change_mask_color!(new_color)
  raise ChunkyPNG::ExpectationFailed, "This is not a mask image!" if palette.opaque_palette.size != 1
  pixels.map! { |pixel| (new_color & 0xffffff00) | ChunkyPNG::Color.a(pixel) }
  self
end

def change_theme_color!(old_theme_color, new_theme_color, bg_color = ChunkyPNG::Color::WHITE, tolerance = 5)

Other tags:
    See: #change_mask_color! -
    See: #change_theme_color! -

Returns:
  • (ChunkyPNG::Canvas) - Returns itself, but with the theme colored pixels changed.

Parameters:
  • tolerance (Integer) -- The tolerance level to use when extracting the mask image. Five is
  • bg_color (Integer) -- The background color on which the theme colored pixels are placed.
  • new_theme_color (Integer) -- The color to replace the old theme color with.
  • old_theme_color (Integer) -- The original theme color in this image.
def change_theme_color!(old_theme_color, new_theme_color, bg_color = ChunkyPNG::Color::WHITE, tolerance = 5)
  base, mask = extract_mask(old_theme_color, bg_color, tolerance)
  mask.change_mask_color!(new_theme_color)
  replace!(base.compose!(mask))
end

def extract_mask(mask_color, bg_color = ChunkyPNG::Color::WHITE, tolerance = 5)

Other tags:
    See: #change_mask_color! -
    See: #change_theme_color! -

Returns:
  • (Array) - An array with the base canvas and the mask

Parameters:
  • tolerance (Integer) -- The tolerance level to use when extracting the mask image. Five is
  • bg_color (Integer) -- The background color on which the theme colored pixels are applied.
  • mask_color (Integer) -- The current theme color.
def extract_mask(mask_color, bg_color = ChunkyPNG::Color::WHITE, tolerance = 5)
  base_pixels = []
  mask_pixels = []
  pixels.each do |pixel|
    if ChunkyPNG::Color.alpha_decomposable?(pixel, mask_color, bg_color, tolerance)
      mask_pixels << ChunkyPNG::Color.decompose_color(pixel, mask_color, bg_color, tolerance)
      base_pixels << bg_color
    else
      mask_pixels << (mask_color & 0xffffff00)
      base_pixels << pixel
    end
  end
  [self.class.new(width, height, base_pixels), self.class.new(width, height, mask_pixels)]
end