Ever wanted to lighten or darken a hex in PHP? This package will allow you to. It's easy to use, fully tested and is very lightweight.
+ Added the ability to convert to RGB
You just require using composer and you're good to go!
composer require lasserafn/php-hexer
As with installation, usage is quite simple.
use LasseRafn\Hexer\Hex;
// Lighten
$hex = new Hex('#333'); // You can leave out the hashtag if you wish.
echo $hex->lighten(15); // Output: #595959 (if you left out the hashtag, it would not be included in the output either)
// Darken
$hex = new Hex('ffffff');
echo $hex->darken(15); // Output: d9d9d9
// To RGB
$hex = new Hex('007F00');
$hex->lighten(50)->toRgb(); // Returns: ['r' => 128, 'g' => 255, 'b' => 128]
The constructor accepts one parameter (hex
) which can optionally contain a hashtag (#). The length has to be between 3-6 characters (without the hashtag).
Will lighten the color by X percentage. Percentage must be between 0-100. An exception will be thrown otherwise.
Will darken the color by X percentage. Percentage must be between 0-100. An exception will be thrown otherwise.
Will return the hex as RGB (an array of r
, g
, b
).
If you input a HEX which is less than 3 characters of length, or greater than 6, an exception will be thrown. Similar with percentages, if you specify a percentage less than zero, or greater than 100, an exception will be thrown. If the percentage is zero, the hex itself will simply be returned.
- PHP 5.6, 7.0 or 7.1