PHP code example of bugo / iris

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

    

bugo / iris example snippets


// before
$color = new RgbColor(r: 255.0, g: 128.0, b: 0.0, a: 1.0);

// after
$color = new RgbColor(r: 1.0, g: 0.502, b: 0.0, a: 1.0);
// or
$color = (new LiteralParser())->toRgb('#ff8000');

use Bugo\Iris\Spaces\HslColor;
use Bugo\Iris\Spaces\OklchColor;
use Bugo\Iris\Spaces\RgbColor;

$red   = new RgbColor(r: 1.0, g: 0.0, b: 0.0, a: 1.0);
$green = new HslColor(h: 120.0, s: 100.0, l: 50.0, a: 1.0);
$blue  = new OklchColor(l: 45.2, c: 0.313, h: 264.1, a: 1.0);

use Bugo\Iris\Converters\SpaceConverter;
use Bugo\Iris\Spaces\RgbColor;

$converter = new SpaceConverter();
$rgb       = new RgbColor(r: 1.0, g: 0.5, b: 0.0, a: 1.0);

// RGB -> OKLCh object
$oklch = $converter->rgbToOklch($rgb);
echo $oklch->l; // ~73 on the 0-100 lightness scale
echo $oklch->h; // ~53

// RGB -> XYZ D65 object
$xyz = $converter->rgbToXyzD65($rgb);
echo $xyz->x;

// HSL channels -> RGB channels (returns [r, g, b] as normalized floats)
[$r, $g, $b] = $converter->hslToRgb(30.0, 1.0, 0.5);

// sRGB channels -> XYZ D65 object
$xyzFromChannels = $converter->srgbToXyzD65(1.0, 0.5, 0.0);

use Bugo\Iris\SpaceRouter;
use Bugo\Iris\Exceptions\UnsupportedColorSpace;

$router = new SpaceRouter();

try {
    $rgba = $router->convertToRgba('display-p3', 1.0, 0.5, 0.0, 1.0);
    echo $rgba->r; // normalized 0-1
} catch (UnsupportedColorSpace $e) {
    // unknown color space
}

$xyz = $router->convertToXyzD65('rec2020', 0.4, 0.3, 0.2);
echo $xyz->y;

use Bugo\Iris\Manipulators\LegacyManipulator;
use Bugo\Iris\Spaces\RgbColor;

$manipulator = new LegacyManipulator();
$color       = new RgbColor(r: 0.784, g: 0.392, b: 0.196, a: 1.0);

$gray      = $manipulator->grayscale($color);
$mixed     = $manipulator->mix($color, new RgbColor(0.0, 0.588, 1.0, 1.0), 0.5);
$darker    = $manipulator->darken($color, 10.0);
$saturated = $manipulator->saturate($color, 20.0);
$rotated   = $manipulator->spin($color, 30.0);

use Bugo\Iris\Operations\GamutMapper;
use Bugo\Iris\Spaces\OklchColor;

$mapper = new GamutMapper();
$oklch  = new OklchColor(l: 70.0, c: 0.35, h: 30.0, a: 1.0);

$clipped = $mapper->clip($oklch);
$mapped  = $mapper->localMinde($oklch);

use Bugo\Iris\Operations\ColorMixResolver;
use Bugo\Iris\Spaces\OklchColor;
use Bugo\Iris\Spaces\RgbColor;

$resolver = new ColorMixResolver();

$mixSrgb = $resolver->mixSrgb(
    new RgbColor(r: 1.0, g: 0.0, b: 0.0, a: 1.0),
    new RgbColor(r: 0.0, g: 0.0, b: 1.0, a: 1.0),
    0.5,
);

$mixOklch = $resolver->mixOklch(
    new OklchColor(l: 70.0, c: 0.2, h: 30.0, a: 1.0),
    new OklchColor(l: 50.0, c: 0.1, h: 200.0, a: 1.0),
    0.5,
    hueMethod: 'shorter',
);

use Bugo\Iris\Encoders\HexEncoder;
use Bugo\Iris\Encoders\HexNormalizer;
use Bugo\Iris\Encoders\HexShortener;

$encoder    = new HexEncoder();
$shortener  = new HexShortener();
$normalizer = new HexNormalizer();

$hex   = $encoder->encodeRgb(255, 128, 0);       // '#ff8000'
$hexA  = $encoder->encodeRgba(255, 128, 0, 255); // '#ff8000ff'
$short = $shortener->shorten('#aabbcc');         // '#abc'
$norm  = $normalizer->normalize('#AABBCC');      // '#abc'

use Bugo\Iris\LiteralParser;
use Bugo\Iris\Serializers\LiteralSerializer;
use Bugo\Iris\Spaces\RgbColor;

$converter  = new LiteralParser();
$serializer = new LiteralSerializer();

$rgbFromHex  = $converter->toRgb('#ff8000');
$rgbFromName = $converter->toRgb('tomato');

echo $serializer->serialize(new RgbColor(r: 1.0, g: 0.0, b: 0.0, a: 1.0));   // 'red'
echo $serializer->serialize(new RgbColor(r: 1.0, g: 0.502, b: 0.0, a: 1.0)); // '#ff8000'

use Bugo\Iris\Serializers\Serializer;

$serializer = new Serializer();

