Reputation: 10643
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?
Upvotes: 0
Views: 2202
Reputation: 50314
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 == 0 ? 0 : (($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.
Upvotes: 3
Reputation: 95424
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];
Upvotes: 0