我有以下php代码来确定十六进制值与白色的接近程度,如下所示。但接近1的高值是黄色,而不是白色。
例如,#f7f8f8(接近白色)的值是.972,而黄色的值#fff100是1.这里需要进行哪些更改?
function is_verylight($hexcolor) {
$hexcolor = trim($hexcolor, "#");
$r = hexdec(substr($hexcolor, 0 , 2));
$g = hexdec(substr($hexcolor, 2 , 2));
$b = hexdec(substr($hexcolor, 4 , 2));
$hsv = rgb_to_hsv($r, $g, $b);
return ($hsv["V"] > .999);
}
function rgb_to_hsv($R, $G, $B) // RGB Values:Number 0-255
{ // HSV Results:Number 0-1
$HSL = array();
$var_R = ($R / 255);
$var_G = ($G / 255);
$var_B = ($B / 255);
$var_Min = min($var_R, $var_G, $var_B);
$var_Max = max($var_R, $var_G, $var_B);
$del_Max = $var_Max - $var_Min;
$V = $var_Max;
if ($del_Max == 0) {
$H = 0;
$S = 0;
}
else {
$S = $del_Max / $var_Max;
$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++;
if ($H>1) $H--;
}
$HSL['H'] = $H;
$HSL['S'] = $S;
$HSL['V'] = $V;
return $HSL;
}
答案 0 :(得分:1)
HSV是错误的方式。我从Stack Overflow中得到了这个答案 - 这有效:Formula to determine brightness of RGB color