public function render() { //Example ImagickPixel::isSimilar // The tests below are written with the maximum distance expressed as 255 // so we need to scale them by the square root of 3 - the diagonal length // of a unit cube. $root3 = 1.732050807568877; $tests = array(['rgb(245, 0, 0)', 'rgb(255, 0, 0)', 9 / $root3, false], ['rgb(245, 0, 0)', 'rgb(255, 0, 0)', 10 / $root3, true], ['rgb(0, 0, 0)', 'rgb(7, 7, 0)', 9 / $root3, false], ['rgb(0, 0, 0)', 'rgb(7, 7, 0)', 10 / $root3, true], ['rgba(0, 0, 0, 1)', 'rgba(7, 7, 0, 1)', 9 / $root3, false], ['rgba(0, 0, 0, 1)', 'rgba(7, 7, 0, 1)', 10 / $root3, true], ['rgb(128, 128, 128)', 'rgb(128, 128, 120)', 7 / $root3, false], ['rgb(128, 128, 128)', 'rgb(128, 128, 120)', 8 / $root3, true], ['rgb(0, 0, 0)', 'rgb(255, 255, 255)', 254.9, false], ['rgb(0, 0, 0)', 'rgb(255, 255, 255)', 255, true], ['rgb(255, 0, 0)', 'rgb(0, 255, 255)', 254.9, false], ['rgb(255, 0, 0)', 'rgb(0, 255, 255)', 255, true], ['black', 'rgba(0, 0, 0)', 0.0, true], ['black', 'rgba(10, 0, 0, 1.0)', 10.0 / $root3, true]); $output = "<table width='100%' class='infoTable'><thead>\n <tr>\n <th>\n Color 1\n </th>\n <th>\n Color 2\n </th>\n <th>\n Test distance * 255\n </th>\n <th>\n Is within distance\n </th>\n </tr>\n </thead>"; $output .= "<tbody>"; foreach ($tests as $testInfo) { $color1 = $testInfo[0]; $color2 = $testInfo[1]; $distance = $testInfo[2]; $expectation = $testInfo[3]; $testDistance = $distance / 255.0; $color1Pixel = new \ImagickPixel($color1); $color2Pixel = new \ImagickPixel($color2); $isSimilar = $color1Pixel->isPixelSimilar($color2Pixel, $testDistance); if ($isSimilar !== $expectation) { echo "Test distance failed. Color [{$color1}] compared to color [{$color2}] is not within distance {$testDistance} FAILED." . NL; } $layout = "<tr>\n <td>%s</td>\n <td>%s</td>\n <td>%s</td>\n <td style='text-align: center;'>%s</td>\n </tr>"; $output .= sprintf($layout, $color1, $color2, $distance, $isSimilar ? 'yes' : 'no'); } $output .= "</tbody></table>"; return $output; //Example end }
<?php $root3 = 1.732050807568877; $tests = array(array('rgb(245, 0, 0)', 'rgb(255, 0, 0)', 9 / $root3, false), array('rgb(245, 0, 0)', 'rgb(255, 0, 0)', 10 / $root3, true), array('rgb(0, 0, 0)', 'rgb(7, 7, 0)', 9 / $root3, false), array('rgb(0, 0, 0)', 'rgb(7, 7, 0)', 10 / $root3, true), array('rgba(0, 0, 0, 1)', 'rgba(7, 7, 0, 1)', 9 / $root3, false), array('rgba(0, 0, 0, 1)', 'rgba(7, 7, 0, 1)', 10 / $root3, true), array('rgb(128, 128, 128)', 'rgb(128, 128, 120)', 7 / $root3, false), array('rgb(128, 128, 128)', 'rgb(128, 128, 120)', 8 / $root3, true), array('rgb(0, 0, 0)', 'rgb(255, 255, 255)', 254.9, false), array('rgb(0, 0, 0)', 'rgb(255, 255, 255)', 255, true), array('rgb(255, 0, 0)', 'rgb(0, 255, 255)', 254.9, false), array('rgb(255, 0, 0)', 'rgb(0, 255, 255)', 255, true), array('black', 'rgba(0, 0, 0)', 0.0, true), array('black', 'rgba(10, 0, 0, 1.0)', 10.0 / $root3, true)); try { foreach ($tests as $testInfo) { $color1 = $testInfo[0]; $color2 = $testInfo[1]; $distance = $testInfo[2]; $expectation = $testInfo[3]; $testDistance = $distance / 255.0 + 1.0E-8; $color1Pixel = new ImagickPixel($color1); $color2Pixel = new ImagickPixel($color2); $isSimilar = $color1Pixel->isPixelSimilar($color2Pixel, $testDistance); if ($isSimilar !== $expectation) { echo "isPixelSimilar failed. Color [{$color1}] compared to color [{$color2}]" . " is not within distance {$testDistance}." . PHP_EOL; } } echo "success"; } catch (\Exception $e) { echo "Exception caught in ImagickPixel::isPixelSimilar test: " . $e->getMessage() . PHP_EOL; }
try { $pixel = new ImagickPixel('the-best-color-in-the-world'); } catch (ImagickPixelException $ex) { echo "__construct\n"; } $pixel = new ImagickPixel('white'); try { $pixel->setColor('the-worst-color-in-the-world'); } catch (ImagickPixelException $ex) { echo "setColor\n"; } define('IMAGICK_COLOR_INVALID', -1); try { $pixel->getColorValue(IMAGICK_COLOR_INVALID); } catch (ImagickPixelException $ex) { echo "getColorValue\n"; } try { $pixel->setColorValue(IMAGICK_COLOR_INVALID, 0); } catch (ImagickPixelException $ex) { echo "setColorValue\n"; } try { $pixel->isPixelSimilar(new ImagickPixelException(), 0); } catch (ImagickPixelException $ex) { echo "isPixelSimilar\n"; } ?> ==DONE==
<?php $sqrt3 = sqrt(3); $pixel = new ImagickPixel('red'); var_dump($pixel->isPixelSimilar($pixel, 0)); var_dump($pixel->isPixelSimilar('red', 0)); var_dump($pixel->isPixelSimilar('#FF0000', 0)); var_dump($pixel->isPixelSimilar('rgb(255, 0, 0)', 0)); var_dump($pixel->isPixelSimilar('green', 0.01)); var_dump($pixel->isPixelSimilar('#0000FF', 0.02)); var_dump($pixel->isPixelSimilar('rgb(0, 0, 0)', 0.03)); $pixel = new ImagickPixel('#F02B88'); var_dump($pixel->isPixelSimilar('#F02B89', 0.9 / $sqrt3 / 255)); var_dump($pixel->isPixelSimilar('#F02B89', 1.1 / $sqrt3 / 255)); var_dump($pixel->isPixelSimilar('#F12A86', 0.9 / 255)); var_dump($pixel->isPixelSimilar('#F12A88', 1.1 / 255)); ?> ==DONE==