Class: ChunkyPNG::Chunk::Transparency

Inherits:
Generic show all
Defined in:
lib/chunky_png/chunk.rb

Overview

A transparency (tRNS) chunk defines the transparency for an image.

  • For indexed images, it contains the alpha channel for the colors defined in the Palette (PLTE) chunk.

  • For grayscale images, it contains the grayscale teint that should be considered fully transparent.

  • For truecolor images, it contains the color that should be considered fully transparent.

Images having a color mode that already includes an alpha channel, this chunk should not be included.

Instance Attribute Summary

Attributes inherited from Generic

#content

Attributes inherited from Base

#type

Instance Method Summary collapse

Methods inherited from Generic

#initialize, read

Methods inherited from Base

#initialize, #write, #write_with_crc

Constructor Details

This class inherits a constructor from ChunkyPNG::Chunk::Generic

Instance Method Details

#grayscale_entry(bit_depth) ⇒ Integer

Returns the grayscale entry to be replaced by transparent pixels.

This method should only be used for images having color mode ChunkyPNG::COLOR_GRAYSCALE (0).

Returns:

  • (Integer)

    The (grayscale) color to replace with fully transparent pixels.


254
255
256
257
# File 'lib/chunky_png/chunk.rb', line 254

def grayscale_entry(bit_depth)
  value = ChunkyPNG::Canvas.send(:"decode_png_resample_#{bit_depth}bit_value", content.unpack("n")[0])
  ChunkyPNG::Color.grayscale(value)
end

#palette_alpha_channelArray<Integer>

Returns the alpha channel for the palette of an indexed image.

This method should only be used for images having color mode ChunkyPNG::COLOR_INDEXED (3).

Returns:

  • (Array<Integer>)

    Returns an array of alpha channel values [0-255].


231
232
233
# File 'lib/chunky_png/chunk.rb', line 231

def palette_alpha_channel
  content.unpack("C*")
end

#truecolor_entry(bit_depth) ⇒ Integer

Returns the truecolor entry to be replaced by transparent pixels,

This method should only be used for images having color mode ChunkyPNG::COLOR_TRUECOLOR (2).

Returns:

  • (Integer)

    The color to replace with fully transparent pixels.


241
242
243
244
245
# File 'lib/chunky_png/chunk.rb', line 241

def truecolor_entry(bit_depth)
  decode_method_name = :"decode_png_resample_#{bit_depth}bit_value"
  values = content.unpack("nnn").map { |c| ChunkyPNG::Canvas.send(decode_method_name, c) }
  ChunkyPNG::Color.rgb(*values)
end