note 46674 added to function.imagecolorat

From: Date: Tue, 19 Oct 2004 21:14:12 +0000
Subject: note 46674 added to function.imagecolorat
Groups: php.notes 
Request: Send a blank email to php-notes+get-78792@lists.php.net to get a copy of this message
In my various play stages with photomosaics I ended up writing this function that works with GD 2. The conversion from RGB to HSL is straightforward and there are a number of papers on it on our beloved Internet; however, this function simply finds the lightness value for a pixel and works exactly the same as imagecolorat(). <?php /* Computes the lightness of a pixel. Lightness is used in HSL notation, as well * as in various operations on images. This function returns a normalized value * for lightness between 0 and 1, inclusive (0.0 being black, 1.0 being white). * * double imagelightnessat(resource img, int x, int y) * img An open image resource to operate on (true color or palette) * x, y The coordinates of the pixel to work on * * by Jed Smith <?php $u = "jed"; $d = "bz"; printf("<%s@%s.%s>", $u, $u, $d) ?> */ function imagelightnessat($img, $x, $y) { if(!is_resource($img)) { trigger_error("imagelightnessat(): supplied argument is not a valid " . "Image resource", E_USER_WARNING); return 0.0; } $c = @imagecolorat($img, $x, $y); if($c === false) return false; if(imageistruecolor($img)) { $red = ($c >> 16) & 0xFF; $green = ($c >> 8) & 0xFF; $blue = $c & 0xFF; } else { $i = imagecolorsforindex($img, $c); $red = $i['red']; $green = $i['green']; $blue = $i['blue']; } $m = min($red, $green, $blue); $n = max($red, $green, $blue); /* Because RGB isn't normalized in GD, we divide by 510 here. * Lightness = (Max(RGB) + Min(RGB)) / 2 * But that's assuming red, green, and blue are 0 through 1 inclusive. * Red, green, and blue are actually 0-255 (255 + 255 = 510). */ $lightness = (double)(($m + $n) / 510.0); return($lightness); } ?> ---- Manual Page -- http://www.php.net/manual/en/function.imagecolorat.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46674 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46674&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46674&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46674&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46674&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46674&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46674&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+46674&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#78792) next »