Exemplo n.º 1
0
 public function testToRgbArray()
 {
     $res = $this->obj->toRgbArray();
     $this->assertEquals(array('red' => 0.25, 'green' => 0.5, 'blue' => 0.75, 'alpha' => 0.85), $res, '', 0.01);
     $col = new \Com\Tecnick\Color\Model\Hsl(array('hue' => 0.583, 'saturation' => 0.5, 'lightness' => 0.4, 'alpha' => 1));
     $res = $col->toRgbArray();
     $this->assertEquals(array('red' => 0.199, 'green' => 0.4, 'blue' => 0.6, 'alpha' => 1), $res, '', 0.01);
     $col = new \Com\Tecnick\Color\Model\Hsl(array('hue' => 0.583, 'saturation' => 0, 'lightness' => 0.4, 'alpha' => 1));
     $res = $col->toRgbArray();
     $this->assertEquals(array('red' => 0.4, 'green' => 0.4, 'blue' => 0.4, 'alpha' => 1), $res, '', 0.01);
     $col = new \Com\Tecnick\Color\Model\Hsl(array('hue' => 0.01, 'saturation' => 1, 'lightness' => 0.4, 'alpha' => 1));
     $res = $col->toRgbArray();
     $this->assertEquals(array('red' => 0.8, 'green' => 0.048, 'blue' => 0, 'alpha' => 1), $res, '', 0.01);
     $col = new \Com\Tecnick\Color\Model\Hsl(array('hue' => 1, 'saturation' => 1, 'lightness' => 0.4, 'alpha' => 1));
     $res = $col->toRgbArray();
     $this->assertEquals(array('red' => 0.8, 'green' => 0, 'blue' => 0, 'alpha' => 1), $res, '', 0.01);
 }
Exemplo n.º 2
0
 * @category    Library
 * @package     Color
 * @author      Nicola Asuni <*****@*****.**>
 * @copyright   2015-2015 Nicola Asuni - Tecnick.com LTD
 * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 * @link        https://github.com/tecnickcom/tc-lib-color
 *
 * This file is part of tc-lib-color software library.
 */
// autoloader when using Composer
require '../vendor/autoload.php';
// autoloader when using RPM or DEB package installation
//require ('/usr/share/php/Com/Tecnick/Color/autoload.php');
$colobj = new \Com\Tecnick\Color\Web();
$colmap = $colobj->getMap();
$tablerows = '';
$invtablerows = '';
foreach ($colmap as $name => $hex) {
    $rgbcolor = $colobj->getRgbObjFromHex($hex);
    $hslcolor = new \Com\Tecnick\Color\Model\Hsl($rgbcolor->toHslArray());
    $comp = $rgbcolor->getNormalizedArray(255);
    // web colors
    $tablerows .= '<tr>' . '<td style="background-color:' . $rgbcolor->getCssColor() . ';">&nbsp;</td>' . '<td>' . $name . '</td>' . '<td>' . $rgbcolor->getRgbHexColor() . '</td>' . '<td style="text-align:right;">' . $comp['R'] . '</td>' . '<td style="text-align:right;">' . $comp['G'] . '</td>' . '<td style="text-align:right;">' . $comp['B'] . '</td>' . '<td>' . $rgbcolor->getCssColor() . '</td>' . '<td>' . $hslcolor->getCssColor() . '</td>' . '<td>' . $rgbcolor->getJsPdfColor() . '</td>' . '</tr>' . "\n";
    // normalised inverted web colors
    $invcolor = clone $rgbcolor;
    $invcolor->invertColor();
    $invcolname = $colobj->getClosestWebColor($invcolor->toRgbArray());
    $invrgbcolor = $colobj->getRgbObjFromName($invcolname);
    $invtablerows .= '<tr>' . '<td style="text-align:right;">' . $name . '</td>' . '<td style="background-color:' . $rgbcolor->getCssColor() . ';">&nbsp;</td>' . '<td style="background-color:' . $invrgbcolor->getCssColor() . ';">&nbsp;</td>' . '<td>' . $invcolname . '</td>' . '</tr>' . "\n";
}
echo "\n<!DOCTYPE html>\n<html>\n    <head>\n        <title>Usage example of tc-lib-color library</title>\n        <meta charset=\"utf-8\">\n        <style>\n            body {font-family:Arial, Helvetica, sans-serif;}\n            table {border: 1px solid black;font-family: \"Courier New\", Courier, monospace}\n            th {border: 1px solid black;padding:4px;background-color:cornsilk;}\n            td {border: 1px solid black;padding:4px;}\n        </style>\n    </head>\n\n    <body>\n        <h1>Usage example of tc-lib-color library</h1>\n        <p>This is an usage example of <a href=\"https://github.com/tecnickcom/tc-lib-color\" title=\"tc-lib-color: PHP library to manipulate various color representations\">tc-lib-color</a> library.</p>\n        <h2>Web Colors Table</h2>\n        <table>\n            <thead>\n                <tr>\n                    <th>COLOR</th>\n                    <th>NAME</th>\n                    <th>HEX</th>\n                    <th>RED</th>\n                    <th>GREEN</th>\n                    <th>BLUE</th>\n                    <th>CSS-RGBA</th>\n                    <th>CSS-HSLA</th>\n                    <th>PDF-JS</th>\n                </tr>\n            </thead>\n            <tbody>\n" . $tablerows . "\n            </tbody>\n        </table>\n        <h2>Normalized Inverted Web Colors Table</h2>\n        <table>\n            <thead>\n                <tr>\n                    <th colspan=\"2\">A</th>\n                    <th colspan=\"2\">B</th>\n                </tr>\n                <tr>\n                    <th>NAME</th>\n                    <th>COLOR</th>\n                    <th>COLOR</th>\n                    <th>NAME</th>\n                </tr>\n            </thead>\n            <tbody>\n" . $invtablerows . "\n            </tbody>\n        </table>\n    </body>\n</html>\n";