0

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;
    }
heykatieben
  • 269
  • 2
  • 16

1 Answers1

1

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

Community
  • 1
  • 1
heykatieben
  • 269
  • 2
  • 16