echo $serializer->serialize('#AABBCC', false);          // '#abc'
echo $serializer->serialize('rgb(255, 128, 0)', true); // '#ff8000'
echo $serializer->serialize('rgb(255, 128, 0)', false); // 'rgb(255, 128, 0)'

use Bugo\Iris\Serializers\CssSerializer;
use Bugo\Iris\Spaces\HslColor;
use Bugo\Iris\Spaces\LabColor;
use Bugo\Iris\Spaces\LchColor;
use Bugo\Iris\Spaces\OklabColor;
use Bugo\Iris\Spaces\OklchColor;
use Bugo\Iris\Spaces\XyzColor;

$serializer = new CssSerializer();
$oklch      = new OklchColor(l: 70.0, c: 0.15, h: 55.0, a: 1.0);

echo $serializer->toCss($oklch);       // 'oklch(70% 0.15 55)'
echo $serializer->toCss($oklch, true); // still serialized as a CSS color string

$hsl   = new HslColor(h: 30.0, s: 100.0, l: 50.0, a: 0.8);
$lab   = new LabColor(l: 50.0, a: 20.0, b: -30.0, alpha: 1.0);
$lch   = new LchColor(l: 70.0, c: 30.0, h: 180.0, alpha: 1.0);
$oklab = new OklabColor(l: 50.0, a: 0.1, b: -0.05, alpha: 1.0);
$xyz   = new XyzColor(x: 0.9505, y: 1.0, z: 1.0890);

echo $serializer->toCss($hsl);   // 'hsl(30 100% 50% / 0.80)'
echo $serializer->toCss($lab);   // 'lab(50% 20 -30)'
echo $serializer->toCss($lch);   // 'lch(70% 30 180)'
echo $serializer->toCss($oklab); // 'oklab(50% 0.1 -0.05)'
echo $serializer->toCss($xyz);   // 'color(xyz-d65 0.9505 1 1.089)'

use Bugo\Iris\Converters\ModelConverter;
use Bugo\Iris\Spaces\RgbColor;

$converter = new ModelConverter();
$rgb       = new RgbColor(r: 1.0, g: 0.5, b: 0.0, a: 1.0);
$hsl       = $converter->rgbToHslColor($rgb);
$rgbBack   = $converter->hslToRgbColor($hsl);

use Bugo\Iris\Manipulators\PerceptualManipulator;
use Bugo\Iris\Spaces\LabColor;
use Bugo\Iris\Spaces\OklchColor;

$manipulator = new PerceptualManipulator();

$adjusted = $manipulator->adjustOklch(
    new OklchColor(l: 70.0, c: 0.15, h: 55.0, a: 1.0),
    ['lightness' => 10.0, 'chroma' => -0.05, 'hue' => 20.0],
);

$labChanged = $manipulator->changeLab(
    new LabColor(l: 50.0, a: 20.0, b: -30.0, alpha: 1.0),
    ['lightness' => 70.0, 'alpha' => 0.5],
);

use Bugo\Iris\Manipulators\SrgbManipulator;

$manipulator = new SrgbManipulator();

$adjusted = $manipulator->adjust(
    red: 1.0,
    green: 0.5,
    blue: 0.0,
    values: ['red' => -0.1, 'green' => 0.1, 'blue' => 0.05],
);

use Bugo\Iris\Converters\SpaceConverter;
use Bugo\Iris\Spaces\RgbColor;
use Bugo\Iris\Spaces\XyzColor;

$converter = new SpaceConverter();
$rgb       = new RgbColor(r: 1.0, g: 0.5, b: 0.0, a: 1.0);

[$p3R, $p3G, $p3B] = $converter->rgbToP3Channels($rgb);

$a98      = $converter->rgbToA98Channels($rgb);
$prophoto = $converter->rgbToProphotoChannels($rgb);
$rec2020  = $converter->rgbToRec2020Channels($rgb);

$xyz       = new XyzColor(x: 0.5, y: 0.4, z: 0.2);
$p3FromXyz = $converter->xyzD65ToP3Channels($xyz);

use Bugo\Iris\Operations\PolarMath;

$math = new PolarMath();

[$a, $b] = $math->toCartesian(chroma: 0.2, hue: 55.0);
$radians = $math->toRadians(180.0); // pi

use Bugo\Iris\NamedColors;

$tomatoRgb = NamedColors::NAMED_RGB['tomato']; // [255.0, 99.0, 71.0]
$redRgb    = NamedColors::NAMED_RGB['red'];    // [255.0, 0.0, 0.0]

$hex = NamedColors::toHex('tomato');      // '#ff6347'
$hex = NamedColors::toHex('transparent'); // '#00000000'

NamedColors::isNamedColor('tomato'); // true
$names = NamedColors::getNames();

use Bugo\Iris\Contracts\ColorValueInterface;
use Bugo\Iris\Spaces\OklchColor;

function describeColor(ColorValueInterface $color): string
{
    return sprintf(
        'Space: %s, channels: [%s], alpha: %s',
        $color->getSpace(),
        implode(', ', $color->getChannels()),
        $color->getAlpha(),
    );
}

echo describeColor(new OklchColor(l: 70.0, c: 0.15, h: 55.0, a: 1.0));

use Bugo\Iris\Exceptions\IrisException;
use Bugo\Iris\Exceptions\InvalidColorChannel;
use Bugo\Iris\Exceptions\InvalidColorFormat;
use Bugo\Iris\Exceptions\UnsupportedColorSpace;