function ctools_stylizer_image_processor::command_colorize

Colorize the current workspace with the given location.

This uses simple color blending to colorize the image.

@todo it is possible that this colorize could allow different methods for determining how to blend colors?

File

includes/stylizer.inc, line 444

Class

ctools_stylizer_image_processor

Code

function command_colorize($color, $x = NULL, $y = NULL, $width = NULL, $height = NULL) {
    if (!isset($x)) {
        $whole_image = TRUE;
        $x = $y = 0;
        $width = imagesx($this->workspace);
        $height = imagesy($this->workspace);
    }
    $this->log("Colorize: {$color} ({$x}, {$y}, {$width}, {$height})");
    $c = _color_unpack($this->palette[$color]);
    imagealphablending($this->workspace, FALSE);
    imagesavealpha($this->workspace, TRUE);
    // If PHP 5 use the nice imagefilter which is faster.
    if (!empty($whole_image) && version_compare(phpversion(), '5.2.5', '>=') && function_exists('imagefilter')) {
        imagefilter($this->workspace, IMG_FILTER_COLORIZE, $c[0], $c[1], $c[2]);
    }
    else {
        // Otherwise we can do it the brute force way.
        for ($j = 0; $j < $height; $j++) {
            for ($i = 0; $i < $width; $i++) {
                $current = imagecolorsforindex($this->workspace, imagecolorat($this->workspace, $i, $j));
                $new_index = imagecolorallocatealpha($this->workspace, $c[0], $c[1], $c[2], $current['alpha']);
                imagesetpixel($this->workspace, $i, $j, $new_index);
            }
        }
    }
}