note 93134 added to function.imagecolorset
| From: | Bartman at osu1 dot php dot net | Date: | Tue, 25 Aug 2009 09:12:07 +0000 |
| Subject: | note 93134 added to function.imagecolorset | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159861@lists.php.net to get a copy of this message | ||
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']);
}
}?>
----
Server IP: 209.41.74.194
Probable Submitter: 86.89.188.74
----
Manual Page -- http://www.php.net/manual/en/function.imagecolorset.php
Edit -- https://master.php.net/note/edit/93134
Del: integrated -- https://master.php.net/note/delete/93134/integrated
Del: useless -- https://master.php.net/note/delete/93134/useless
Del: bad code -- https://master.php.net/note/delete/93134/bad+code
Del: spam -- https://master.php.net/note/delete/93134/spam
Del: non-english -- https://master.php.net/note/delete/93134/non-english
Del: in docs -- https://master.php.net/note/delete/93134/in+docs
Del: other reasons-- https://master.php.net/note/delete/93134
Reject -- https://master.php.net/note/reject/93134
Search -- https://master.php.net/manage/user-notes.php