lasserafn / php-hexer
一个用于调整十六进制颜色代码亮度的包
v1.10
2022-10-15 05:14 UTC
Requires
- php: ^5.6|^7.0|^8.0
Requires (Dev)
- phpunit/phpunit: ^5.7
- satooshi/php-coveralls: ^1.0
This package is auto-updated.
Last update: 2024-09-15 18:56:13 UTC
README
是否想过在PHP中调整十六进制颜色的亮度或暗度?此包允许您这样做。它易于使用,经过全面测试,且非常轻量。
+ 添加了转换为RGB的功能
安装
您只需使用Composer进行要求,然后即可开始使用!
composer require lasserafn/php-hexer
用法
与安装一样,使用也很简单。
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]
方法
构造函数接受一个参数(hex
),该参数可以包含可选的井号(#)。长度必须在3-6个字符之间(不包含井号)。
lighten($percentage)
将颜色亮度增加X百分比。百分比必须在0-100之间。否则将抛出异常。
darken($percentage)
将颜色亮度减少X百分比。百分比必须在0-100之间。否则将抛出异常。
toRgb()
将十六进制值返回为RGB(一个包含r
、g
、b
的数组)。
异常
如果您输入的十六进制颜色长度小于3个字符或大于6个字符,将抛出异常。与百分比类似,如果指定的百分比小于0或大于100,也会抛出异常。如果百分比是0,则直接返回十六进制颜色本身。
需求
- PHP 5.6、7.0或7.1