Skip to content

mybandmarket/color

 
 

Repository files navigation

A little library to handle color conversions

Latest Version on Packagist Software License Build Status SensioLabsInsight Quality Score StyleCI Total Downloads

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.

Postcardware

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.

Installation

You can install the package via composer:

composer require spatie/color

Usage

The Color package contains a seperate class per color format.

Spatie\Color\Rgb

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)');

Channel methods

$rgb = new Rgb(55, 155, 255);

$rgb->red(); // 55
$rgb->green(); // 155
$rgb->blue(); // 255

Conversion methods

$rgb->toHex(); // `Spatie\Color\Hex`

$rgb->toRgba(); // `Spatie\Color\Rgba`
$rgb->toRgba(0.5); // `Spatie\Color\Rgba` with alpha 0.5

Spatie\Color\Rgba

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)');

Channel methods

$rgba = new Rgba(55, 155, 255, 0.5);

$rgba->red(); // 55
$rgba->green(); // 155
$rgba->blue(); // 255
$rgba->alpha(); // 0.5

Conversion methods

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`

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');

Channel methods

$hex = new Hex('aa', 'bb', 'cc');

$hex->red(); // 'aa'
$hex->green(); // 'bb'
$hex->blue(); // 'cc'

Conversion methods

$hex->toRgb(); // `Spatie\Color\Rgb`

$hex->toRgba(); // `Spatie\Color\Rgba`
$hex->toRgba(0.5); // `Spatie\Color\Rgba` with alpha 0.5

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.

Credits

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.

About

A little library to deal with color conversions

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%