PHP code example of domm98cz / color

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

    

domm98cz / color example snippets


use Domm98CZ\Color\Fluent\HexColor;
use Domm98CZ\Color\Fluent\RgbColor;

$brand = new HexColor('#3366cc');

$brand->lighten(0.1)->toHex();                         // "#5085ee" (OKLCh lightness +0.1)
$brand->complement()->toHex();                         // "#916300" (opposite hue on the OKLCh wheel)
$brand->mix(new RgbColor(255, 204, 0), 0.25)->toHex(); // "#6686b6" (25 % gold, mixed in OKLab)
$brand->shades(5);                                     // #264ea0 #193875 #0d234e #040f29 #010209
$brand->contrastWith(new HexColor('#fff'))->value;     // 5.366401794534009

use Domm98CZ\Color\Color;
use Domm98CZ\Color\Space\ColorSpaces;
use Domm98CZ\Color\Space\Record\Oklch;

$blue = Color::fromHex('#3366cc');
$blue->in(Oklch::class)->h;                          // 262.2930486125381, null for achromatic colors
$blue->to(ColorSpaces::displayP3());                 // Display P3 coordinates 0.2499 0.3952 0.7736 (rounded)

Color::parse('color(display-p3 1 0 0)')->toHex();    // OutOfGamutException

Color::parse('rebeccapurple');                          // rgb(102, 51, 153)
Color::parse('oklch(from #3366cc 0.8 c h)');            // oklch(0.8 0.1679 262.293)
Color::parse('color-mix(in oklch, #3366cc 30%, gold)'); // oklch(0.7805 0.1779 145.4193)

$blue->toJson();                                        // {"v":1,"space":"srgb","coords":[0.2,0.4,0.8],"alpha":1}

use Domm98CZ\Color\Harmony\Harmony;
use Domm98CZ\Color\Harmony\HarmonyScheme;
use Domm98CZ\Color\Harmony\HueWheels;

Harmony::complementary($blue, HueWheels::hsl())->members[1]->color->toHex();  // "#cc9933"
Harmony::complementary($blue, HueWheels::ryb())->members[1]->color->toHex();  // "#cc8833"
Harmony::of($blue, HueWheels::oklch(), HarmonyScheme::triadic());             // #3366cc #ba363d #1b8316

use Domm98CZ\Color\Difference\DeltaE;
use Domm98CZ\Color\Gamut\GamutMappers;
use Domm98CZ\Color\Palette\Catalogs;
use Domm98CZ\Color\Palette\Scales;

Scales::tints(ColorSpaces::oklab(), 5)->generate($blue)->palette;
// #3366cc #5a86d9 #82a4e4 #abc3ee #d4e1f7

$scale = Scales::lightnessSteps(ColorSpaces::oklch(), [0.97, 0.85, 0.65, 0.45, 0.25])
    ->generate($blue, GamutMappers::cssColor4());
// #e9f6ff #acceff #558bf4 #1c4db0 #000670; $scale->mappedSteps() lists steps 0, 1 and 4

Catalogs::cssNamed()->nearest($blue, DeltaE::ciede2000())->entry->name;  // "royalblue" (ΔE00 3.34)

use Domm98CZ\Color\Contrast\Contrast;

Contrast::wcag2()->measure(Color::fromHex('#777'), Color::fromHex('#fff'))->value;  // 4.478089453577214

use Domm98CZ\Color\Contrast\ContrastAdjuster;
use Domm98CZ\Color\Operation\LightnessModel;

$adjuster = new ContrastAdjuster(Contrast::wcag2(), LightnessModel::OklchL);
$text = $adjuster->adjustForeground(Color::fromHex('#3366cc'), Color::fromHex('#fff'), 7.0);

$text->color->toHex();        // "#2253b8" (same hue, darker)
$text->contrast->value;       // 7.0059382501719
$text->reachable;             // true

(new HexColor('#3366cc'))->readableOn('#fff', 7.0)->toHex();  // "#2253b8" in a chain; readableBehind() adjusts a background

$p3Red = Color::parse('color(display-p3 1 0 0)');

GamutMappers::cssColor4()->map($p3Red, ColorSpaces::srgb())->color->toHex();  // "#ff0b0c"

$a = Color::fromHex('#3366cc');
$b = Color::fromHex('#3a6bd0');

DeltaE::cie76()->difference($a, $b)->value;      // 2.196665323520467
DeltaE::ciede2000()->difference($a, $b)->value;  // 1.8384675223924878
DeltaE::ok()->difference($a, $b)->value;         // 0.016372279663656605