PHP code example of webzille / colorutility

1. Go to this page and download the library: Download webzille/colorutility library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

webzille / colorutility example snippets


use Webzille\ColorUtility\Colors\RGB;
use Webzille\ColorUtility\Colors\RYB;
use Webzille\ColorUtility\Colors\LAB;

$rgbColor = new RGB(255, 0, 0);  // Red in RGB
$rybColor = new RYB(255, 0, 0);  // Red in RYB
$labColor = new LAB(53.23288178584245, 80.10930952982204, 67.22006831026425);  // Red in LAB

use Webzille\ColorUtility\SetColor;

$HexString = "#ffcc00";
$HexObject = SetColor::fromString($HexString);

$RGBString = "rgb(255, 0, 0);";
$RGBObject = SetColor::fromString($RGBString);

$RGBAString = "rgba(153, 255, 46, 0.4)";
$RGBAObject = SetColor::fromString($RGBAString);

$HSLAString = "hsla(0, 41%, 51%, 0.3)";
$HSLAObject = SetColor::fromString($HSLAString);

$HSLString = "hsl(146, 41%, 51%)";
$HSLObject = SetColor::fromString($HSLString);

$NamedString = "steelblue";
$NamedObject = SetColor::fromString($NamedString);

$rgbColor = new RGB(255, 128, 69);
echo $rgbColor->setSpace(LAB::class)->findColorByAngle(180)->viewColor();  // rgb(0, 189, 255)

$rgbColor = new RGB(255, 128, 69);
echo $rgbColor->setSpace(HSV::class)->findColorByAngle(180)->viewColor();  // rgb(69, 196, 255)

// The default color space / wheel
$rgbColor = new RGB(255, 128, 69);
echo $rgbColor->setSpace(RYB::class)->findColorByAngle(180)->viewColor();  // rgb(69, 255, 122)

$rgbColor = new RGB(255, 128, 69);
echo $rgbColor->findColorByAngle(180)->viewColor();  // rgb(69, 255, 122)

$rgbColor = new RGB(255, 128, 69);
echo $rgbColor->as(LAB::class)->findColorByAngle(180)->viewColor();

$rgbColor = new RGB(255, 128, 69);
echo $rgbColor->asLAB()->findColorByAngle(180)->viewColor();

$labColor = new LAB(53, 80, 67);
echo $labColor->setSpace(RYB::class)->findColorByAngle(180);  // LAB(53, -80, -67) // Light-blue

$labColor = new LAB(53, 80, 67);
echo $labColor->asRYB()->findColorByAngle(180)->backTo($labColor);  // LAB(87, -86, 83) // Green
// or

echo $labColor->asRYB()->findColorByAngle(180)->asLAB();  // LAB(87, -86, 83) // Green

$value = ".cssRuleset {
    font-size: 1.3em;
    color: #ffcc00;
    border: 1px solid rgb(124, 20, 0);
    background-color: transparent;
}";

$transparency = true;
preg_match_all(Colors::getMatchingPattern($transparency), $value, $matches);

print_r($matches[1]);

// Array
// (
//     [0] => #ffcc00
//     [1] => rgb(124, 20, 0)
//     [2] => transparent
// )

echo $NamedString->viewColor("The optional label");

echo $RGBAObject->viewColor();

$complementaryRGB = $rgbColor->complementary(); // Green

$weight = 0.5;
$blendedRYB = $rybColor->blendColors(new RYB(0, 0, 255), $weight); // Blend red and blue in RYB

$darkerColor = $rgbColor->adjustShade(50); // 50% of the current shade
$brighterColor = $rgbColor->adjustShade(150); // 50% brighter than it's current shade
$sameShade - $rgbColor->adjustShade(100); // No change; 100% of it's current shade

$isLight = $rgbColor->isLight(); // Returns true if the color is considered light
$angleBetween = $rybColor->calculateAngle(new RYB(0, 255, 0)); // Calculate angle between red and yellow in RYB

$triadicScheme = $rgbColor->triadic(); // Returns an array of RGB objects in a triadic scheme

[$color, $seed] = SetColor::random();
echo $color->viewColor("Seed: $seed");

$colorSeed = 8167421221;

[$color, $seed] = SetColor::random($colorSeed);
echo $color->viewColor("Seed: $seed");

$hexColor = $rgbColor->asHEX();
echo $hexColor; // Outputs HEX code for red

// Default websafe color to view is RGB
$hslColor = new HSL(195.51382638999, 100, 50);
echo $hslColor->asLAB()->findColorByAngle(180)->viewColor();  // rgb(228, 163, 97)

// Making sure we view the color in HSL format after using the color in LAB color space
$hslColor = new HSL(195.51382638999, 100, 50);
echo $hslColor->asLAB()->findColorByAngle(180)->backTo($hslColor)->viewColor();  // hsl(30.533318540464, 70.332302060189%, 63.65760628305%)

// Making sure we view the color in HSL format after using the color in LAB color space
$hslColor = new HSL(195.51382638999, 100, 50);
echo $hslColor->asLAB()->findColorByAngle(180)->asHSL()->viewColor();  // hsl(30.533318540464, 70.332302060189%, 63.65760628305%)

// Assuming $rgbColor, $rybColor, and $labColor are already defined as instances of their respective color classes

// RGB to other formats
$hexFromRGB = $rgbColor->asHEX();
$hslFromRGB = $rgbColor->asHSL();
$labFromRGB = $rgbColor->asLAB();

// RYB to RGB (and then to other formats)
$rgbFromRYB = $rybColor->asRGB();
$hexFromRYB = $rybColor->asHEX();
$hslFromRYB = $rybColor->asHSL();

// LAB to RGB and to HEX
$rgbFromLAB = $labColor->asRGB();
$hexFromLAB = $labColor->asHEX();