This is a fun little utility I couchified for a project of mine. It gets the average color of a JPG image as an rgba value. Use it for automated color-coordinated backgrounds and effects. Opacity and brightness can be added as parameters.
Code: Select all
<div style="background-color:<cms:avg_color gallery_image brightness='1.5' opacity='.8' />;">
    <img src="<cms:show gallery_image />" alt='' />
</div>

The average color is a pretty blunt tool. When you mix a bunch of colors together, they tend to get muddy, right? So this utility tends to produce... let's call them earth tones. You can brighten the colors by setting the brightness parameter. Above 1 is brighter, below 1 is darker. Brightening tends to give more pastel shades. But again, the brightness dial is a pretty blunt tool. These aren't sophisticated algorithms. Results may vary.

But as a simple automatic colorization tool, I thought it was worth sharing.

Here's a sample of the average colors generated by 4 different images:
auto-background.jpg
auto-background.jpg (101.44 KiB) Viewed 147 times

Here are the same images with the brightness goosed to 1.5:
auto-background-goosed.jpg
auto-background-goosed.jpg (93.12 KiB) Viewed 147 times

Add this custom code to couch/addons/kfunctions.php to register the tag:
Code: Select all
class CustomTags {   
    static function avg_color ( $params, $node ){
        if( count($node->children) ) {die("ERROR: Tag \"".$node->name."\" is a self closing tag");}
        $opacity='1';
        $brightness='1';
        foreach($params as $param){
            if (trim($param['lhs']) === 'opacity' ){
                $opacity = trim($param['rhs']);
            }
            if (trim($param['lhs']) === 'brightness' ){
                $brightness = abs(trim($param['rhs']));
            }
        }
        $filename = trim($params[0]['rhs']);   
        $image = imagecreatefromjpeg($filename);
        $width = imagesx($image);
        $height = imagesy($image);
        $pixel = imagecreatetruecolor(1, 1);
        imagecopyresampled($pixel, $image, 0, 0, 0, 0, 1, 1, $width, $height);
        $rgb = imagecolorat($pixel, 0, 0);
        $color = imagecolorsforindex($pixel, $rgb);
        $color['red'] = (intval($color['red'] * $brightness) <= 255 )? intval($color['red'] * $brightness) : 255;
        $color['blue'] = (intval($color['blue'] * $brightness) <= 255 )? intval($color['blue'] * $brightness) : 255;
        $color['green'] = (intval($color['green'] * $brightness) <= 255 )? intval($color['green'] * $brightness) : 255;
        return 'rgba(' . $color['red'] . ', ' . $color['green'] .', ' . $color['blue'] . ', ' .$opacity . ')';
    }
}

$FUNCS->register_tag( 'avg_color', array('CustomTags', 'avg_color') );