【问题标题】:How to calculate a certain color based on another color?如何根据另一种颜色计算某种颜色?
【发布时间】:2011-12-22 01:28:30
【问题描述】:

例如我有一个蓝色:

#049cd9rgba(4, 156, 218)

我如何计算对应的颜色,在这种情况下它将是深蓝色:

#004ea0rgba(0, 78, 160)

?

通常我不知道第二种颜色(我想知道),所以我想找到一种方法来根据第一种颜色获得更深的颜色。

我可以通过某种方式减去两种颜色来生成公式或其他东西吗?


所以我找到了HEX to HSL and HSL to HEX functions

function hex_to_hue($hexcode)
{
    $redhex  = substr($hexcode,0,2);
    $greenhex = substr($hexcode,2,2);
    $bluehex = substr($hexcode,4,2);

    // $var_r, $var_g and $var_b are the three decimal fractions to be input to our RGB-to-HSL conversion routine
    $var_r = (hexdec($redhex)) / 255;
    $var_g = (hexdec($greenhex)) / 255;
    $var_b = (hexdec($bluehex)) / 255;

    // Input is $var_r, $var_g and $var_b from above
    // Output is HSL equivalent as $h, $s and $l — these are again expressed as fractions of 1, like the input values

    $var_min = min($var_r,$var_g,$var_b);
    $var_max = max($var_r,$var_g,$var_b);
    $del_max = $var_max - $var_min;

    $l = ($var_max + $var_min) / 2;

    if ($del_max == 0) {
        $h = 0;
        $s = 0;
    } else {
        if ($l < 0.5) {
            $s = $del_max / ($var_max + $var_min);
        } else {
            $s = $del_max / (2 - $var_max - $var_min);
        }
        ;

        $del_r = ((($var_max - $var_r) / 6) + ($del_max / 2)) / $del_max;
        $del_g = ((($var_max - $var_g) / 6) + ($del_max / 2)) / $del_max;
        $del_b = ((($var_max - $var_b) / 6) + ($del_max / 2)) / $del_max;

        if ($var_r == $var_max) {
            $h = $del_b - $del_g;
        } else if ($var_g == $var_max) {
            $h = (1 / 3) + $del_r - $del_b;
        } else if ($var_b == $var_max) {
            $h = (2 / 3) + $del_g - $del_r;
        }
        ;

        if ($h < 0) {
            $h += 1;
        }
        ;

        if ($h > 1) {
            $h -= 1;
        }
        ;
    }
    ;

    return array($h, $s, $l);

    /*
// Calculate the opposite hue, $h2
$h2 = $h + 0.5;
if ($h2 > 1)
{
$h2 -= 1;
};

return array($h2, $s, $l);
*/

}



function hue_to_hex($hue = array())
{
    function hue_2_rgb($v1,$v2,$vh)
    {
        if ($vh < 0) {
            $vh += 1;
        }
        ;

        if ($vh > 1) {
            $vh -= 1;
        }
        ;

        if ((6 * $vh) < 1) {
            return($v1 + ($v2 - $v1) * 6 * $vh);
        }
        ;

        if ((2 * $vh) < 1) {
            return($v2);
        }
        ;

        if ((3 * $vh) < 2) {
            return($v1 + ($v2 - $v1) * ((2 / 3 - $vh) * 6));
        }
        ;

        return($v1);
    }
    ;


    list($h2, $s, $l) = $hue;

    // Input is HSL value of complementary colour, held in $h2, $s, $l as fractions of 1
    // Output is RGB in normal 255 255 255 format, held in $r, $g, $b
    // Hue is converted using function hue_2_rgb, shown at the end of this code

    if ($s == 0) {
        $r = $l * 255;
        $g = $l * 255;
        $b = $l * 255;
    } else {
        if ($l < 0.5) {
            $var_2 = $l * (1 + $s);
        } else {
            $var_2 = ($l + $s) - ($s * $l);
        }
        ;

        $var_1 = 2 * $l - $var_2;
        $r = 255 * hue_2_rgb($var_1,$var_2,$h2 + (1 / 3));
        $g = 255 * hue_2_rgb($var_1,$var_2,$h2);
        $b = 255 * hue_2_rgb($var_1,$var_2,$h2 - (1 / 3));
    }
    ;


    $rhex = sprintf("%02X",round($r));
    $ghex = sprintf("%02X",round($g));
    $bhex = sprintf("%02X",round($b));

    return $rhex.$ghex.$bhex;
}

它们之所以有效,是因为我通过来回转换颜色来测试它们。

但我不知道如何像在 Photoshop 中一样更改色相和亮度属性? 深色为 H +13 和 L -28。

上面的 hex_to_hsl 函数返回 0 到 1 之间的浮点值...

