开发者

PHP Work out colour saturation

开发者 https://www.devze.com 2022-12-22 11:50 出处:网络
lets say i have the following RGB values: R:129 G:98 B:87 Photoshop says the saturation of that colour is 33%

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 o开发者_开发知识库ut that percentage using PHP and the RGB values?


See 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 == 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.


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];    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号