当前位置:Gxlcms > PHP教程 > 给定一个颜色值,如何把它转换成一个深一点的值

给定一个颜色值,如何把它转换成一个深一点的值

时间:2021-07-01 10:21:17 帮助过:10人阅读

给定一个颜色值,怎么把它转换成一个深一点的值?
a = "#333333"
需要这个函数:把a的色彩变深一点,比如a="#222222"

------解决思路----------------------
$a = "#333333";
$rgb = unpack('C*', pack('H*', substr($a, 1)));
$hsb = call_user_func_array('rgb2hsb', $rgb);
$hsb[2] *= 0.67;
$rgb = call_user_func_array('hsb2rgb', $hsb);
echo '#' . join('', array_map('dechex', $rgb));


function rgb2hsb($rgbR, $rgbG, $rgbB) {
$rgbR &= 255;
$rgbG &= 255;
$rgbB &= 255;
$rgb = array($rgbR, $rgbG, $rgbB );
sort($rgb);
$max = $rgb[2];
$min = $rgb[0];

$hsbB = $max / 255.0;
$hsbS = $max == 0 ? 0 : ($max - $min) / $max;

$hsbH = 0;
if ($max == $rgbR && $rgbG >= $rgbB) {
$hsbH = ($rgbG - $rgbB) * 60 / (($m = $max - $min) ? $m : 1);
} else if ($max == $rgbR && $rgbG < $rgbB) {
$hsbH = ($rgbG - $rgbB) * 60 / ($max - $min) + 360;
} else if ($max == $rgbG) {
$hsbH = ($rgbB - $rgbR) * 60 / ($max - $min) + 120;
} else if ($max == $rgbB) {
$hsbH = ($rgbR - $rgbG) * 60 / ($max - $min) + 240;
}

return array( $hsbH, $hsbS, $hsbB );
}

function hsb2rgb($h, $s, $v) {
$r = $g = $b = 0;
$i = ($h / 60) % 6;
$f = ($h / 60) - $i;
$p = $v * (1 - $s);
$q = $v * (1 - $f * $s);
$t = $v * (1 - (1 - $f) * $s);
switch ($i) {
case 0:
$r = $v;
$g = $t;
$b = $p;
break;
case 1:
$r = $q;
$g = $v;
$b = $p;
break;
case 2:
$r = $p;
$g = $v;
$b = $t;
break;
case 3:
$r = $p;
$g = $q;
$b = $v;
break;
case 4:
$r = $t;
$g = $p;
$b = $v;
break;
case 5:
$r = $v;
$g = $p;
$b = $q;
break;
default:
break;
}
return array( intval($r * 255), intval($g * 255), intval($b * 255) );
}
#222222

人气教程排行