Skip to content

Images with a color theme

wvanbergen edited this page Aug 18, 2010 · 9 revisions

ChunkyPNG can be used to created variants of images with a different theme color. This only works for images for which the theme color is used as a solid color, or anti-aliased/composed on a single background color.

image = ChunkyPNG::Image.from_file('clock.png')

default_theme_color = ChunkyPNG::Color.from_hex('#e10f7a')
new_theme_color     = ChunkyPNG::Color.from_hex('#ff0000')
background          = ChunkyPNG::Color::WHITE
tolerance           = 5

image.change_theme_color!(default_theme_color, new_theme_color, background, tolerance)
image.save('clock_red.png')

How this works

Basically, it will first split the original image into a base image and a mask image. The mask image contains all the pixels of the theme color, the base image all the other pixels.

The color of the mask image is then updated to the new theme color:

The updated mask image is then composed on top of the base image, generating the new themed image:

In code:

base, mask = image.extract_mask(default_theme_color, background, tolerance)
mask.change_mask_color!(new_theme_color)
image.replace(base.compose(mask))