/** * @param $hex * @return string * @throws InvalidHexException */ private function getClosestHex($hex) { $RGB = $this->getRGB($hex); $HSL = $this->getHSL($hex); $df = -1; $final = ''; foreach (ColorList::getAll() as $color) { $checkRGB = $this->getRGB($color[0]); $checkHSL = $this->getHSL($color[0]); $rgbDiff = pow($RGB[0] - $checkRGB[0], 2) + pow($RGB[1] - $checkRGB[1], 2) + pow($RGB[2] - $checkRGB[2], 2); $hslDiff = pow($HSL[0] - $checkHSL[0], 2) + pow($HSL[1] - $checkHSL[1], 2) + pow($HSL[2] - $checkHSL[2], 2); $diff = $rgbDiff + $hslDiff * 2; if ($df < 0 || $df > $diff) { $df = $diff; $final = $color[0]; } } if ($final == '') { throw new InvalidHexException($hex); } return $final; }
public function testGetAll() { $all = ColorList::getAll(); $this->assertGreaterThan(1564, count($all)); }