note 94785 added to function.imagecolorallocate

From: Date: Wed, 25 Nov 2009 00:55:36 +0000
Subject: note 94785 added to function.imagecolorallocate
Groups: php.notes 
Request: Send a blank email to php-notes+get-163255@lists.php.net to get a copy of this message
Note that you can only assign 255 colors to any image palette. If you try assigning more, imagecolorallocate() will fail. If, for example, you are randomly allocating colors, it will be wise to check if you have used up all of the colors possible. You can use imagecolorclosest() to get the closest assigned color: <?php //assign random rgb values $c1 = mt_rand(50,200); //r(ed) $c2 = mt_rand(50,200); //g(reen) $c3 = mt_rand(50,200); //b(lue) //test if we have used up palette if(imagecolorstotal($pic)>=255) { //palette used up; pick closest assigned color $color = imagecolorclosest($pic, $c1, $c2, $c3); } else { //palette NOT used up; assign new color $color = imagecolorallocate($pic, $c1, $c2, $c3); } ?> Also, imagecolorallocate() will assign a new color EVERY time the function is called, even if the color already exists in the palette: <?php // [...] imagecolorallocate($pic,125,125,125); //returns 5 imagecolorallocate($pic,125,125,125); //returns 6 imagecolorallocate($pic,125,125,125); //returns 7 // [...] imagecolorallocate($pic,125,125,125); //returns 23 imagecolorallocate($pic,125,125,125); //returns 25 // [...] // etc... ?> So here, imagecolorexact() is useful: <?php //see if color already exists $color = imagecolorexact($pic, $c1, $c2, $c3); if($color==-1) { //color does not exist; allocate a new one $color = imagecolorallocate($pic, $c1, $c2, $c3); } ?> And, for nerdy-ness sake, we can put the two ideas together: <?php //assign random rgb values $c1 = mt_rand(50,200); //r(ed) $c2 = mt_rand(50,200); //g(reen) $c3 = mt_rand(50,200); //b(lue) //get color from palette $color = imagecolorexact($pic, $c1, $c2, $c3); if($color==-1) { //color does not exist... //test if we have used up palette if(imagecolorstotal($pic)>=255) { //palette used up; pick closest assigned color $color = imagecolorclosest($pic, $c1, $c2, $c3); } else { //palette NOT used up; assign new color $color = imagecolorallocate($pic, $c1, $c2, $c3); } } ?> Or as a function: <?php function createcolor($pic,$c1,$c2,$c3) { //get color from palette $color = imagecolorexact($pic, $c1, $c2, $c3); if($color==-1) { //color does not exist... //test if we have used up palette if(imagecolorstotal($pic)>=255) { //palette used up; pick closest assigned color $color = imagecolorclosest($pic, $c1, $c2, $c3); } else { //palette NOT used up; assign new color $color = imagecolorallocate($pic, $c1, $c2, $c3); } } return $color; } for($i=0; $i<1000; $i++) { //1000 because it is significantly greater than 255 //assign random rgb values $c1 = mt_rand(50,200); //r(ed) $c2 = mt_rand(50,200); //g(reen) $c3 = mt_rand(50,200); //b(lue) //generate the color $color = createcolor($pic,$c1,$c2,$c3); //do something with color... } ?> ---- Server IP: 192.41.40.227 Probable Submitter: 216.85.118.110 ---- Manual Page -- http://www.php.net/manual/en/function.imagecolorallocate.php Edit -- https://master.php.net/note/edit/94785 Del: integrated -- https://master.php.net/note/delete/94785/integrated Del: useless -- https://master.php.net/note/delete/94785/useless Del: bad code -- https://master.php.net/note/delete/94785/bad+code Del: spam -- https://master.php.net/note/delete/94785/spam Del: non-english -- https://master.php.net/note/delete/94785/non-english Del: in docs -- https://master.php.net/note/delete/94785/in+docs Del: other reasons-- https://master.php.net/note/delete/94785 Reject -- https://master.php.net/note/reject/94785 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#163255) next »