PHP code example of dustinwilson / pigmentum

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

    

dustinwilson / pigmentum example snippets


class dW\Pigmentum\Color {
    // Common illuminants used in color spaces
    const ILLUMINANT_D65 = [ 0.95047, 1, 1.08883 ];
    const ILLUMINANT_D50 = [ 0.96422, 1, 0.82521 ];

    const REFERENCE_WHITE = self::ILLUMINANT_D50;

    // Math constants
    const KAPPA = 903.296296296296296;
    const EPSILON = 0.008856451679036;

    // RGB color profiles
    const PROFILE_SRGB = 'dW\Pigmentum\Color\Profile\RGB\sRGB';
    const PROFILE_SIMPLE_SRGB = 'dW\Pigmentum\Color\Profile\RGB\Simple_sRGB';
    const PROFILE_ADOBERGB1998 = 'dW\Pigmentum\Color\Profile\RGB\AdobeRGB1998';
    const PROFILE_PROPHOTORGB = 'dW\Pigmentum\Color\Profile\RGB\ProPhoto';
    const PROFILE_DISPLAYP3 = 'dW\Pigmentum\Color\Profile\RGB\DisplayP3';

    public ?string $name = null;
    public static string $workingSpaceRGB = self::PROFILE_SRGB;


    public static function withLab(float $L, float $a, float $b, ?string $name = null): dW\Pigmentum\Color;
    public static function withLCHab(float $L, float $C, float $H, ?string $name = null): dW\Pigmentum\Color;
    public static function withRGB(float $R, float $G, float $B, ?string $name = null, ?string $profile = null): dW\Pigmentum\Color;
    public static function withRGBHex(string $hex, ?string $name = null, ?string $profile = null): dW\Pigmentum\Color;
    public static function withHSB(float $H, float $S, float $B, ?string $name = null, ?string $profile = null): dW\Pigmentum\Color;
    public static function withXYZ(float $X, float $Y, float $Z, string $name = null): dW\Pigmentum\Color;


    public function toLab(): dW\Pigmentum\ColorSpace\Lab;
    public function toRGB(?string $profile = null): dW\Pigmentum\ColorSpace\RGB;
    public function toXYZ(): dW\Pigmentum\ColorSpace\XYZ;


    public static function average(dW\Pigmentum\Color ...$colors): dW\Pigmentum\Color;
    public static function averageWithLab(dW\Pigmentum\Color ...$colors): dW\Pigmentum\Color;
    public static function averageWithLCHab(dW\Pigmentum\Color ...$colors): dW\Pigmentum\Color;
    public static function averageWithRGB(dW\Pigmentum\Color ...$colors): dW\Pigmentum\Color;
    public static function averageWithHSB(dW\Pigmentum\Color ...$colors): dW\Pigmentum\Color;

    public function mix(dW\Pigmentum\Color $color, float $percentage = 0.5): dW\Pigmentum\Color;
    public function mixWithLab(dW\Pigmentum\Color $color, float $percentage = 0.5): dW\Pigmentum\Color;
    public function mixWithLCHab(dW\Pigmentum\Color $color, float $percentage = 0.5): dW\Pigmentum\Color;
    public function mixWithRGB(dW\Pigmentum\Color $color, float $percentage = 0.5): dW\Pigmentum\Color;
    public function mixWithHSB(dW\Pigmentum\Color $color, float $percentage = 0.5): dW\Pigmentum\Color;


    public function apcaContrast(dW\Pigmentum\Color $backgroundColor): float;
    public function deltaE(dW\Pigmentum\Color $color): float;
    public function distance(dW\Pigmentum\Color $color): float;
    public function euclideanDistance(dW\Pigmentum\Color $color): float;
    public function wcag2Contrast(dW\Pigmentum\Color $color): float;
}

