1. Go to this page and download the library: Download matthieumastadenis/couleur 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/ */
matthieumastadenis / couleur example snippets
use matthieumastadenis\couleur\ColorFactory;
use matthieumastadenis\couleur\ColorSpace;
50 ], ColorSpace::Hsl);
echo $css1; // Prints 'red'
// Convert to RGB:
$rgb1 = $css1->toRgb();
// Stringify:
echo $rgb1; // Prints 'rgb(100% 0% 0%)'
echo $rgb1->stringify(); // Prints 'rgb(100% 0% 0%)'
echo $rgb1->stringify(legacy : false, alpha : true); // Prints 'rgb(100% 0% 0% / 100%)'
echo $rgb1->stringify(legacy : true); // Prints 'rgb(255,0,0)'
echo $rgb1->stringify(legacy : true, alpha : true); // Prints 'rgba(255,0,0,1)'
// Create a variant color:
$rgb2 = $rgb1->change('-35', '+20', 60);
echo $rgb2->stringify(legacy : true); // Prints 'rgb(220,20,60)';
// Convert to CSS:
$css2 = $rgb2->toCss();
echo $css2; // Prints 'crimson'
// Convert to Lch:
$lch = $css2->toLch();
echo $lch->stringify(alpha : true); // Prints 'lch(47.878646049% 79.619059282 26.464486652deg / 100%)'
// Convert to P3:
$p3 = $lch->toP3();
echo $p3; // Prints 'color(display-p3 0.791710722 0.191507424 0.257366748)'
use matthieumastadenis\couleur\colors\Rgb;
use matthieumastadenis\couleur\colors\Hsl;
nstance (with 50% opacity):
$hex = new HexRgb('FF', '00', '00', '80');
// Create a new colors\Hsl instance (with 50% opacity):
$hsl = new Hsl(0, 100, 50, 50);
// Create a new colors\Rgb instance (with 50% opacity):
$rgb = new Rgb(255, 0, 0, 127.5);
use matthieumastadenis\couleur\ColorFactory;
ity):
$rgb1 = ColorFactory::newRgb('rgba(255,0,0,.5)');
// Returns a new colors\Lab instance:
$lab1 = ColorFactory::newLab('lab(54.29%,80.80,69.89,1)');
// Using a string value formatted with modern CSS syntax works as well:
$rgb2 = ColorFactory::newRgb('rgb(100% 0% 0% / 50%)');
$lab2 = ColorFactory::newLab('lab(54.29% 80.80 69.89 / 100%)');
// Using an array as a value also works:
$rgb3 = ColorFactory::newRgb([ 255, 0, 0, 127.5 ]);
$lab3 = ColorFactory::newLab([ 54.29, 80.80, 69.89, 100 ]);
use matthieumastadenis\couleur\ColorFactory;
ut:
$rgb = ColorFactory::newRgb('red');
// Returns a new colors\Css instance from a HSL input:
$css = ColorFactory::newCss('hsl(0deg,100%,50%)');
// Returns a new colors\XyzD65 instance from a Lab input:
$xyzD65 = ColorFactory::newXyzD65('lab(54.29% 80.80 69.89 / 100%)');
use matthieumastadenis\couleur\ColorFactory;
$rgb = ColorFactory::newRgb('not valid');
}
catch (\Exception $e) {
die($e); // Unknown color space
}
use matthieumastadenis\couleur\ColorFactory;
$rgb = ColorFactory::newRgb('rgb(255,0)');
}
catch (\Exception $e) {
die($e); // Color value "blue" is missing
}
use matthieumastadenis\couleur\ColorFactory;
use matthieumastadenis\couleur\ColorSpace;
ColorFactory::newRgb([ 0, 100, 50 ]);
\var_dump($rgb1->coordinates()); // [ 0, 100, 50, 255 ]
// With the $from parameter, we can ensure that the input value will be treated like we want
// The following line creates a new colors\Rgb instance from an HSL input:
$rgb2 = ColorFactory::newRgb([ 0, 100, 50 ], 'hsl');
\var_dump($rgb2->coordinates()); // [ 255, 0, 0, 255 ]
// Same result, but with usage of the ColorSpace enum:
$rgb3 = ColorFactory::newRgb([ 0, 100, 50 ], ColorSpace::Hsl);
\var_dump($rgb3->coordinates()); // [ 255, 0, 0, 255 ]
use matthieumastadenis\couleur\ColorFactory;
use matthieumastadenis\couleur\ColorSpace;
rgb(255,0,0)');
// Returns a new colors\HexRgb instance (space guessed automatically):
$hex = ColorFactory::new('#ff0000');
// Returns a new colors\Css instance (space guessed automatically):
$css = ColorFactory::new('red');
// Returns a new colors\Css instance from an RGB value:
$css = ColorFactory::new('rgb(255,0,0)', 'css');
// Same result but using the ColorSpace enum:
$css = ColorFactory::new('rgb(255,0,0)', ColorSpace::Css);
// Returns a new colors\Lch instance (using the ColorSpace enum):
$lch = ColorFactory::new([ 54.29, 106.84, 40.86 ], ColorSpace::Lch);
// Returns a new colors\OkLab instance from an RGB value (using the ColorSpace enum):
$okLab = ColorFactory::new([ 255, 0, 0 ], ColorSpace::OkLab, ColorSpace::Rgb);
use matthieumastadenis\couleur\ColorFactory;
use matthieumastadenis\couleur\ColorSpace;
= $rgb->toCss();
// Converting to a new colors\XyzD50 instance:
$xyzD50 = $css->toXyzD50();
// Converting to a new colors\OkLch instance (using the to() method):
$okLch = $xyzD50->to(ColorSpace::OkLch);
use matthieumastadenis\couleur\ColorFactory;
= $rgb->toCss();
// Prints 'red':
echo $css;
use matthieumastadenis\couleur\ColorFactory;
use matthieumastadenis\couleur\ColorSpace;
s 'rgb(100% 0% 0% / 100%)' ($alpha parameter):
echo $rgb->stringify(null, true);
// Prints 'rgba(255,0,0,1)' (using $legacy and $alpha parameters):
echo $rgb->stringify(true, true);
$lch = ColorFactory::newLch([ 54.2905429, 106.837191, 40.8576688 ], ColorSpace::Lch);
// Prints 'lch(54.29% 106.84 40.86deg)' Using the $precision parameter:
echo $lch->stringify(precision : 2);
use matthieumastadenis\couleur\utils\rgb;
use matthieumastadenis\couleur\utils\lch;
use matthieumastadenis\couleur\ColorSpace;
('red', ColorSpace::Css);
// All of the following convert and clean from RGB to HSL,
// returning [ 0, 100, 50, 100 ]:
$hsl1 = hsl\from([ 255, 0, 0, 255 ]);
$hsl2 = hsl\from('rgb(100% 0% 0% / 100%)');
$hsl3 = hsl\from('rgba(255,0,0,1)', 'rgb');
$hsl4 = hsl\from('rgba(255,0,0,1)', ColorSpace::Rgb);
use matthieumastadenis\couleur\utils\hexRgb;
use matthieumastadenis\couleur\utils\rgb;
use matthieumastadenis\couleur\utils\xyzD65;
, '00', sharp : false);
// Prints '#F00' (using array destructuring on clean() result):
echo hexRgb\stringify(... hexRgb\clean('#FF0000'));
// Prints '#FF0000' (using $short parameter):
echo hexRgb\stringify('FF', '00', '00', short : false);
// Prints '#F00F' (using $alpha parameter):
echo hexRgb\stringify('FF', '00', '00', alpha : true);
// Prints '#FF0000FF' (using $alpha and $short parameters):
echo hexRgb\stringify('FF', '00', '00', alpha : true, short : false);
// Prints 'rgb(100% 0% 0%)':
echo rgb\stringify(255, 0, 0);
// Prints 'rgb(100% 0% 0%)' (using array destructuring on clean() result):
echo rgb\stringify(... rgb\clean('rgb(255,0,0,1)'));
// Prints 'rgb(100% 0% 0% / 100%)' (using $alpha parameter):
echo rgb\stringify(255, 0, 0, alpha : true);
// Prints 'rgb(255,0,0)' (using $legacy parameter):
echo rgb\stringify(255, 0, 0, legacy : true);
// Prints 'rgba(255,0,0,1)' (using $legacy and $alpha parameters):
echo rgb\stringify(255, 0, 0, legacy : true, alpha : true);
// Prints 'color(xyz-d65 0.412390799 0.212639006 0.019330819)':
echo xyzD65\stringify(0.412390799, 0.212639006, 0.019330819);
// Prints 'color(xyz-d65 0.412390799 0.212639006 0.019330819 / 100%)' (using $alpha parameter):
echo xyzD65\stringify(0.412390799, 0.212639006, 0.019330819, alpha : true);
use matthieumastadenis\couleur\utils\css;
use matthieumastadenis\couleur\utils\hexRgb;
use matthieumastadenis\couleur\utils\hsl;
e:
\var_dump(hexRgb\verify('f00'));
\var_dump(hexRgb\verify('f00f'));
\var_dump(hexRgb\verify('ff0000'));
\var_dump(hexRgb\verify('ff0000ff'));
\var_dump(hexRgb\verify('#f00'));
\var_dump(hexRgb\verify('#f00f'));
\var_dump(hexRgb\verify('#ff0000'));
\var_dump(hexRgb\verify('#ff0000ff'));
// Returns false:
\var_dump(hexRgb\verify('invalid'));
// The following also return false, because they eventually could be mistaken for RGB values:
\var_dump(hexRgb\verify([ 'ff', '00', '00' ]));
\var_dump(hexRgb\verify([ 'ff', '00', '00', 'ff' ]));
// All of the following return true:
\var_dump(hsl\verify('hsl(0,100,50)'));
\var_dump(hsl\verify('hsl(0deg,100%,50%)'));
\var_dump(hsl\verify('hsla(0,100,50,1)'));
\var_dump(hsl\verify('hsla(0deg,100%,50%,1)'));
\var_dump(hsl\verify('color(hsl,0,100,50,1)'));
\var_dump(hsl\verify('color(hsl,0deg,100%,50%,1)'));
\var_dump(hsl\verify('color(hsl 0 100 50 / 1)'));
\var_dump(hsl\verify('color(hsl 0deg 100% 50% / 1)'));
// All of the following return false:
\var_dump(hsl\verify('0,100,50'));
\var_dump(hsl\verify('hsl 0,100'));
// The following also return false, because they eventually could be mistaken for RGB values:
\var_dump(hexRgb\verify([ 0, 100, 50 ]));
\var_dump(hexRgb\verify([ '0deg', '100%', '50%' ]));
use matthieumastadenis\couleur\utils\css;
use matthieumastadenis\couleur\utils\rgb;
use matthieumastadenis\couleur\CssColor;
turns [ 0.43606574282481, 0.22249319175624, 0.013923904500943, 1 ]
$xyzD50 = hsl\toXyzD50(... $hsl);
use matthieumastadenis\couleur\utils;
nknown'));
// Returns 7:
\var_dump(utils\constant('precision', 7));
// Creates the constant with a value of 3, then returns 3:
\var_dump(utils\constant('precision', 3, true));
// Now that the constant was created, always returns 3:
\var_dump(utils\constant('precision'));
use matthieumastadenis\couleur\utils;
use matthieumastadenis\couleur\ColorSpace;
e::Rgb:
$space = utils\findColorSpace('rgba(255,0,0,1)');
// Also returns ColorSpace::Rgb:
$space = utils\findColorSpace([ 255, 0, 0, 255 ]);
// Throws a UnknownColorSpace Exception:
$space = utils\findColorSpace('invalid');
// Returns ColorSpace::Rgb (using the $fallback parameter):
$space = utils\findColorSpace('invalid', ColorSpace::Rgb);
// Returns null (using the $throw parameter):
$space = utils\findColorSpace('invalid', throw : false);
use matthieumastadenis\couleur\utils;
use matthieumastadenis\couleur\ColorSpace;
,0,1)'));
\var_dump(utils\isColorString('rgb(100% 0% 0% / 100%)', 'rgb'));
\var_dump(utils\isColorString('rgba(255,0,0,1)', [ 'rgb', 'rgba' ]));
\var_dump(utils\isColorString('color(srgb 100% 0% 0% / 100%)', ColorSpace::Rgb));
// All of the following return false:
\var_dump(utils\isColorString('invalid'));
\var_dump(utils\isColorString('rgb 100%'));
\var_dump(utils\isColorString('255,0,0'));
\var_dump(utils\isColorString('rgba(255,0,0,1)', 'rgb'));
\var_dump(utils\isColorString('srgb(255,0,0,1)', [ 'rgb', 'rgba' ]));
\var_dump(utils\isColorString('myCustomRgb( 100% 0% 0% / 100%)', ColorSpace::Rgb));
\var_dump(utils\isColorString('color(myCustomRgb 100% 0% 0% / 100%)', ColorSpace::Rgb));
use matthieumastadenis\couleur\utils;
use matthieumastadenis\couleur\ColorSpace;
arameter):
$css = utils\to('red', 'css');
// Returns [ 255, 0, 0, 255 ]:
// ([ CssColor::red ] is a valid CSS color so we can omit the $from parameter):
$rgb = utils\to($css, ColorSpace::Rgb);
// Returns [ 0, 100, 50, 100 ]:
// ([ 255, 0, 0, 255 ] is a valid RGB color so we can omit the $from parameter):
$hsl = utils\to($rgb, ColorSpace::Hsl);
// Returns [ 54.29054294697, 80.804920334624, 69.890988258963, 100 ]
// (the $from parameter avoids HSL array being interpreted as RGB):
$lab = utils\to($hsl, ColorSpace::Lab, ColorSpace::Hsl);
// Returns [ 54.29054294697, 106.83719104366, 40.857668782131, 100 ]
// (the $from parameter avoids Lab array being interpreted as RGB):
$lch = utils\to($lab, ColorSpace::Lch, ColorSpace::Lab);
// Returns [ 0.41239079028139, 0.21263903420017, 0.01933077971095, 100 ]
// (the $from parameter avoids Lch array being interpreted as RGB):
$xyzD65 = utils\to($lch, ColorSpace::XyzD65, ColorSpace::Lch);
// Returns [ 0.70226883304033, 0.27562276714962, 0.10344904551878, 1 ]
// (here we use a valid string input so we can omit the $from paramter):
$proPhoto = utils\to('color(xyz-d65 0.4124 0.2126 0.0193 / 100%)', ColorSpace::ProPhoto);
use matthieumastadenis\couleur\Constant;
);
// Always returns 0, which is the default value for the COULEUR_LEGACY constant:
Constant::LEGACY->value;
// Always returns 9, which is the default value for the COULEUR_PRECISION constant:
Constant::PRECISION->value;
// Returns the value of the COULEUR_LEGACY constant if defined, or 0 by default:
Constant::LEGACY->value();
// Returns the value of the COULEUR_PRECISION constant if defined, or 9 by default:
Constant::PRECISION->value();
// Returns the value of the COULEUR_LEGACY constant if defined, or 1 as fallback:
Constant::LEGACY->value(1);
// Returns the value of the COULEUR_PRECISION constant if defined, or 3 as fallback:
Constant::PRECISION->value(3);
// Returns the value of the COULEUR_LEGACY constant if defined, or define it with 1 as value then returns 1:
Constant::LEGACY->value(1, true);
// Returns the value of the COULEUR_PRECISION constant if defined, or define it with 3 as value then returns 3:
Constant::PRECISION->value(3, true);
use matthieumastadenis\couleur\ColorSpace;
s();
use matthieumastadenis\couleur\ColorSpace;
use matthieumastadenis\couleur\colors\Lch;
use matthieumastadenis\couleur\colors\LinP3;
use matthieumastadenis\couleur\colors\Rgb;
use matthieumastadenis\couleur\ColorSpace;
lAliases();
// Returns aliases of the HexRgb space:
ColorSpace::HexRgb->aliases();
// Returns ColorSpace::Rgb:
ColorSpace::fromAlias('rgb');
ColorSpace::fromAlias('srgb');
ColorSpace::fromAlias('RGBA');
// Returns ColorSpace::Lab:
ColorSpace::fromAlias('lab');
ColorSpace::fromAlias('cielab');
ColorSpace::fromAlias('CIE-LAB');
use matthieumastadenis\couleur\CssColor;
o #FA1111:
CssColor::fromHexRgb('FA', '11', '11');
// Throws a UnsupportedCssColor Exception:
CssColor::fromHexRgb('FA', '11', '11', false);
// Returns CssColor::pink:
CssColor::fromHexRgb('FA', '11', '11', false, CssColor::pink);
// Returns null:
CssColor::fromHexRgb('FA', '11', '11', false, null, false);
// Returns CssColor::red which is the closest to rgb(250,10,10):
CssColor::fromRgb(250, 10, 10);
// Throws a UnsupportedCssColor Exception:
CssColor::fromRgb(250, 10, 10, false);
// Returns CssColor::pink:
CssColor::fromRgb(250, 10, 10, false, CssColor::pink);
// Returns null:
CssColor::fromRgb(250, 10, 10, false, null, false);
use matthieumastadenis\couleur\CssColor;
g();
// Returns '#f00' (using the $uppercase parameter):
CssColor::red->toHexRgbString(uppercase : false);
// Returns 'F00' (using the $sharp parameter):
CssColor::red->toHexRgbString(sharp : false);
// Returns '#F00F' (using the $alpha parameter):
CssColor::red->toHexRgbString(true);
// Returns '#FF0000' (using the $short parameter):
CssColor::red->toHexRgbString(short : false);
// Returns '#FF0000FF' (using $alpha and $short parameters):
CssColor::red->toHexRgbString(true, false);
// Returns '#ff0000ff' (using $alpha, $short and $uppercase parameters):
CssColor::red->toHexRgbString(true, false, false);
// Returns 'ff0000ff' (using $alpha, $short, $uppercase and $sharp parameters):
CssColor::red->toHexRgbString(true, false, false, false);
// Returns 'rgb(100% 0% 0%)':
CssColor::red->toRgbString();
// Returns 'rgb(100% 0% 0% / 100%)' (using the $alpha parameter):
CssColor::red->toRgbString(alpha : true);
// Returns 'rgb(255,0,0)' (using the $legacy parameter):
CssColor::red->toRgbString(true);
// Returns 'rgba(255,0,0,1)' (using the $legacy and $alpha parameters):
CssColor::red->toRgbString(true, true);
use matthieumastadenis\couleur\CssColor;
:red->toCss();
// Returns a new colors\HexRgb instance:
CssColor::red->toHexRgb();
// Returns a new colors\Rgb instance:
CssColor::red->toRgb();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.