abyrate/colorist

此包已被放弃,不再维护。未建议替代包。

颜色操作包

v2.0.2 2018-07-09 18:34 UTC

This package is auto-updated.

Last update: 2020-02-15 10:02:46 UTC


README

PHP version Latest Stable Version Build Status codecov Scrutinizer Code Quality Total Downloads License

此包允许您转换和管理颜色模型。

支持的颜色模型

  • RGB (RGBA)
  • HEX (HEXA)
  • html颜色名称

支持简写十六进制代码 (#001122, #012, #00112233, #0123)

您可以在此处查看可用的颜色名称列表 这里

计划中

  • HSL (HSLA)
  • HSV (HSVA)
  • CMYK
  • Lab

要求

  • PHP >= 7.1

安装

运行命令

$ composer require abyrate/colorist

或者在您的根 composer.json 文件中添加以下内容

{
	"require": {
		"abyrate/colorist": "~2.0"
	}
}

然后运行命令

$ composer update

使用方法

如果您更改了任何模型的值,其余值将自动更新

创建

// Create an object in the standard way
$color = new \Abyrate\Colorist('rgb(55,191,0)');

// Create using static method
$color = \Abyrate\Colorist::create('rgb(55,191,0)');

支持的语法

  • 'rgb(0,0,0)' - rgb模型
  • 'rgba(0,0,0,0)' - 带有alpha通道的rgb模型
  • '#000000' - 十六进制模型
  • '#00000000' - 带有alpha通道的十六进制模型
  • 'orange' - 名称模型

通道范围

  • r, g, b - 0-255 (在十六进制中为 00-ff)
  • alpha - 0-1 (浮点值。在十六进制中为 00-ff)

获取

通道

$colorist->getChannel('red');   // get red channel
$colorist->getChannel('green'); // get green channel
$colorist->getChannel('blue');  // get blue channel
$colorist->getChannel('alpha'); // get alpha channel
$colorist->getChannel('hex');   // get hex code (e.g. #15af45)
$colorist->getChannel('hexa');  // get hex code with alpha (e.g. #15af4505)
$colorist->getChannel('name');  // get color name (e.g. orange)

模型

$colorist->get('rgb');  // get rgb string (e.g. 15,156,10)
$colorist->get('rgba'); // get rgb string with alpha channel (e.g. 15,156,10,0.3)
$colorist->get('hex');  // get rgb in the hex format string (e.g. #15af45)
$colorist->get('hexa'); // get rgb with alpha channel in hex string (e.g. #15af4505)
$colorist->get('name'); // get color name (e.g. orange)

设置

通道

$colorist->setChannel('red', 15);           // set red channel
$colorist->setChannel('green', 20);         // set green channel
$colorist->setChannel('blue', 25);          // set blue channel
$colorist->setChannel('alpha', 0.3);        // set alpha channel
$colorist->setChannel('hex', '#004');       // set hex code
$colorist->setChannel('hexa', '#00112233'); // set hex code with alpha
$colorist->setChannel('name', 'orange');    // set color name

模型

$colorist->set('rgb', 'rgb(0,15,36)');       // set rgb string
$colorist->set('rgba', 'rgb(0,15,36, 0.1)'); // set rgb string with alpha channel
$colorist->set('hex', '#123');               // set rgb in the hex format string
$colorist->set('hexa', '#1234');             // set rgb with alpha channel in hex string
$colorist->set('name', 'orange');            // set color name (e.g. orange)

API 文档