scripturadesign / color
颜色处理
0.1.3
2016-08-01 07:17 UTC
Requires
- php: ^5.6|^7.0
Requires (Dev)
- fabpot/php-cs-fixer: ^1.10
- hamcrest/hamcrest-php: 1.2.*
- phpunit/phpunit: 4.8.*
This package is auto-updated.
Last update: 2024-08-28 21:58:32 UTC
README
Color是一个用于在颜色类型之间进行转换的包。
安装
通过Composer
$ composer require scripturadesign/color
使用方法
HEX
十六进制
$hex = new \Scriptura\Color\Types\HEX('ff0000'); // New HEX color $hex = new \Scriptura\Color\Types\HEX('ff0000', '#{code}'); // New HEX color with template echo $hex->code(); // 'FF0000' echo $hex; // '#FF0000' echo $hex->withTemplate('color: #{code};'); // 'color: #FF0000;' $hex = $hex->toHEX(); // Convert the color to \Scriptura\Color\Types\HEX $rgb = $hex->toRGB(); // Convert the color to \Scriptura\Color\Types\RGB $hsl = $hex->toHSL(); // Convert the color to \Scriptura\Color\Types\HSL $c256 = $hex->to256(); // Convert the color to \Scriptura\Color\Types\C256
默认模板
#{code}
清洗
HEX进行一些清洗以支持各种十六进制颜色表示方法。
示例
123 => '112233'
987654 => '987654'
'aBc' => 'AABBCC'
'FeDcBa' => 'FEDCBA'
'#1B5' => '11BB55'
'#1a2b3c' => '1A2B3C'
RGB
红、绿、蓝
$rgb = new \Scriptura\Color\Types\RGB(255, 0, 0); // New RGB color $rgb = new \Scriptura\Color\Types\RGB(255, 0, 0, '{red},{green},{blue}'); // New RGB color with template echo $rgb->red(); // 255 echo $rgb->green(); // 0 echo $rgb->blue(); // 0 echo $rgb->rgb(); // [255, 0, 0] echo $rgb; // '255,0,0' echo $rgb->withTemplate('color: rgb({red}, {green}, {blue});'); // 'color: rgb(255, 0, 0);' $rgb = $rgb->withRed(0); // New instance with red set to 0 $rgb = $rgb->withGreen(255); // New instance with green set to 255 $rgb = $rgb->withBlue(255); // New instance with blue set to 255 $hex = $rgb->toHEX(); // Convert the color to \Scriptura\Color\Types\HEX $rgb = $rgb->toRGB(); // Convert the color to \Scriptura\Color\Types\RGB $hsl = $rgb->toHSL(); // Convert the color to \Scriptura\Color\Types\HSL $c256 = $rgb->to256(); // Convert the color to \Scriptura\Color\Types\C256
默认模板
{red},{green},{blue}
HSL
色调、饱和度、亮度
$hsl = new \Scriptura\Color\Types\HSL(0, 100, 50); // New HSL color $hsl = new \Scriptura\Color\Types\HSL(0, 100, 50, '{hue}° {saturation}% {lightness}%'); // New HSL color with template echo $hsl->hue(); // 0 echo $hsl->saturation(); // 100 echo $hsl->lightness(); // 50 echo $hsl->hsl(); // [0, 100, 50] echo $hsl; // '0° 100% 50%' echo $hsl->withTemplate('color: ({hue}, {saturation}%, {lightness}%);'); // 'color: hsl(0, 100%, 50%);' $hsl = $hsl->withHue(180); // New instance with hue set to 180 $hsl = $hsl->withSaturation(50); // New instance with saturation set to 50 $hsl = $hsl->withLightness(25); // New instance with lightness set to 25 $hex = $hsl->toHEX(); // Convert the color to \Scriptura\Color\Types\HEX $rgb = $hsl->toRGB(); // Convert the color to \Scriptura\Color\Types\RGB $hsl = $hsl->toHSL(); // Convert the color to \Scriptura\Color\Types\HSL $c256 = $hsl->to256(); // Convert the color to \Scriptura\Color\Types\C256 $hsl = $hsl->lighten(10); // New instance that is lightened by 10% $hsl = $hsl->darken(10); // New instance that is darkened by 10% $hsl = $hsl->saturate(10); // New instance that is saturated by 10% $hsl = $hsl->desaturate(10); // New instance that is desaturated by 10% $mix = $hsl->mix(new \Scriptura\Color\Types\HSL(120, 100, 50)); // Get a new color that is a mix between two colors
默认模板
{hue}° {saturation}% {lightness}%
256
终端256色
000 - 007
:标准颜色008 - 015
:高亮颜色016 - 231
:216种颜色232 - 255
:灰度(从黑到白) https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
$c256 = new \Scriptura\Color\Types\C256(196); // New 256 color $c256 = new \Scriptura\Color\Types\C256(196, '{code}'); // New 256 color with template echo $c256->code(); // 196 echo $c256; // '196' echo $c256->withTemplate('\e[48;{code}m'); // '\e[48;196m' $hex = $c256->toHEX(); // Convert the color to \Scriptura\Color\Types\HEX $rgb = $c256->toRGB(); // Convert the color to \Scriptura\Color\Types\RGB $hsl = $c256->toHSL(); // Convert the color to \Scriptura\Color\Types\HSL $c256 = $c256->to256(); // Convert the color to \Scriptura\Color\Types\C256
默认模板
{code}
变更日志
请参阅[CHANGELOG][link-changelog]了解最近的变化信息。
测试
可以使用以下Composer脚本来运行测试套件。
$ composer test
贡献和分支
请注意,此项目采用MIT许可证。我们鼓励分支此项目,但要求您保留所有版权、归属说明,并在您分支的项目中继续使用MIT许可证。
有关贡献指南的更多信息,请阅读贡献指南。
安全性
如果您发现任何安全问题,请通过martindilling@gmail.com发送电子邮件,而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。