A PHP library to deal with color conversions
[TOC]
Coloreeze is a PHP library to deal with color conversions.
Currently it supports the following color spaces:
- Hexadecimal
- Integer
- RGB(a)
- HSB/HSV
- HSL
- CMYK
- CIELab
- XYZ
Additionally this library contains some useful methods to:
- Generate a greyscale version from a color
- Generate a darker version from a color
- Generate a lighter version from a color
- to create gradients and measure the distance CIE76 between colors.
You can install the package via composer:
$ composer require alcidesrc/coloreezeColoreeze package contains independent color classes, all of them implementing a Color interface:
- ColorCIELab
- ColorCMYK
- ColorFactory
- ColorHSB
- ColorHSL
- ColorHex
- ColorInt
- ColorRGBA
- ColorXYZ
The ColorFactory class allows you to create a color instance from any valid input string.
If the input string is not a valid color representation it throws an InvalidInput exception.
ColorFactory::fromString('rgb(0,100,200)'); // Returns a `ColorRGBA` instance
ColorFactory::fromString('#336699'); // Returns a `ColorHex` instance
ColorFactory::fromString('unknown(1,2,3)'); // Throws an `InvalidInput` exceptionCast the color value to a string:
echo ColorRGBA::fromString('rgba(0,100,200,1.0)');
echo ColorHex::fromString('#336699');
'rgba(0,100,200,1.0)'
'#336699'Parses an input string and returns accordingly the related Color implementation:
$hex = ColorHex::fromString('#336699');
$rgba = ColorRGBA::fromString('rgba(0,100,200,1.0)');
...It throws an InvalidInput exception in case of the string is not well formed or unsupported color.
Returns the Color value. On single-value colors, this method returns a primitive value (int or string) but on composite ones it returns an array with color's components:
$int = ColorInt::fromString('int(100)');
var_dump($int->getValue());
int(100)$rgba = ColorRGBA::fromString('rgba(0, 100, 200)');
var_dump($int->getValue());
array(3) {
[0]=>
int(0)
[1]=>
int(100)
[2]=>
int(200)
}$hex = ColorHex::fromString('#336699');
var_dump($int->getValue());
string(7) "#336699"Converts a color to a CIELab:
$lab = ColorHex::fromString('#336699')->toCIELab();Converts a color to a CMYK:
$cmyk = ColorHex::fromString('#336699')->toCMYK();Converts a color to a Hex:
$hex = ColorRGBA::fromString('rgba(100,200,200,1.0)')->toHex();Converts a color to a HSB/HSV:
$hsb = ColorHex::fromString('#336699')->toHSB();Converts a color to a HSL:
$hsl = ColorHex::fromString('#336699')->toHSL();Converts a color to an Int:
$int = ColorHex::fromString('#336699')->toInt();$rgba = ColorInt::fromString('int(255)')->toRGBA();$rgba = ColorCMYK::fromString('cmyk(0,0,0,0)')->toXYZ();$complementary = ColorRGBA::fromString('hsl(182,25,50)')->toComplementary();$greyscale = ColorInt::fromString('int(4278255615)')->toGreyscale();$dark = ColorInt::fromString('int(4278255615)')->adjustBrightness(-10);
$light = ColorInt::fromString('int(4278255615)')->adjustBrightness(10);$distance = ColorInt::fromString('int(4278255615)')->distanceCIE76(ColorInt::fromString('int(0)'));You can run the test suite via composer:
$ composer testsThis Composer script runs the PHPUnit command with PCOV support in order to generate a Code Coverage report.
This library provides a PHPUnit testsuite with 1434 unit tests and 2670 assertions:
Time: 00:00.426, Memory: 24.00 MB
OK (1434 tests, 2670 assertions)Here is the Code Coverage report summary:
Code Coverage Report:
2022-07-22 06:32:15
Summary:
Classes: 100.00% (11/11)
Methods: 100.00% (135/135)
Lines: 100.00% (475/475)Full report will be generated in ./reports/coverage folder.
You can check this library with PHPStan:
$ composer phpstanThis command generates the following report:
> vendor/bin/phpstan analyse --level 9 --memory-limit 1G --ansi ./src ./tests
29/29 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
[OK] No errorsYou can check this library with PHP Parallel Lint:
$ composer linterThis command generates the following report:
PHP 8.1.8 | 10 parallel jobs
............................. 29/29 (100 %)
Checked 29 files in 0.1 seconds
No syntax error foundYou can check this library with PHP Insights:
$ composer phpinsightsThis command generates the following summary:
> ./vendor/bin/phpinsights --fix
16/16 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
✨ Analysis Completed !
[2022-07-22 08:20:20] `/code`
99.0% 89.5% 94.1% 100 %
Code Complexity Architecture Style
Score scale: ◼ 1-49 ◼ 50-79 ◼ 80-100
[CODE] 99 pts within 494 lines
Comments ....................................................... 5.1 %
Classes ....................................................... 85.6 %
Functions ...................................................... 0.0 %
Globally ....................................................... 9.3 %
[COMPLEXITY] 89.5 pts with average of 1.32 cyclomatic complexity
[ARCHITECTURE] 94.1 pts within 13 files
Classes ....................................................... 84.6 %
Interfaces ..................................................... 7.7 %
Globally ....................................................... 7.7 %
Traits ......................................................... 0.0 %
[MISC] 100 pts on coding style and 0 security issues encounteredPlease review our security policy on how to report security vulnerabilities:
PLEASE DON'T DISCLOSE SECURITY-RELATED ISSUES PUBLICLY
Only the latest major version receives security fixes.
If you discover a security vulnerability within this project, please open an issue here. All security vulnerabilities will be promptly addressed.
The MIT License (MIT). Please see License File for more information.