1

I am trying to refrain from using a library to do this but I am sending a method a hex color as a string and I'd like to determine if it's a white (or near white) color and if so throw an exception.

$color = $request->input('color') // '#FFFFFF'

doing so for white ( like above ), or any single color, would be pretty self explanatory but I can't figure out how to map out a bunch of light colors without explicitly creating an array with every light color

ddjk
  • 133
  • 2
  • 7

1 Answers1

0

There are probably 100 ways to do what you are asking, this is the first that came to mind.

$color = "#FAFBFE";
$numVal = hexdec(substr($color, 1));

$red = ($numVal & 0xFF0000) >> 16;
$green = ($numVal & 0xFF00) >> 8;
$blue = $numVal & 0xFF;

if ($red >= 225 && $green >= 225 && $blue >= 225) {
   // Close enough
}
Alex Barker
  • 4,316
  • 4
  • 28
  • 47