dpda53918 2014-10-28 11:58
浏览 197

如何确定十六进制值的白色接近程度?

I have the following php code to determine how close a hex value is to white, below. But the high values, close to 1, are yellow, not white.

For instance, the value of #f7f8f8 (close to white) is .972, while the value of yellow, #fff100, is 1. What change is needed here?

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;
    }
  • 写回答

1条回答 默认 最新

  • dox90448 2014-10-28 12:20
    关注

    HSV is the wrong way to go. I went with this answer from Stack Overflow - this works: Formula to determine brightness of RGB color

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置