public static function withLab(
    float $L,
    float $a,
    float $b,
    ?string $name = null
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

Color::withLab(57, 35, 38);

public static function withLCHab(
    float $L,
    float $C,
    float $H,
    ?string $name = null
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

Color::withLCHab(57, 51, 47);

public static function withRGB(
    float $R,
    float $G,
    float $B,
    ?string $name = null,
    ?string $profile = null
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

$sRGBColor = Color::withRGB(202, 110, 72);
$adobeRGBColor = Color::withRGB(202, 110, 72, null, Color::PROFILE_ADOBERGB1998);
echo $sRGBColor->XYZ . "\n";
echo $adobeRGBColor->XYZ;

public static function withRGBHex(
    string $hex,
    ?string $name = null,
    ?string $profile = null
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

$sRGBColor = Color::withRGBHex('#ca6e48');
$adobeRGBColor = Color::withRGBHex('#ca6e48', null, Color::PROFILE_ADOBERGB1998);
echo $sRGBColor->XYZ . "\n";
echo $adobeRGBColor->XYZ;

public static function withHSB(
    float $H,
    float $S,
    float $B,
    ?string $name = null,
    ?string $profile = null
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

Color::withHSB(18, 64, 79);

public static function withXYZ(
    float $X,
    float $Y,
    float $Z,
    ?string $name = null
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

Color::withXYZ(0.3267, 0.2471, 0.0696);

public function toLab(): dW\Pigmentum\ColorSpace\Lab;

namespace dW\Pigmentum\Color;

$color = Color::withHSB(18, 64, 79);
echo $color->toLab() . "\n";
echo $color->Lab;

public function toRGB(
    ?string $profile = null
): dW\Pigmentum\ColorSpace\RGB;

namespace dW\Pigmentum\Color;

$color = Color::withXYZ(0.3267, 0.2471, 0.0696);
echo $color->toRGB() . "\n";
echo $color->RGB;

public function toXYZ(): dW\Pigmentum\ColorSpace\XYZ;

namespace dW\Pigmentum\Color;

$color = Color::withRGBHex('#ca6e48');
echo $color->toXYZ() . "\n";
echo $color->XYZ;

public static function average(
    dW\Pigmentum\Color ...$colors
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

$color = Color::average(Color::withRGBHex('#ca6e48'), Color::withXYZ(0.0864, 0.0868, 0.1409), Color::withLab(100, 0, 0));
echo $color->RGB->Hex;

public static function averageWithLab(
    dW\Pigmentum\Color ...$colors
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

$color = Color::averageWithLab(Color::withRGBHex('#ca6e48'), Color::withXYZ(0.0864, 0.0868, 0.1409), Color::withLab(100, 0, 0));
echo $color->RGB->Hex;

public static function averageWithLCHab(
    dW\Pigmentum\Color ...$colors
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

$color = Color::averageWithLCHab(Color::withRGBHex('#ca6e48'), Color::withXYZ(0.0864, 0.0868, 0.1409), Color::withLab(100, 0, 0));
echo $color->RGB->Hex;

public static function averageWithRGB(
    dW\Pigmentum\Color ...$colors
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

$color = Color::averageWithRGB(Color::withRGBHex('#ca6e48'), Color::withXYZ(0.0864, 0.0868, 0.1409), Color::withLab(100, 0, 0));
echo $color->RGB->Hex;

public static function averageWithHSB(
    dW\Pigmentum\Color ...$colors
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

$color = Color::averageWithHSB(Color::withRGBHex('#ca6e48'), Color::withXYZ(0.0864, 0.0868, 0.1409), Color::withLab(100, 0, 0));
echo $color->RGB->Hex;

public function mix(
    dW\Pigmentum\Color $color,
    float $percentage = 0.5
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

$color = Color::withRGBHex('#ca6e48')->mix(Color::withXYZ(0.0864, 0.0868, 0.1409), 0.625);
echo $color->RGB->Hex;

public function mixWithLab(
    dW\Pigmentum\Color $color,
    float $percentage = 0.5
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

$color = Color::withRGBHex('#ca6e48')->mixWithLab(Color::withXYZ(0.0864, 0.0868, 0.1409), 0.625);
echo $color->RGB->Hex;

public function mixWithLab(
    dW\Pigmentum\Color $color,
    float $percentage = 0.5
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

$color = Color::withRGBHex('#ca6e48')->mixWithLCHab(Color::withXYZ(0.0864, 0.0868, 0.1409), 0.625);
echo $color->RGB->Hex;

public function mixWithRGB(
    dW\Pigmentum\Color $color,
    float $percentage = 0.5
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

$color = Color::withRGBHex('#ca6e48')->mixWithRGB(Color::withXYZ(0.0864, 0.0868, 0.1409), 0.625);
echo $color->RGB->Hex;

public function mixWithHSB(
    dW\Pigmentum\Color $color,
    float $percentage = 0.5
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

$color = Color::withRGBHex('#ca6e48')->mixWithHSB(Color::withXYZ(0.0864, 0.0868, 0.1409), 0.625);
echo $color->RGB->Hex;

public function apcaContrast(
    dW\Pigmentum\Color $backgroundColor
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

echo Color::withRGBHex('#ca6e48')->apcaContrast(Color::withXYZ(0.0864, 0.0868, 0.1409));

public function deltaE(
    dW\Pigmentum\Color $color
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

echo Color::withRGBHex('#ca6e48')->deltaE(Color::withXYZ(0.0864, 0.0868, 0.1409));

public function distance(
    dW\Pigmentum\Color $color
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

echo Color::withRGBHex('#ca6e48')->distance(Color::withXYZ(0.0864, 0.0868, 0.1409));

public function euclideanDistance(
    dW\Pigmentum\Color $color
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

echo Color::withRGBHex('#ca6e48')->euclideanDistance(Color::withXYZ(0.0864, 0.0868, 0.1409));

public function wcag2Contrast(
    dW\Pigmentum\Color $color
): dW\Pigmentum\Color;

namespace dW\Pigmentum\Color;

echo Color::withRGBHex('#ca6e48')->wcag2Contrast(Color::withXYZ(0.0864, 0.0868, 0.1409));

class dW\Pigmentum\ColorSpace\Lab implements \Stringable {
    public readonly float $L;
    public readonly float $a;
    public readonly float $b;


    public function toLCHab(): dW\Pigmentum\ColorSpace\Lab\LCHab;
}

public function toLCHab(): dW\Pigmentum\ColorSpace\Lab\LCHab;

namespace dW\Pigmentum\Color;

$color = Color::withRGBHex('#ca6e48');
echo $color->toLab()->toLCHab() . "\n";
echo $color->Lab->LCHab;

class dW\Pigmentum\ColorSpace\RGB implements \Stringable {
    public readonly float $R;
    public readonly float $G;
    public readonly float $B;
    public readonly float $unclampedR;
    public readonly float $unclampedG;
    public readonly float $unclampedB;
    public readonly string $profile;
    public readonly bool $outOfGamut;


    public function changeProfile(?string $profile = null): dW\Pigmentum\ColorSpace\RGB;
    public function convertToWorkingSpace(?string $profile = null): dW\Pigmentum\ColorSpace\RGB;

    public function toHex(): string;
    public function toHSB(): dW\Pigmentum\ColorSpace\RGB\HSB;
}

public function changeProfile(
    ?string $profile = null
): dW\Pigmentum\ColorSpace\RGB;

namespace dW\Pigmentum\Color;

// sRGB is the default working space
$color = Color::withRGBHex('#ca6e48');
echo $color->RGB . "\n";
echo $color->RGB->changeProfile(Color::PROFILE_DISPLAYP3);

public function toRGBHex(): string;

namespace dW\Pigmentum\Color;

$color = Color::withRGBHex('#ca6e48');
echo $color->toRGB()->toHex() . "\n";
echo $color->RGB->Hex;

public function toHSB(): dW\Pigmentum\ColorSpace\RGB\HSB;

namespace dW\Pigmentum\Color;

$color = Color::withRGBHex('#ca6e48');
echo $color->toRGB()->toHSB() . "\n";
echo $color->RGB->HSB;

class dW\Pigmentum\ColorSpace\XYZ implements \Stringable {
    public function toLMS(): dW\Pigmentum\ColorSpace\XYZ\LMS;
    public function chromaticAdaptation(array $new, array $old): dW\Pigmentum\ColorSpace\XYZ;
}

public function toLMS(): dW\Pigmentum\ColorSpace\XYZ\LMS;

namespace dW\Pigmentum\Color;

$color = Color::withRGBHex('#ca6e48');
echo $color->toXYZ()->toLMS() . "\n";
echo $color->XYZ->LMS;

public function chromaticAdaptation(array $new, array $old): dW\Pigmentum\ColorSpace\XYZ;

namespace dW\Pigmentum\Color;

// XYZ D50
$color = Color::withRGBHex('#ca6e48')->XYZ;
echo $color . "\n";
// XYZ D65
echo $color->chromaticAdaptation(Color::ILLUMINANT_D65, Color::ILLUMINANT_D50);

class dW\Pigmentum\ColorSpace\Lab\LCHab implements \Stringable {
    public readonly float $L;
    public readonly float $C;
    public readonly float $H;
}

class dW\Pigmentum\ColorSpace\RGB\HSB implements \Stringable {
    public readonly float $H;
    public readonly float $S;
    public readonly float $B;
}

class dW\Pigmentum\ColorSpace\XYZ\LMS implements \Stringable {
    public readonly float $rho;
    public readonly float $gamma;
    public readonly float $beta;
}

abstract class dW\Pigmentum\Profile\RGB {
    const illuminant = dW\Pigmentum\Color::ILLUMINANT_D65;
    const chromaticity = [];
    const gamma = 2.2;
    const name = '';

    protected static array $xyzMatrix;
    protected static array $xyzMatrixInverse;


    public static function companding(float $channel): float;
    public static function inverseCompanding(float $channel): float;
    public static function getXYZMatrix(): MathPHP\LinearAlgebra\Matrix\Matrix;
}

class dW\Pigmentum\Profile\AdobeRGB1998 extends dW\Pigmentum\Profile\RGB {
    const name = 'Adobe RGB (1998)';

    const chromaticity = [
        [ 0.6400, 0.3300 ],
        [ 0.2100, 0.7100 ],
        [ 0.1500, 0.0600 ]
    ];

    protected static array $xyzMatrix = [
        [ 0.5767308871981477, 0.18555395071121408, 0.18818516209063843 ],
        [ 0.29737686371154487, 0.6273490714522, 0.07527406483625537 ],
        [ 0.027034260337413143, 0.0706872193185578, 0.9911085203440292 ]
    ];

    protected static array $xyzMatrixInverse = [
        [ 2.04136897926008, -0.5649463871751956, -0.34469438437784833 ],
        [ -0.9692660305051868, 1.8760108454466942, 0.041556017530349834 ],
        [ 0.013447387216170259, -0.11838974235412557, 1.0154095719504166 ]
    ];
}

class dW\Pigmentum\Profile\AdobeRGB1998 extends dW\Pigmentum\Profile\RGB {
    const name = 'Display P3';

    const chromaticity = [
        [ 0.680, 0.320 ],
        [ 0.265, 0.690 ],
        [ 0.150, 0.060 ]
    ];

    protected static array $xyzMatrix = [
        [ 0.48663265, 0.2656631625, 0.1981741875 ],
        [ 0.2290036, 0.6917267249999999, 0.079269675 ],
        [ -3.972579210032023E-17, 0.04511261250000004, 1.0437173875 ]
    ];

    protected static array $xyzMatrixInverse = [
        [ 2.4931807553289667, -0.9312655254971397, -0.40265972375888165 ],
        [ -0.8295031158210787, 1.7626941211197922, 0.02362508874173958 ],
        [ 0.03585362578007169, -0.07618895478265217, 0.9570926215180212 ]
    ];
}

class dW\Pigmentum\Profile\ProPhoto extends dW\Pigmentum\Profile\RGB {
    const name = 'ProPhoto RGB';

    const illuminant = Color::ILLUMINANT_D50;

    const chromaticity = [
        [ 0.7347, 0.2653 ],
        [ 0.1596, 0.8404 ],
        [ 0.0366, 0.0001 ]
    ];

    const gamma = 1.8;

    protected static array $xyzMatrix = [
        [ 0.7976749444306044, 0.13519170147409815, 0.031353354095297416 ],
        [ 0.2880402378623102, 0.7118740972357901, 8.566490189971971E-5 ],
        [ 0, 0, 0.82521 ]
    ];

    protected static array $xyzMatrixInverse = [
        [ 1.3459433009386654, -0.25560750931676696, -0.05111176587088495 ],
        [ -0.544598869458717, 1.508167317720767, 0.020535141586646915 ],
        [ 0, 0, 1.2118127506937628 ]
    ];
}

class dW\Pigmentum\Profile\Simple_sRGB extends dW\Pigmentum\Profile\RGB {
    const name = 'Simple sRGB';

    const chromaticity = [
        [ 0.6400, 0.3300 ],
        [ 0.3000, 0.6000 ],
        [ 0.1500, 0.0600 ]
    ];

    protected static array $xyzMatrix = [
        [ 0.4124564390896922, 0.357576077643909, 0.18043748326639894 ],
        [ 0.21267285140562253, 0.715152155287818, 0.07217499330655958 ],
        [ 0.0193338955823293, 0.11919202588130297, 0.9503040785363679 ]
    ];

    protected static array $xyzMatrixInverse = [
        [ 3.2404541621141045, -1.5371385127977166, -0.498531409556016 ],
        [ -0.9692660305051868, 1.8760108454466942, 0.041556017530349834 ],
        [ 0.055643430959114726, -0.2040259135167538, 1.0572251882231791 ]
    ];
}

class dW\Pigmentum\Profile\Simple_sRGB extends dW\Pigmentum\Profile\RGB {
    const name = 'sRGB IEC61966-2.1';

    const chromaticity = [
        [ 0.6400, 0.3300 ],
        [ 0.3000, 0.6000 ],
        [ 0.1500, 0.0600 ]
    ];

    protected static array $xyzMatrix = [
        [ 0.4124564390896922, 0.357576077643909, 0.18043748326639894 ],
        [ 0.21267285140562253, 0.715152155287818, 0.07217499330655958 ],
        [ 0.0193338955823293, 0.11919202588130297, 0.9503040785363679 ]
    ];

    protected static array $xyzMatrixInverse = [
        [ 3.2404541621141045, -1.5371385127977166, -0.498531409556016 ],
        [ -0.9692660305051868, 1.8760108454466942, 0.041556017530349834 ],
        [ 0.055643430959114726, -0.2040259135167538, 1.0572251882231791 ]
    ];
}

-21.758825698145