A little library to handle color conversions. Currently supports rgb, rgba and hex formats.
$rgb = Rgb::fromString('rgb(55,155,255)');
echo $rgb->red(); // 55
echo $rgb->green(); // 155
echo $rgb->blue(); // 255
echo $rgb; // rgb(55,155,255)
$rgba = $rgb->toRgba(); // `Spatie\Color\Rgba`
$rgba->alpha(); // 1
echo $rgba; // rgba(55,155,255,1)
$hex = $rgb->toHex(); // `Spatie\Color\Hex`
echo $hex; // #379bff
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.
The best postcards will get published on the open source page on our website.
You can install the package via composer:
composer require spatie/color
The Color
package contains a seperate class per color format.
Can be instantiated with integer values:
$rgb = new Rgb(55, 155, 255);
Or can be created from a string:
$rgb = Rgb::fromString('rgb(55,155,255)');
$rgb = new Rgb(55, 155, 255);
$rgb->red(); // 55
$rgb->green(); // 155
$rgb->blue(); // 255
$rgb->toHex(); // `Spatie\Color\Hex`
$rgb->toRgba(); // `Spatie\Color\Rgba`
$rgb->toRgba(0.5); // `Spatie\Color\Rgba` with alpha 0.5
Can be instantiated with integer and float values:
$rgba = new Rgba(55, 155, 255, 0.5);
Or can be created from a string:
$rgba = Rgba::fromString('rgba(55,155,255,0.5)');
$rgba = new Rgba(55, 155, 255, 0.5);
$rgba->red(); // 55
$rgba->green(); // 155
$rgba->blue(); // 255
$rgba->alpha(); // 0.5
When converting to a format that doesn't support alpha, the alpha channel will be ignored
$rgba->toRgb(); // `Spatie\Color\Rgb`
$rgba->toHex(); // `Spatie\Color\Hex`
Can be instantiated with string values:
$hex = new Hex('aa', 'bb', 'cc');
Or can be created from a string:
$hex = Hex::fromString('#aabbcc');
$hex = new Hex('aa', 'bb', 'cc');
$hex->red(); // 'aa'
$hex->green(); // 'bb'
$hex->blue(); // 'cc'
$hex->toRgb(); // `Spatie\Color\Rgb`
$hex->toRgba(); // `Spatie\Color\Rgba`
$hex->toRgba(0.5); // `Spatie\Color\Rgba` with alpha 0.5
Please see CHANGELOG for more information what has changed recently.
$ composer test
Please see CONTRIBUTING for details.
If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
The MIT License (MIT). Please see License File for more information.