lib/chunky_png/canvas/masking.rb
# Creates a base image and a mask image from an original image that has a particular theme color.
# This can be used to easily change a theme color in an image.
#
# It will extract all the pixels that look like the theme color (with a tolerance level) and put
# these in a mask image. All the other pixels will be stored in a base image. Both images will be
# of the exact same size as the original image. The original image will be left untouched.
#
# The color of the mask image can be changed with {#change_mask_color!}. This new mask image can
# then be composed upon the base image to create an image with a new theme color. A call to
# {#change_theme_color!} will perform this in one go.
#
# @param [Integer] mask_color The current theme color.
# @param [Integer] bg_color The background color on which the theme colored pixels are applied.
# @param [Integer] tolerance The tolerance level to use when extracting the mask image. Five is
#    the default; increase this if the masked image does not extract all the required pixels,
#    decrease it if too many pixels get extracted.
# @return [Array<ChunkyPNG::Canvas, ChunkyPNG::Canvas>] An array with the base canvas and the mask
#    canvas as elements.
# @see #change_theme_color!
# @see #change_mask_color!
Creates a base image and a mask image from an original image that has a particular theme color.
This can be used to easily change a theme color in an image.

It will extract all the pixels that look like the theme color (with a tolerance level) and put
these in a mask image. All the other pixels will be stored in a base image. Both images will be
of the exact same size as the original image. The original image will be left untouched.

The color of the mask image can be changed with {#change_mask_color!}. This new mask image can
then be composed upon the base image to create an image with a new theme color. A call to
{#change_theme_color!} will perform this in one go.
Creates a base image and a mask image from an original image that has a particular theme color . This can be used to easily change a theme color in an image . It will extract all the pixels that look like the theme color ( with a tolerance level ) and put these in a mask image . All the other pixels will be stored in a base image . Both images will be of the exact same size as the original image . The original image will be left untouched . The color of the mask image can be changed with { #change_mask_color! } . This new mask image can then be composed upon the base image to create an image with a new theme color . A call to { #change_theme_color! } will perform this in one go .
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
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
Creates a base image and a mask image from an original image that has a particular theme color.
Creates a base image and a mask image from an original image that has a particular theme color .
An array with the base canvas and the mask canvas as elements.
An array with the base canvas and the mask canvas as elements .
Array<ChunkyPNG::Canvas, ChunkyPNG::Canvas>
mask_color
<not_specific>
bg_color
<not_specific>
tolerance
<not_specific>
<not_specific>
tolerance The tolerance level to use when extracting the mask image. Five is the default; increase this if the masked image does not extract all the required pixels, decrease it if too many pixels get extracted.
tolerance The tolerance level to use when extracting the mask image . Five is the default ; increase this if the masked image does not extract all the required pixels decrease it if too many pixels get extracted .
see
None
see
None