PHP code example of savvywombat / color

1. Go to this page and download the library: Download savvywombat/color 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/ */

    

savvywombat / color example snippets


$rgb = new \SavvyWombat\Color\RGB(50, 60, 70);
echo (string) $rgb; // rgb(50,60,70)

$hsl = $rgb->toHsl();
echo (string) $hsl; // hsl(210,16.7%,23.5%);

$redderHsl = $hsl->red('+50');
echo (string) $redderHsl; // hsl(345,25%,31.4%);

$lighterRgb = $rgb->lighten('+10%');
echo (string) $lighterRgb; // rgb(55,66,77);

$transparentRgb = $rgb->alpha('0.5');
echo (string) $transparentRgb; // rgb(50,60,70,0.5)

echo (string) $rgb->toHex(); // #323C46

Color::fromString('#123'); // Hex: #112233
Color::fromString('#1234'); // Hex (with alpha): #11223344
Color::fromString('#123456'); // Hex: #123456
Color::fromString('#12345678'); // Hex: #12345678

Color::fromString('rgb(10, 20, 30)'); // RGB
Color::fromString('rgba(10, 20, 30, 0.4)'); // RGB

Color::fromString('hsl(10, 20%, 30%)'); // HSL
Color::fromString('hsl(10, 20%, 30%, 0.4)'); // HSL

echo (string) Color::fromString('#123')->toHsl(); // hsl(210,50%,8.6%)
echo (string) Color::fromString('#123')->toRGB(); // rgb(17,34,51)

echo (string) Color::fromString('rgb(25, 75, 125)')->toHex(); // #194B7D
echo (string) Color::fromString('rgb(25, 75, 125)')->toHsl(); // hsl(210,66.7%,39.4%)

echo (string) Color::fromString('hsl(135, 50%, 75%)')->toHex(); // #9FDFAF
echo (string) Color::fromString('hsl(135, 50%, 75%)')->toRGB(); // rgb(159,223,175)

$newHsl = $hsl->red(50); // set to specific value
$newHsl = $hsl->red('+50'); // add 50 to the current value
$newHsl = $hsl->red('-50'); // remove 50 from the current value
$newHsl = $hsl->red('+50%'); // increase the current value by 50%
$newHsl = $hsl->red('-50%'); // decrease the current value by 50%
$newHsl = $hsl->red('+1/2'); // set to a value halfway between the current value and 255 (maximum red value)
$newHsl = $hsl->red('-1/2'); // set to a value halfway between the current value and 0 (minimum red value)

    public static function fromString(string $colorSpec): ColorInterface;
    public function __toString(): string;

    public static function fromRGB(Rgb $rgb): ColorInterface;
    public function toRGB(): Rgb;

Color::registerColor('Gray', Gray::class);
echo (string) Color::fromString('#163248')->toGray(); // #323232

echo (string) Color::fromString('#204060')->toGray(); // gray(25%)
echo (string) Color::fromString('gray(50%, 0.25)')->toRGB(); // #80808040

/**
 * color spec pattern: 'gray\((\d{1,3}(\.\d{1})?)%\)'
 * color spec pattern: 'gray\((\d{1,3}(\.\d{1})?)%,([0-1](\.\d{1,2})?)\)'
 *
 * If the color spec matches either of the patterns, extractChannels will return the parameters from the string using regex.
 */
public static function fromString(string $colorSpec): ColorInterface
{
    $channels = Color::extractChannels($colorSpec, self::class);

    if (!isset($channels[3])) {
        $channels[3] = 1.0;
    }

    return new Gray((float) $channels[1], $channels[3]);
}

Color::registerColor('Gray', Gray::class);
Color::registerColorSpec('gray\((\d{1,3})\)', Gray::class);

public function __toString(): string
{
    $value = round($this->value, 1);
    $alpha = round($this->alpha, 2);
    
    if ($alpha === 1.0) {
        return "gray({$value},{$alpha})";
    }

    return "gray({$value})";
}

public static function fromRGB(Rgb $rgb): ColorInterface
{
    $average = ($rgb->red + $rgb->green + $rgb->blue) / 3;
    $gray = $average * 100 / 255;

    return new Gray($gray, $rgb->alpha);
}

public function toRGB(): Rgb
{
    return new Rgb($this->red, $this->green, $this->blue, $this->alpha);
}

use \SavvyWombat\Color\Color;

class Gray extends Color
{
    // gray value in range 0..100
    protected $value;

    public function __construct(float $value, float $alpha = 1.0)
    {
        $this->value = $value;
        $this->alpha = $alpha;

        // calculate RGB equivalent values for easy conversion
        $this->red = $value * 2.55; // convert to 0..255 for RGB
        $this->green = $value * 2.55;
        $this->blue = $value * 2.55;
    }
}

public function gray($gray): self
{
    $gray = $this->adjustValue($this->gray, $gray, 100);
    
    if ($gray < 0) {
        $gray = 0;
    }

    if ($gray > 100) {
        $gray = 100;
    }

    return new self($gray, $this->alpha);
}

$hsl = Color::fromString('deepskyblue')->toHsl();