PHP 计算颜色饱和度

发布于 2024-08-23 11:47:53 字数 113 浏览 6 评论 0原文

假设我有以下 RGB 值:

R:129 重力:98 B:87

Photoshop 说该颜色的饱和度为 33%

我如何使用 PHP 和 RGB 值计算出该百分比?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作妖 2024-08-30 11:47:53

请参阅 PHP 中的 RGB 到 HSV

仅从该代码中获取饱和度位,然后转换为百分比:

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;
}

或者,您可以使用上面链接中的原始代码 - 它返回的 HSV 值介于 0.0 和 1.0 之间,因此您只需将饱和度值乘以 100 即可获得百分比。

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.

夜无邪 2024-08-30 11:47:53

PEAR(PHP 扩展和应用程序存储库) 有一个很好的包,名为 Image_Color2 允许您在不同颜色模型之间快速转换:

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

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];    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文