Voting

: seven minus six?
(Example: nine)

The Note You're Voting On

Bartman
15 years ago
I had the same problem as heavyraptor2 so i made this function...
<?php
function imagecolorize($im,$endcolor){
//funcion takes image and turns black into $endcolor, white to white and
//everything in between in corresponding gradient
//$endcolor should be 6 char html color

//make sure it has usable palette
if (imageistruecolor($im)) {
imagetruecolortopalette($im, false, 256);
}

//first make it gray to be sure of even results (thanks moxleystratton.com)
//comment this loop if you want the output based on, for example,
//the red channel (for this take a look at the $gray-var in the last loop)
for ($c = 0; $c < imagecolorstotal($im); $c++) {
$col = imagecolorsforindex($im, $c);
$gray = round(0.299 * $col['red'] + 0.587 * $col['green'] + 0.114 * $col['blue']);
imagecolorset($im, $c, $gray, $gray, $gray);
}

//determine end-colors in hexdec
$EndcolorRGB['r'] = hexdec( substr($endcolor, 0, 2));
$EndcolorRGB['g'] = hexdec( substr($endcolor, 2, 2));
$EndcolorRGB['b'] = hexdec( substr($endcolor, 4, 2));

//determine gradient-delta's
$stepR = (255-$EndcolorRGB['r'])/255.0;
$stepG = (255-$EndcolorRGB['g'])/255.0;
$stepB = (255-$EndcolorRGB['b'])/255.0;

//aColor contains the 256 gradations between endcolor(i=0) and white(i=255)
$aColor = array();
for (
$i = 0; $i<=255; $i++){
$aColor[$i]['r'] = $EndcolorRGB['r'] + ($i*$stepR);
$aColor[$i]['g'] = $EndcolorRGB['g'] + ($i*$stepG);
$aColor[$i]['b'] = $EndcolorRGB['b'] + ($i*$stepB);
}

//for every color-index we now replace $gray-values for $aColor
for ($c = 0; $c < imagecolorstotal($im); $c++){
$currentColorRGB = imagecolorsforindex($im, $c);
$gray = $currentColorRGB['red'];//image is grayscale, so red,green and blue
// should be equal. We use this number as key of aColor
imagecolorset($im,$c,(int)$aColor[$gray]['r'], (int)$aColor[$gray]['g'], (int)$aColor[$gray]['b']);
}
}
?>

<< Back to user notes page

To Top