note 86801 added to function.imagecolorsforindex

From: Date: Tue, 04 Nov 2008 14:19:16 +0000
Subject: note 86801 added to function.imagecolorsforindex
Groups: php.notes 
Request: Send a blank email to php-notes+get-146118@lists.php.net to get a copy of this message
I've created a function to retrieve the HSV (HSB) value of an RGB color. It's useful if you need a HSV representation of color instead the HSL one. The RGB array passed as parameter, you can obtain it from the ImageColorsForIndex() function. If not, you can pass any array, you must replace the indexes from $rgb['xxx'] to suit your array. <?php //---------------------------------------------------------- // Name: rgb2hsv() // Description: Convert RGB colors array // into HSV (HSB) array // // Parameter: - $rgb: // array of RGB color, // each component with range 0 to 255 // Return: - array($h, $s, $v): // array HSV set with its default range // (H to 360º, S and V to 100%) //--------------------------------------------------------- function rgb2hsv($rgb){ $r = $rgb['red']; $g = $rgb['green']; $b = $rgb['blue']; $minVal = min($r, $g, $b); $maxVal = max($r, $g, $b); $delta = $maxVal - $minVal; $v = $maxVal / 255; if ($delta == 0) { $h = 0; $s = 0; } else { $s = $delta / $maxVal; $del_R = ((($maxVal - $r) / 6) + ($delta / 2)) / $delta; $del_G = ((($maxVal - $g) / 6) + ($delta / 2)) / $delta; $del_B = ((($maxVal - $b) / 6) + ($delta / 2)) / $delta; if ($r == $maxVal){ $h = $del_B - $del_G; } else if ($g == $maxVal) { $h = (1 / 3) + $del_R - $del_B; } else if ($b == $maxVal) { $h = (2 / 3) + $del_G - $del_R; } if ($h < 0){ $h++; } if ($h > 1) { $h--; } } $h = round($h * 360); $s = round($s * 100); $v = round($v * 100); return array($h, $s, $v); } ?> ---- Server IP: 200.24.234.70 Probable Submitter: 152.74.32.86 ---- Manual Page -- http://www.php.net/manual/en/function.imagecolorsforindex.php Edit -- https://master.php.net/note/edit/86801 Del: integrated -- https://master.php.net/note/delete/86801/integrated Del: useless -- https://master.php.net/note/delete/86801/useless Del: bad code -- https://master.php.net/note/delete/86801/bad+code Del: spam -- https://master.php.net/note/delete/86801/spam Del: non-english -- https://master.php.net/note/delete/86801/non-english Del: in docs -- https://master.php.net/note/delete/86801/in+docs Del: other reasons-- https://master.php.net/note/delete/86801 Reject -- https://master.php.net/note/reject/86801 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#146118) next »