https://github.com/lasserafn/php-hexer
Modify HEX brightness
https://github.com/lasserafn/php-hexer
brightness color darken hex lighten php
Last synced: over 1 year ago
JSON representation
Modify HEX brightness
- Host: GitHub
- URL: https://github.com/lasserafn/php-hexer
- Owner: LasseRafn
- License: mit
- Created: 2017-06-16T09:14:06.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-02-15T17:48:37.000Z (over 2 years ago)
- Last Synced: 2025-04-09T20:05:46.359Z (over 1 year ago)
- Topics: brightness, color, darken, hex, lighten, php
- Language: PHP
- Size: 26.4 KB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# 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**
## Installation
You just require using composer and you're good to go!
```bash
composer require lasserafn/php-hexer
```
## Usage
As with installation, usage is quite simple.
```php
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