Skip to content

LasseRafn/php-hexer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HEX color modifier — brightness, conversion and more.

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

Build Status Coverage StyleCI Status Total Downloads Latest Stable Version License

Installation

You just require using composer and you're good to go!

composer require lasserafn/php-hexer

Usage

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]

Methods

The constructor accepts one parameter (hex) which can optionally contain a hashtag (#). The length has to be between 3-6 characters (without the hashtag).

lighten($percentage)

Will lighten the color by X percentage. Percentage must be between 0-100. An exception will be thrown otherwise.

darken($percentage)

Will darken the color by X percentage. Percentage must be between 0-100. An exception will be thrown otherwise.

toRgb()

Will return the hex as RGB (an array of r, g, b).

Exceptions

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.

Requirements

  • PHP 5.6, 7.0 or 7.1