tags:

views:

178

answers:

3

Hi there, lets say i have the following RGB values:

R:129 G:98 B:87

Photoshop says the saturation of that colour is 33%

How would i work out that percentage using PHP and the RGB values?

+2  A: 

See http://stackoverflow.com/questions/1773698/rgb-to-hsv-in-php

Taking only the saturation bits from that code, and converting into a percentage:

function saturation($R, $G, $B) {  // 0-255
     $Min = min($R, $G, $B);
     $Max = max($R, $G, $B);
     return (($Max - $Min) / $Max) * 100;
}

Alternately you could use the original code in the link above - the HSV values it returns are between 0.0 and 1.0, so you just need to multiply the saturation value by 100 to get your percentage.

rjh
Perfect :) Thanks for rewriting it to be much smaller, running this in every iteration of a 600 * 600 loop :P
Ozzy
A: 

PEAR (PHP Extensions And Application Repository) has a nice package called Image_Color2 which allows you do to quick conversions between different color models:

include "Image/Color2.php";

$color = new Image_Color2(array(129,98,87));
$hsv = $color->convertTo('hsv');
$hsvArray = $hsv->getArray();

echo "Hue is " . $hsvArray[0] . "\n";
echo "Saturation is: " . $hsvArray[1] . "\n";
echo "Brightness is: " . $hsvArray[2];    
Andrew Moore
A: 

what about http://php.net/manual/de/function.hexdec.php ? as a start?

Steve