Php 如何根据另一种颜色计算某种颜色?

Php 如何根据另一种颜色计算某种颜色?,php,javascript,math,colors,Php,Javascript,Math,Colors,例如,我有一个蓝色: 049cd9或rgba(4156218) 我如何计算相应的颜色,在这种情况下,它将是深蓝色: #004ea0或rgba(0,78,160) ? 通常我不知道第二种颜色(我想知道),所以我想在第一种颜色的基础上找到一种更暗的颜色 是否有一个公式或东西,我可以通过某种方式减去这两种颜色来生成 所以我发现: 函数十六进制到色调($hexcode) { $redhex=substr($hexcode,0,2); $greenhex=substr($hexcode,2,2); $

例如,我有一个蓝色:

049cd9
rgba(4156218)

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

#004ea0
rgba(0,78,160)

?

通常我不知道第二种颜色(我想知道),所以我想在第一种颜色的基础上找到一种更暗的颜色

是否有一个公式或东西,我可以通过某种方式减去这两种颜色来生成


所以我发现:

函数十六进制到色调($hexcode)
{
$redhex=substr($hexcode,0,2);
$greenhex=substr($hexcode,2,2);
$bluehex=substr($hexcode,4,2);
//$var_r、$var_g和$var_b是要输入我们的RGB到HSL转换例程的三个十进制分数
$var_r=(hexdec($redhex))/255;
$var_g=(hexdec($greenhex))/255;
$var_b=(hexdec($bluehex))/255;
//从上面输入$var_r、$var_g和$var_b
//输出是HSL等价于$h、$s和$l——它们同样表示为1的分数,就像输入值一样
$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\u max+$var\u min)/2;
如果($del_max==0){
$h=0;
$s=0;
}否则{
如果($l<0.5){
$s=$del_max/($var_max+$var_min);
}否则{
$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;
如果($var\u r==$var\u max){
$h=$del_b-$del_g;
}否则如果($var\u g==$var\u max){
$h=(1/3)+$del_r-$del_b;
}否则如果($var\u b==$var\u max){
$h=(2/3)+$del_g-$del_r;
}
;
如果($h<0){
$h+=1;
}
;
如果($h>1){
$h-=1;
}
;
}
;
返回数组($h、$s、$l);
/*
//计算相反的色调,$h2
$h2=$h+0.5;
如果($h2>1)
{
$h2-=1;
};
返回数组($h2,$s,$l);
*/
}
函数hue\u to\u hex($hue=array())
{
功能色调2_rgb($v1、$v2、$vh)
{
如果($vh<0){
$vh+=1;
}
;
如果($vh>1){
$vh-=1;
}
;
如果((6*$vh)<1){
回报($v1+($v2-$v1)*6*$vh);
}
;
如果((2*$vh)<1){
回报(2美元);
}
;
如果((3*$vh)<2){
回报($v1+($v2-$v1)*((2/3-$vh)*6));
}
;
回报(1美元);
}
;
列表($h2,$s,$l)=$hue;
//输入是互补色的HSL值,以$h2、$s、$l作为1的分数表示
//输出为正常255格式的RGB,保存在$r、$g、$b中
//色调是使用函数Hue_2_rgb转换的,如本代码末尾所示
如果($s==0){
$r=$l*255;
$g=$l*255;
$b=$l*255;
}否则{
如果($l<0.5){
$var_2=$l*(1+s);
}否则{
$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”,第四轮($r));
$ghex=sprintf(“%02X”,圆形($g));
$bhex=sprintf(“%02X”,圆($b));
返回$rhex.$ghex.$bhex;
}
它们可以工作,因为我通过来回转换颜色来测试它们

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


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

有一些公式可以将RGB颜色转换为HSV(色调、饱和度和值)。从HSV,您可以更改任何HSV组件,然后转换回RGB。我在网上找到了一些东西,以前也这么做过。如果您想了解算法的更多细节,请告诉我,如果您愿意,我可以为您挖掘。

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

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

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);
    }
有关示例代码,请参见此答案:

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

函数hex\u to\u色调($hexcode,$percent){
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);
    }
$redhex=substr($hexcode,0,2); $greenhex=substr($hexcode,2,2); $bluehex=substr($hexcode,4,2); //$var_r、$var_g和$var_b是要输入我们的RGB到HSL转换例程的三个十进制分数 $var_r=(hexdec($redhex))/255; $var_g=(hexdec($greenhex))/255; $var_b=(hexdec($bluehex))/255; //从上面输入$var_r、$var_g和$var_b //输出是HSL等价于$h、$s和$l——它们同样表示为1的分数,就像输入值一样 $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\u max+$var\u min)/2; 如果($del_max==0){ $h=0; $s=0;