Pop Color is a helpful component to manage different types of color values and conversions. Supported color formats include:
- RGB
- HEX
- HSL
- CMYK
- Grayscale
Within the Pop PHP Framework, the pop-css, pop-image and pop-pdf components use this component.
Install pop-color using Composer.
composer require popphp/pop-color
Or, require it in your composer.json file
"require": {
"popphp/pop-color" : "^1.0.3"
}
$rgb = Color::rgb(120, 60, 30, 0.5);
echo $rgb . PHP_EOL;The above command will print the default CSS format:
rgba(120, 60, 30, 0.5)
$hex = $rgb->toHex();
echo $hex . PHP_EOL;#783c1e
$hsl = $hex->toHsl();
echo $hsl . PHP_EOL;hsl(20, 75%, 47%)
// Will print a string of space-separated values, common to the PDF color string format
$cmyk = $rgb->toCmyk();
echo $cmyk . PHP_EOL; 0 0.5 0.75 0.53
$rgb = Color::rgb(120, 60, 30, 0.5);
echo $rgb->getR() . PHP_EOL;
echo $rgb->getG() . PHP_EOL;
echo $rgb->getB() . PHP_EOL;
echo $rgb->getA() . PHP_EOL;120
60
30
0.5
$cmyk = Color::cmyk(60, 30, 20, 50);
echo $cmyk->getC() . PHP_EOL;
echo $cmyk->getM() . PHP_EOL;
echo $cmyk->getY() . PHP_EOL;
echo $cmyk->getK() . PHP_EOL;60
30
20
50
$rgb = Color::parse('rgba(120, 60, 30, 0.5)');
echo $rgb->getR() . PHP_EOL;
echo $rgb->getG() . PHP_EOL;
echo $rgb->getB() . PHP_EOL;
echo $rgb->getA() . PHP_EOL;
echo $rgb . PHP_EOL;120
60
30
0.5
rgba(120, 60, 30, 0.5)