devlop/colours

Collection of color classes to simplify working and converting colors in PHP.

v1.0.2 2024-06-19 01:23 UTC

This package is auto-updated.

Last update: 2024-10-19 02:06:45 UTC


README

Latest Stable Version License

Colours

Collection of color classes to simplify working with and converting colors in PHP.

Installation

composer require devlop/colours

Usage

HexColor

use Devlop\Colours\HexColor;

// check validity
HexColor::isValid('#fff'); // bool(true)
HexColor::isValid('#0B33B1'); // bool(true)
HexColor::isValid('#19b9cd'); // bool(true)
HexColor::isValid('#b9cd'); // bool(false)
HexColor::isValid('red'); // bool(false)
HexColor::isValid('19b9cd'); // bool(true)

// check validity (strict mode)
HexColor::isValid('19b9cd', true); // bool(false)

// creating from hex string
$hexColor = new HexColor('#f0f');

// creating from other formats
$hexColor = HexColor::fromHslColor($hslColorInstance);
$hexColor = HexColor::fromRgbColor($rgbColorInstance);
$hexColor = HexColor::fromCmykColor($cmykColorInstance);

// get the hex string
$hexColor->getHexString(); // string('#ff00ff')

// converting to other formats
$hslColor = $hexColor->toHsl();
$rgbColor = $hexColor->toRgb();
$cmykColor = $hexColor->toCmyk();

HslColor

use Devlop\Colours\HslColor;

// creating from HSL values
$hslColor = new HslColor(180, 96, 77);

// creating from other formats
$hslColor = HslColor::fromHexString('#b187F5');
$hslColor = HslColor::fromHexColor($hexColorInstance);
$hslColor = HslColor::fromRgbColor($rgbColorInstance);

// get HSL properties
$hslColor->getHue(); // int(180)
$hslColor->getSaturation(); // int(96)
$hslColor->getLightness(); // int(77)

// converting to other formats
$hexColor = $hslColor->toHex();
$rgbColor = $hslColor->toRgb();

RgbColor

use Devlop\Colours\RgbColor;

// creating from RGB values
$rgbColor = new RgbColor(221, 186, 146);

// creating from other formats
$rgbColor = RgbColor::fromHexString('#f3840c');
$rgbColor = RgbColor::fromHexColor($hexColorInstance);
$rgbColor = RgbColor::fromHslColor($hslColorInstance);
$rgbColor = RgbColor::fromCmykColor($cmykColorInstance);

// get RGB properties
$rgbColor->getRed(); // int(221)
$rgbColor->getGreen(); // int(186)
$rgbColor->getBlue(); // int(146)

// converting to other formats
$hexColor = $rgbColor->toHex();
$hslColor = $rgbColor->toHsl();
$cmykColor = $rgbColor->toCmyk();

CmykColor

use Devlop\Colours\CmykColor;

// creating from CMYK values
$cmykColor = new CmykColor(20, 80, 65, 34);

// creating from other formats
$cmykColor = CmykColor::fromHexString('#f3840c');
$cmykColor = CmykColor::fromHexColor($hexColorInstance);
$cmykColor = CmykColor::fromRgbColor($rgbColorInstance);

// get CMYK properties
$cmykColor->getCyan(); // int(20)
$cmykColor->getMagenta(); // int(80)
$cmykColor->getYellow(); // int(65)
$cmykColor->getKey(); // int(34)

// converting to other formats
$hexColor = $cmykColor->toHex();
$rgbColor = $cmykColor->toRgb();