The Color module defines methods for handling colors. Within the ChunkyPNG library, the concepts of pixels and colors are both used, and they are both represented by a Integer.
Pixels/colors are represented in RGBA components. Each of the four components is stored with a depth of 8 bits (maximum value = 255 = {ChunkyPNG::Color::MAX}). Together, these components are stored in a 4-byte Integer.
A color will always be represented using these 4 components in memory. When the image is encoded, a more suitable representation can be used (e.g. rgb, grayscale, palette-based), for which several conversion methods are provided in this module.
@return [Integer] Black pixel/color
@private @return [Regexp] The regexp to parse hex color values.
@private @return [Regexp] The regexp to parse named color values.
@return [Integer] The maximum value of each color component.
@return [Hash<Symbol, Integer>] All the predefined color names in HTML.
@return [Integer] Fully transparent pixel/color
@return [Integer] White pixel/color
Returns the alpha channel value for the color value.
@param [Integer] value The color value. @return [Integer] A value between 0 and MAX.
# File lib/chunky_png/color.rb, line 197 def a(value) value & 0x000000ff end
Checks whether an alpha channel value can successfully be composed given the resulting color, the mask color and a background color, all of which should be opaque.
@param [Integer] color The color that was the result of compositing. @param [Integer] mask The opaque variant of the color that was being composed @param [Integer] bg The background color on which the color was composed. @param [Integer] tolerance The decomposition tolerance level, a value between 0 and 255. @return [Boolean] True if the alpha component can be decomposed successfully. @see decompose_alpha
# File lib/chunky_png/color.rb, line 394 def alpha_decomposable?(color, mask, bg, tolerance = 1) components = decompose_alpha_components(color, mask, bg) sum = components.inject(0) { |a,b| a + b } max = components.max * 3 return components.max <= 255 && components.min >= 0 && (sum + tolerance * 3) >= max end
Returns the blue-component from the color value.
@param [Integer] value The color value. @return [Integer] A value between 0 and MAX.
# File lib/chunky_png/color.rb, line 189 def b(value) (value & 0x0000ff00) >> 8 end
Blends the foreground and background color by taking the average of the components.
@param [Integer] fg The foreground color. @param [Integer] bg The foreground color. @return [Integer] The blended color.
# File lib/chunky_png/color.rb, line 304 def blend(fg, bg) (fg + bg) >> 1 end
Composes two colors with an alpha channel using floating point math.
This method uses more precise floating point math, but this precision is lost when the result is converted back to an integer. Because it is slower than the version based on integer math, that version is preferred.
@param [Integer] fg The foreground color. @param [Integer] bg The foreground color. @return [Integer] The composited color. @see #compose_quick
# File lib/chunky_png/color.rb, line 281 def compose_precise(fg, bg) return fg if opaque?(fg) || fully_transparent?(bg) return bg if fully_transparent?(fg) fg_a = a(fg).to_f / MAX bg_a = a(bg).to_f / MAX a_com = (1.0 - fg_a) * bg_a new_r = (fg_a * r(fg) + a_com * r(bg)).round new_g = (fg_a * g(fg) + a_com * g(bg)).round new_b = (fg_a * b(fg) + a_com * b(bg)).round new_a = ((fg_a + a_com) * MAX).round rgba(new_r, new_g, new_b, new_a) end
Composes two colors with an alpha channel using integer math.
This version is faster than the version based on floating point math, so this compositing function is used by default.
@param [Integer] fg The foreground color. @param [Integer] bg The foreground color. @return [Integer] The composited color. @see #compose_precise
# File lib/chunky_png/color.rb, line 259 def compose_quick(fg, bg) return fg if opaque?(fg) || fully_transparent?(bg) return bg if fully_transparent?(fg) a_com = int8_mult(0xff - a(fg), a(bg)) new_r = int8_mult(a(fg), r(fg)) + int8_mult(a_com, r(bg)) new_g = int8_mult(a(fg), g(fg)) + int8_mult(a_com, g(bg)) new_b = int8_mult(a(fg), b(fg)) + int8_mult(a_com, b(bg)) new_a = a(fg) + a_com rgba(new_r, new_g, new_b, new_a) end
Decomposes the alpha channel value given the resulting color, the mask color and a background color, all of which should be opaque.
Make sure to call {#alpha_decomposable?} first to see if the alpha channel value can successfully decomposed with a given tolerance, otherwise the return value of this method is undefined.
@param [Integer] color The color that was the result of compositing. @param [Integer] mask The opaque variant of the color that was being composed @param [Integer] bg The background color on which the color was composed. @return [Integer] The best fitting alpha channel, a value between 0 and 255. @see alpha_decomposable?
# File lib/chunky_png/color.rb, line 413 def decompose_alpha(color, mask, bg) components = decompose_alpha_components(color, mask, bg) (components.inject(0) { |a,b| a + b } / 3.0).round end
Decomposes an alpha channel for either the r, g or b color channel. @param [:r, :g, :b] channel The channel to decompose the alpha channel from. @param [Integer] color The color that was the result of compositing. @param [Integer] mask The opaque variant of the color that was being composed @param [Integer] bg The background color on which the color was composed. @return [Integer] The decomposed alpha value for the channel.
# File lib/chunky_png/color.rb, line 424 def decompose_alpha_component(channel, color, mask, bg) cc, mc, bc = send(channel, color), send(channel, mask), send(channel, bg) return 0x00 if bc == cc return 0xff if bc == mc return 0xff if cc == mc (((bc - cc).to_f / (bc - mc).to_f) * MAX).round end
Decomposes the alpha channels for the r, g and b color channel. @param [Integer] color The color that was the result of compositing. @param [Integer] mask The opaque variant of the color that was being composed @param [Integer] bg The background color on which the color was composed.
@return [Array<Integer>] The decomposed alpha values for the r, g and b channels.
# File lib/chunky_png/color.rb, line 439 def decompose_alpha_components(color, mask, bg) [ decompose_alpha_component(:r, color, mask, bg), decompose_alpha_component(:g, color, mask, bg), decompose_alpha_component(:b, color, mask, bg) ] end
Decomposes a color, given a color, a mask color and a background color. The returned color will be a variant of the mask color, with the alpha channel set to the best fitting value. This basically is the reverse operation if alpha composition.
If the color cannot be decomposed, this method will return the fully transparent variant of the mask color.
@param [Integer] color The color that was the result of compositing. @param [Integer] mask The opaque variant of the color that was being composed @param [Integer] bg The background color on which the color was composed. @param [Integer] tolerance The decomposition tolerance level, a value between 0 and 255. @return [Integer] The decomposed color,a variant of the masked color with the
alpha channel set to an appropriate value.
# File lib/chunky_png/color.rb, line 376 def decompose_color(color, mask, bg, tolerance = 1) if alpha_decomposable?(color, mask, bg, tolerance) mask & 0xffffff00 | decompose_alpha(color, mask, bg) else mask & 0xffffff00 end end
Lowers the intensity of a color, by lowering its alpha by a given factor. @param [Integer] color The color to adjust. @param [Integer] factor Fade factor as an integer between 0 and 255. @return [Integer] The faded color.
# File lib/chunky_png/color.rb, line 357 def fade(color, factor) new_alpha = int8_mult(a(color), factor) (color & 0xffffff00) | new_alpha end
Creates a color by converting it from a string in hex notation.
It supports colors with (#rrggbbaa) or without (#rrggbb) alpha channel. Color strings may include the prefix “0x” or “#”.
@param [String] str The color in hex notation. @return [Integer] The
converted color value.
@param [Integer] opacity The opacity value for the color. Overrides any
opacity value given in the hex value if given.
@return [Integer] The color value. @raise [ArgumentError] if the value given is not a hex color notation.
# File lib/chunky_png/color.rb, line 155 def from_hex(hex_value, opacity = nil) if HEX_COLOR_REGEXP =~ hex_value base_color = $1.hex << 8 opacity ||= $2 ? $2.hex : 0xff base_color | opacity else raise ArgumentError, "Not a valid hex color notation: #{hex_value.inspect}!" end end
Creates a color by unpacking an rgb triple from a string.
@param [String] stream The string to load the color from. It should be
at least 3 + pos bytes long.
@param [Integer] pos The position in the string to load the triple from. @return [Integer] The newly constructed color value.
# File lib/chunky_png/color.rb, line 130 def from_rgb_stream(stream, pos = 0) rgb(*stream.unpack("@#{pos}C3")) end
Creates a color by unpacking an rgba triple from a string
@param [String] stream The string to load the color from. It should be
at least 4 + pos bytes long.
@param [Integer] pos The position in the string to load the triple from. @return [Integer] The newly constructed color value.
# File lib/chunky_png/color.rb, line 140 def from_rgba_stream(stream, pos = 0) rgba(*stream.unpack("@#{pos}C4")) end
Returns true if this color is fully transparent.
@param [Integer] value The color to test. @return [true, false] True if the alpha channel equals 0.
# File lib/chunky_png/color.rb, line 228 def fully_transparent?(value) a(value) == 0x00000000 end
Returns the green-component from the color value.
@param [Integer] value The color value. @return [Integer] A value between 0 and MAX.
# File lib/chunky_png/color.rb, line 181 def g(value) (value & 0x00ff0000) >> 16 end
Creates a new color using a grayscale teint. @param [Integer] teint The grayscale teint (0-255), will be used as r, g, and b value. @return [Integer] The newly constructed color value.
# File lib/chunky_png/color.rb, line 108 def grayscale(teint) teint << 24 | teint << 16 | teint << 8 | 0xff end
Returns true if this color is fully transparent.
@param [Integer] value The color to test. @return [true, false] True if the r, g and b component are equal.
# File lib/chunky_png/color.rb, line 220 def grayscale?(value) r(value) == b(value) && b(value) == g(value) end
Creates a new color using a grayscale teint and alpha value. @param [Integer] teint The grayscale teint (0-255), will be used as r, g, and b value. @param [Integer] a The opacity (0-255) @return [Integer] The newly constructed color value.
# File lib/chunky_png/color.rb, line 116 def grayscale_alpha(teint, a) teint << 24 | teint << 16 | teint << 8 | a end
Calculates the grayscale teint of an RGB color.
@param [Integer] color The color to convert.
@return [Integer] The grayscale teint of the input color, 0-255.
# File lib/chunky_png/color.rb, line 336 def grayscale_teint(color) (r(color) * 0.3 + g(color) * 0.59 + b(color) * 0.11).round end
Gets a color value based on a HTML color name.
The color name is flexible. E.g. 'yellowgreen'
,
'Yellow green'
, 'YellowGreen'
,
'YELLOW_GREEN'
and :yellow_green
will all
return the same color value.
You can include a opacity level in the color name (e.g. 'red @
0.5'
) or give an explicit opacity value as second argument. If
no opacity value is given, the color will be fully opaque.
@param [Symbol, String] color_name The color name. It may include an opacity specifier
like <tt>@ 0.8</tt> to set the color's opacity.
@param [Integer] opacity The opacity value for the color between 0 and 255. Overrides
any opacity value given in the color name.
@return [Integer] The color value. @raise [ChunkyPNG::Exception] If the color name was not recognized.
# File lib/chunky_png/color.rb, line 672 def html_color(color_name, opacity = nil) if color_name.to_s =~ HTML_COLOR_REGEXP opacity ||= $2 ? ($2.to_f * 255.0).round : 0xff base_color_name = $1.gsub(/[^a-z]+/, '').downcase.to_sym return PREDEFINED_COLORS[base_color_name] | opacity if PREDEFINED_COLORS.has_key?(base_color_name) end raise ArgumentError, "Unknown color name #{color_name}!" end
Multiplies two fractions using integer math, where the fractions are stored using an integer between 0 and 255. This method is used as a helper method for compositing colors using integer math.
This is a quicker implementation of ((a * b) / 255.0).round.
@param [Integer] a The first fraction. @param [Integer] b The second fraction. @return [Integer] The result of the multiplication.
# File lib/chunky_png/color.rb, line 245 def int8_mult(a, b) t = a * b + 0x80 ((t >> 8) + t) >> 8 end
Interpolates the foreground and background colors by the given alpha value. This also blends the alpha channels themselves.
A blending factor of 255 will give entirely the foreground, while a blending factor of 0 will give the background.
@param [Integer] fg The foreground color. @param [Integer] bg The background color. @param [Integer] alpha The blending factor (fixed 8bit) @param [Integer] The interpolated color.
# File lib/chunky_png/color.rb, line 318 def interpolate_quick(fg, bg, alpha) return fg if alpha >= 255 return bg if alpha <= 0 alpha_com = 255 - alpha new_r = int8_mult(alpha, r(fg)) + int8_mult(alpha_com, r(bg)) new_g = int8_mult(alpha, g(fg)) + int8_mult(alpha_com, g(bg)) new_b = int8_mult(alpha, b(fg)) + int8_mult(alpha_com, b(bg)) new_a = int8_mult(alpha, a(fg)) + int8_mult(alpha_com, a(bg)) return rgba(new_r, new_g, new_b, new_a) end
Returns the opaque value of this color by removing the alpha channel. @param [Integer] value The color to transform. @return [Integer] The opaque color
# File lib/chunky_png/color.rb, line 212 def opaque!(value) value | 0x000000ff end
Returns true if this color is fully opaque.
@param [Integer] value The color to test. @return [true, false] True if the alpha channel equals MAX.
# File lib/chunky_png/color.rb, line 205 def opaque?(value) a(value) == 0x000000ff end
Parses a color value given a numeric or string argument.
It supports color numbers, colors in hex notation and named HTML colors.
@param [Integer, String] The color value. @return [Integer] The color value, with the opacity applied if one was given.
# File lib/chunky_png/color.rb, line 76 def parse(source) return source if source.kind_of?(Integer) case source.to_s when /^\d+$/; source.to_s.to_i when ChunkyPNG::Color::HEX_COLOR_REGEXP; ChunkyPNG::Color.from_hex(source.to_s) when ChunkyPNG::Color::HTML_COLOR_REGEXP; ChunkyPNG::Color.html_color(source.to_s) else raise ArgumentError, "Don't know how to create a color from #{source.inspect}!" end end
Returns the number of bytes used for an image pass @param [Integer] color_mode The color mode in which the pixels are stored. @param [Integer] depth The color depth of the pixels. @param [Integer] width The width of the image pass. @param [Integer] width The height of the image pass. @return [Integer] The number of bytes used per scanline in a datastream.
# File lib/chunky_png/color.rb, line 739 def pass_bytesize(color_mode, depth, width, height) return 0 if width == 0 || height == 0 (scanline_bytesize(color_mode, depth, width) + 1) * height end
Returns the size in bits of a pixel when it is stored using a given color mode. @param [Integer] color_mode The color mode in which the pixels are stored. @param [Integer] depth The color depth of the pixels. @return [Integer] The number of bytes used per pixel in a datastream.
# File lib/chunky_png/color.rb, line 720 def pixel_bitsize(color_mode, depth = 8) samples_per_pixel(color_mode) * depth end
Returns the size in bytes of a pixel when it is stored using a given color mode. @param [Integer] color_mode The color mode in which the pixels are stored. @return [Integer] The number of bytes used per pixel in a datastream.
# File lib/chunky_png/color.rb, line 711 def pixel_bytesize(color_mode, depth = 8) return 1 if depth < 8 (pixel_bitsize(color_mode, depth) + 7) >> 3 end
Returns the red-component from the color value.
@param [Integer] value The color value. @return [Integer] A value between 0 and MAX.
# File lib/chunky_png/color.rb, line 173 def r(value) (value & 0xff000000) >> 24 end
Creates a new color using an r, g, b triple. @param [Integer] r The r-component (0-255) @param [Integer] g The g-component (0-255) @param [Integer] b The b-component (0-255) @return [Integer] The newly constructed color value.
# File lib/chunky_png/color.rb, line 101 def rgb(r, g, b) r << 24 | g << 16 | b << 8 | 0xff end
Creates a new color using an r, g, b triple and an alpha value. @param [Integer] r The r-component (0-255) @param [Integer] g The g-component (0-255) @param [Integer] b The b-component (0-255) @param [Integer] a The opacity (0-255) @return [Integer] The newly constructed color value.
# File lib/chunky_png/color.rb, line 92 def rgba(r, g, b, a) r << 24 | g << 16 | b << 8 | a end
Returns the number of sample values per pixel. @param [Integer] color_mode The color mode being used. @return [Integer] The number of sample values per pixel.
# File lib/chunky_png/color.rb, line 697 def samples_per_pixel(color_mode) case color_mode when ChunkyPNG::COLOR_INDEXED; 1 when ChunkyPNG::COLOR_TRUECOLOR; 3 when ChunkyPNG::COLOR_TRUECOLOR_ALPHA; 4 when ChunkyPNG::COLOR_GRAYSCALE; 1 when ChunkyPNG::COLOR_GRAYSCALE_ALPHA; 2 else raise ChunkyPNG::NotSupported, "Don't know the numer of samples for this colormode: #{color_mode}!" end end
Returns the number of bytes used per scanline. @param [Integer] color_mode The color mode in which the pixels are stored. @param [Integer] depth The color depth of the pixels. @param [Integer] width The number of pixels per scanline. @return [Integer] The number of bytes used per scanline in a datastream.
# File lib/chunky_png/color.rb, line 729 def scanline_bytesize(color_mode, depth, width) ((pixel_bitsize(color_mode, depth) * width) + 7) >> 3 end
Converts a color to a fiting grayscale value. It will conserve the alpha channel.
This method will return a full color value, with the R, G, and B value set to the grayscale teint calcuated from the input color's R, G and B values.
@param [Integer] color The color to convert. @return [Integer] The input color, converted to the best fitting grayscale. @see grayscale_teint
# File lib/chunky_png/color.rb, line 349 def to_grayscale(color) grayscale_alpha(grayscale_teint(color), a(color)) end
Returns an array with the grayscale teint and alpha channel values for this color.
This method expects the color to be grayscale, i.e. r,g and b value to be equal and uses only the B channel. If you need to convert a color to grayscale first, see {#to_grayscale}.
@param [Integer] color The grayscale color to convert. @return [Array<Integer>] An array with 2 Integer elements. @see to_grascale
# File lib/chunky_png/color.rb, line 497 def to_grayscale_alpha_bytes(color) [b(color), a(color)] # assumption r == g == b end
Returns an array with the grayscale teint value for this color.
This method expects the r,g and b value to be equal, and the alpha channel will be discarded.
@param [Integer] color The grayscale color to convert. @return [Array<Integer>] An array with 1 Integer element.
# File lib/chunky_png/color.rb, line 483 def to_grayscale_bytes(color) [b(color)] # assumption r == g == b end
Returns a string representing this color using hex notation (i.e. rrggbbaa).
@param [Integer] value The color to convert. @return [String] The color in hex notation, starting with a pound sign.
# File lib/chunky_png/color.rb, line 455 def to_hex(color, include_alpha = true) include_alpha ? ('#%08x' % color) : ('#%06x' % [color >> 8]) end
Returns an array with the separate RGBA values for this color.
@param [Integer] color The color to convert. @return [Array<Integer>] An array with 4 Integer elements.
# File lib/chunky_png/color.rb, line 463 def to_truecolor_alpha_bytes(color) [r(color), g(color), b(color), a(color)] end
Returns an array with the separate RGB values for this color. The alpha channel will be discarded.
@param [Integer] color The color to convert. @return [Array<Integer>] An array with 3 Integer elements.
# File lib/chunky_png/color.rb, line 472 def to_truecolor_bytes(color) [r(color), g(color), b(color)] end