Voting

: min(zero, one)?
(Example: nine)

The Note You're Voting On

Thomas Mueller
17 years ago
Here is a function to colorize a picture gradient style.

All you have to do is to pass an img-object and an array of colors.

e.g.

$arr = array('#000000', '#990000', '#00FFFF', '#FFFFDD');
colorize ($img, $arr);

<?php
function colorize($imgdata, $palette)
{
imageTrueColorToPalette($imgdata,false,0xFF);
$l = count($palette)-1;
$i = imagecolorstotal($imgdata);
while (
$i--)
{
list(
$r,$g,$b) = array_values(imageColorsForIndex($imgdata,$i));

$grayscale = ($r*.3 + $g*.59 +$b*.11) / 0xFF;

$pos = $l*$grayscale;

$perc = $pos-floor($pos);

$tbase = str_replace("#", '', $palette[$pos]);
$baseR = hexdec(substr($tbase,0,2));
$baseG = hexdec(substr($tbase,2,2));
$baseB = hexdec(substr($tbase,4,2));

$tmix = str_replace("#", '', $palette[$pos+1]);
$mixR = hexdec(substr($tmix,0,2));
$mixG = hexdec(substr($tmix,2,2));
$mixB = hexdec(substr($tmix,4,2));

$resR = $baseR+($mixR-$baseR)*$perc;
$resG = $baseG+($mixG-$baseG)*$perc;
$resB = $baseB+($mixB-$baseB)*$perc;

imagecolorset($imgdata, $i, $resR, $resG, $resB);
}
}
?>

<< Back to user notes page

To Top