【问题讨论】:

  • 你到底是什么意思?什么是“对应颜色”?
  • 什么定义了相应的颜色?您只是想获得更深的颜色吗?
  • 是的,类似的
  • 您需要更清楚地描述您想要什么,或者提供更大的样本集以便我们尝试进行模式匹配。
  • 我能想到的最简单的变暗形式是除以 2。您将保持 rgb 的比例,但它会更暗。它与您提供的颜色不匹配。

标签: php javascript math colors


【解决方案1】:

有一些公式可以将 RGB 颜色转换为 HSV(色相、饱和度和数值)。您可以从 HSV 更改任何 HSV 组件,然后转换回 RGB。我在网上找了一些东西,以前也做过。如果您需要有关算法的更多详细信息,请告诉我,如果您愿意,我可以为您挖掘它们。

【讨论】:

  • 您是如何想到(出于好奇)问题是关于 RGB 和 HSV 颜色空间之间的来回映射?
  • 谢谢,我可以自己搜索,我只是想知道如何做到这一点:)
  • 啊,我明白了 - 转换为 HSV,减少“V”,然后转换回来 :-)
  • @Pointy 转换为 HSV 是我所知道的唯一可以让您采用 RGB 颜色并使其变暗或变亮的方法。
  • 您可以做的另一件事是在 RGB 颜色空间中从起始颜色返回 (0, 0, 0) 的“直线”上插值。
【解决方案2】:

#XXXXXX代表十六进制数,颜色为红、绿、蓝,每两个字符从左到右,增加数字会变亮,如果减少数字会变暗。

【讨论】:

  • Dev,其实并不需要强调
【解决方案3】:

您需要将 RGB 值转换为 HSL 或 HSV,然后您可以根据需要减小 L(亮度)或 V(值)分量,然后再转换回 RGB。

请参阅此答案以获取示例代码: RGB to HSV in PHP

【讨论】:

  • 谢谢,你知道一个可以逆转这个的函数吗? hsv 转 rgb?
【解决方案4】:

修改颜色感知方式(即更亮、更暗、更亮、更暗等)的最简单方法是将其转换为 HSL。在PHPJavaScript 中有大量用于将RGB 转换为HSL 并再次转换回来的资源。谷歌会找到你想要的尽可能多的实现。然后为了降低亮度,降低 L 值(乘以 0.75 或类似值)并转换回 RGB。

【讨论】:

    【解决方案5】:
    function hex_to_hue($hexcode, $percent) {
            $redhex = substr($hexcode, 0, 2);
            $greenhex = substr($hexcode, 2, 2);
            $bluehex = substr($hexcode, 4, 2);
    
            // $var_r, $var_g and $var_b are the three decimal fractions to be input to our RGB-to-HSL conversion routine
            $var_r = (hexdec($redhex)) / 255;
            $var_g = (hexdec($greenhex)) / 255;
            $var_b = (hexdec($bluehex)) / 255;
    
            // Input is $var_r, $var_g and $var_b from above
            // Output is HSL equivalent as $h, $s and $l — these are again expressed as fractions of 1, like the input values
    
            $var_min = min($var_r, $var_g, $var_b);
            $var_max = max($var_r, $var_g, $var_b);
            $del_max = $var_max - $var_min;
    
            $l = ($var_max + $var_min) / 2;
    
            if ($del_max == 0) {
                $h = 0;
                $s = 0;
            } else {
                if ($l < 0.5) {
                    $s = $del_max / ($var_max + $var_min);
                } else {
                    $s = $del_max / (2 - $var_max - $var_min);
                }
                ;
    
                $del_r = ((($var_max - $var_r) / 6) + ($del_max / 2)) / $del_max;
                $del_g = ((($var_max - $var_g) / 6) + ($del_max / 2)) / $del_max;
                $del_b = ((($var_max - $var_b) / 6) + ($del_max / 2)) / $del_max;
    
                if ($var_r == $var_max) {
                    $h = $del_b - $del_g;
                } else if ($var_g == $var_max) {
                    $h = (1 / 3) + $del_r - $del_b;
                } else if ($var_b == $var_max) {
                    $h = (2 / 3) + $del_g - $del_r;
                }
                ;
    
                if ($h < 0) {
                    $h += 1;
                }
                ;
    
                if ($h > 1) {
                    $h -= 1;
                }
                ;
            }
            ;
    
            //return array($h, $s, $l);
    // Calculate the opposite hue, $h2
            $h2 = $h + $percent;
            if ($h2 > 1) {
                $h2 -= 1;
            }
    
    // Calculate the opposite hue, $s2
            $s2 = $s + $percent;
            if ($s2 > 1) {
                $s2 -= 1;
            }
    
    // Calculate the opposite hue, $s2
            $l2 = $l + $percent;
            if ($l2 > 1) {
                $l2 -= 1;
            }
            return array($h2, $s2, $l2);
        }
    

    【讨论】:

    • 请在您的答案中添加一些详细信息,解释它如何回答问题。
    猜你喜欢
    • 1970-01-01
    • 2012-02-18
    • 2013-08-02
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    相关资源
    最近更新 更多