php - PHP中的RGB到HSV

标签 php image colors rgb hsv

在 PHP 中,将 RGB 三元组转换为 HSV 值的最直接方法是什么?

最佳答案

这是一个简单直接的方法,它以度数和百分比的形式返回 HSV 值,这是 Photoshop 的颜色选择器使用的。

请注意,返回值不会四舍五入,如果需要,您可以自己进行。请记住 H(360) == H(0),因此 359.5 及更大的 H 值应四舍五入为 0

大量记录用于学习目的。

/**
 * Licensed under the terms of the BSD License.
 * (Basically, this means you can do whatever you like with it,
 *   but if you just copy and paste my code into your app, you
 *   should give me a shout-out/credit :)
 */

<?php

function RGBtoHSV($R, $G, $B)    // RGB values:    0-255, 0-255, 0-255
{                                // HSV values:    0-360, 0-100, 0-100
    // Convert the RGB byte-values to percentages
    $R = ($R / 255);
    $G = ($G / 255);
    $B = ($B / 255);

    // Calculate a few basic values, the maximum value of R,G,B, the
    //   minimum value, and the difference of the two (chroma).
    $maxRGB = max($R, $G, $B);
    $minRGB = min($R, $G, $B);
    $chroma = $maxRGB - $minRGB;

    // Value (also called Brightness) is the easiest component to calculate,
    //   and is simply the highest value among the R,G,B components.
    // We multiply by 100 to turn the decimal into a readable percent value.
    $computedV = 100 * $maxRGB;

    // Special case if hueless (equal parts RGB make black, white, or grays)
    // Note that Hue is technically undefined when chroma is zero, as
    //   attempting to calculate it would cause division by zero (see
    //   below), so most applications simply substitute a Hue of zero.
    // Saturation will always be zero in this case, see below for details.
    if ($chroma == 0)
        return array(0, 0, $computedV);

    // Saturation is also simple to compute, and is simply the chroma
    //   over the Value (or Brightness)
    // Again, multiplied by 100 to get a percentage.
    $computedS = 100 * ($chroma / $maxRGB);

    // Calculate Hue component
    // Hue is calculated on the "chromacity plane", which is represented
    //   as a 2D hexagon, divided into six 60-degree sectors. We calculate
    //   the bisecting angle as a value 0 <= x < 6, that represents which
    //   portion of which sector the line falls on.
    if ($R == $minRGB)
        $h = 3 - (($G - $B) / $chroma);
    elseif ($B == $minRGB)
        $h = 1 - (($R - $G) / $chroma);
    else // $G == $minRGB
        $h = 5 - (($B - $R) / $chroma);

    // After we have the sector position, we multiply it by the size of
    //   each sector's arc (60 degrees) to obtain the angle in degrees.
    $computedH = 60 * $h;

    return array($computedH, $computedS, $computedV);
}

?>

关于php - PHP中的RGB到HSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1773698/

相关文章:

Android:将 <img> 标签替换为 ImageViews

ios - Storyboard中的颜色与 UIColor 不匹配

php - 使用 .htaccess 的 Wordpress 自定义永久链接

php - 在本地主机中打开 10 月 cms

image - matplotlib imshow():如何制作动画?

image - 在 MATLAB 中读取 MNIST 图像数据库二进制文件

vb.net - 根据项目更改一行 ListView 的颜色

javascript - 试图从数组中检索对象的颜色

php - 使用时间戳过滤查询?

php - 当我转换时,网站可以是 mysql 和 mysqli 的一部分吗