savvywombat / color
用于在多个颜色空间中操作颜色的包。
Requires
- php: ^7.4||^8
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpunit/phpunit: ^9.5.0
README
一个PHP包,用于在HSL和RGB颜色空间/类型之间进行转换和操作。
主要特性
- 支持CSS颜色模块3的颜色规范(#323C46,rgb(50,60,70),hsl(210,16.7%,23.5%));
- 支持alpha透明度(#323C4680,rgba(50,60,70,0.5),hsla(210,16.7%,23.5%,0.5));
- 修改器可以更改红色/绿色/蓝色或色调/饱和度/亮度以及alpha在任何颜色类型上;
- 能够扩展自定义颜色空间/类型,然后可以进行转换和/或修改;
- 能够扩展自定义修改器;
- 不可变行为。
$rgb = new \SavvyWombat\Color\RGB(50, 60, 70); echo (string) $rgb; // rgb(50,60,70) $hsl = $rgb->toHsl(); echo (string) $hsl; // hsl(210,16.7%,23.5%); $redderHsl = $hsl->red('+50'); echo (string) $redderHsl; // hsl(345,25%,31.4%); $lighterRgb = $rgb->lighten('+10%'); echo (string) $lighterRgb; // rgb(55,66,77); $transparentRgb = $rgb->alpha('0.5'); echo (string) $transparentRgb; // rgb(50,60,70,0.5) echo (string) $rgb->toHex(); // #323C46
安装
composer require savvywombat\color
用法
创建颜色
以下模式默认可用以创建颜色
Color::fromString('#123'); // Hex: #112233 Color::fromString('#1234'); // Hex (with alpha): #11223344 Color::fromString('#123456'); // Hex: #123456 Color::fromString('#12345678'); // Hex: #12345678 Color::fromString('rgb(10, 20, 30)'); // RGB Color::fromString('rgba(10, 20, 30, 0.4)'); // RGB Color::fromString('hsl(10, 20%, 30%)'); // HSL Color::fromString('hsl(10, 20%, 30%, 0.4)'); // HSL
转换颜色
颜色可以转换为任何其他注册的颜色类型
echo (string) Color::fromString('#123')->toHsl(); // hsl(210,50%,8.6%) echo (string) Color::fromString('#123')->toRGB(); // rgb(17,34,51) echo (string) Color::fromString('rgb(25, 75, 125)')->toHex(); // #194B7D echo (string) Color::fromString('rgb(25, 75, 125)')->toHsl(); // hsl(210,66.7%,39.4%) echo (string) Color::fromString('hsl(135, 50%, 75%)')->toHex(); // #9FDFAF echo (string) Color::fromString('hsl(135, 50%, 75%)')->toRGB(); // rgb(159,223,175)
修改颜色
任何注册的颜色类型默认都有以下方法可用
red($value)
green($value)
blue($value)
alpha($value)
hue($value)
saturation($value)
lightness($value)
所有方法返回原始对象的修改副本,保留原始对象未修改,并接受以下参数格式
$newHsl = $hsl->red(50); // set to specific value $newHsl = $hsl->red('+50'); // add 50 to the current value $newHsl = $hsl->red('-50'); // remove 50 from the current value $newHsl = $hsl->red('+50%'); // increase the current value by 50% $newHsl = $hsl->red('-50%'); // decrease the current value by 50% $newHsl = $hsl->red('+1/2'); // set to a value halfway between the current value and 255 (maximum red value) $newHsl = $hsl->red('-1/2'); // set to a value halfway between the current value and 0 (minimum red value)
红色、绿色、蓝色值限制在0..255的范围内。这意味着将100添加到红色值为200的颜色,将返回红色设置为255的颜色。
类似地,饱和度和亮度限制在0..100的范围内,而alpha限制在0和1之间。
色调不受限制,因为可以朝任何方向旋转超过360度(420°相当于60°)。因此,不应使用分数模式('+1/2'),因为没有最小值或最大值。
扩展
自定义颜色类型
该包允许您创建自己的颜色类型。这些必须扩展Color
,并实现从ColorInterface
的以下方法
public static function fromString(string $colorSpec): ColorInterface; public function __toString(): string; public static function fromRGB(Rgb $rgb): ColorInterface; public function toRGB(): Rgb;
注册颜色类型
您需要注册该类,以便允许将新颜色转换为和从新颜色
Color::registerColor('Gray', Gray::class); echo (string) Color::fromString('#163248')->toGray(); // #323232
您可以用新类型替换现有类型。
您可以通过Color::registeredColors()
获取注册的颜色类型列表。
public static function fromString(string $colorSpec): ColorInterface
echo (string) Color::fromString('#204060')->toGray(); // gray(25%) echo (string) Color::fromString('gray(50%, 0.25)')->toRGB(); // #80808040
fromString
接受一个颜色规范并对其与注册的模式进行测试,提取信息。
/** * color spec pattern: 'gray\((\d{1,3}(\.\d{1})?)%\)' * color spec pattern: 'gray\((\d{1,3}(\.\d{1})?)%,([0-1](\.\d{1,2})?)\)' * * If the color spec matches either of the patterns, extractChannels will return the parameters from the string using regex. */ public static function fromString(string $colorSpec): ColorInterface { $channels = Color::extractChannels($colorSpec, self::class); if (!isset($channels[3])) { $channels[3] = 1.0; } return new Gray((float) $channels[1], $channels[3]); }
注册颜色规范模式
要注册颜色规范模式,您必须首先注册它将应用到的颜色类型
Color::registerColor('Gray', Gray::class); Color::registerColorSpec('gray\((\d{1,3})\)', Gray::class);
您可以为任何注册的颜色类型注册新规范,包括默认类型。您还可以覆盖注册模式,以便它们适用于不同的颜色类型。
您可以通过Color::registeredColorSpecs()
获取注册的颜色规范列表。
public function __toString(): string;
public function __toString(): string { $value = round($this->value, 1); $alpha = round($this->alpha, 2); if ($alpha === 1.0) { return "gray({$value},{$alpha})"; } return "gray({$value})"; }
public static function fromRGB(Rgb $rgb): ColorInterface
此方法用于允许将一种颜色类型转换为任何已注册的其他颜色类型。这意味着您只需指定如何将RGB颜色转换为您的类型。
public static function fromRGB(Rgb $rgb): ColorInterface { $average = ($rgb->red + $rgb->green + $rgb->blue) / 3; $gray = $average * 100 / 255; return new Gray($gray, $rgb->alpha); }
public function toRGB(): Rgb
抽象的\SavvyWombat\Color\Color
已实现了此方法
public function toRGB(): Rgb { return new Rgb($this->red, $this->green, $this->blue, $this->alpha); }
红色、绿色、蓝色和alpha属性也定义在抽象中。您可以选择重新实现该方法,或在颜色类型构造函数中设置这些值以使用默认实现
use \SavvyWombat\Color\Color; class Gray extends Color { // gray value in range 0..100 protected $value; public function __construct(float $value, float $alpha = 1.0) { $this->value = $value; $this->alpha = $alpha; // calculate RGB equivalent values for easy conversion $this->red = $value * 2.55; // convert to 0..255 for RGB $this->green = $value * 2.55; $this->blue = $value * 2.55; } }
建议您使用默认实现,因为红色、绿色和蓝色属性已在公共接口中公开。
自定义颜色修饰符
颜色修饰符是颜色类型上的方法,这些方法被注册并可供所有其他颜色类型使用。
调用颜色类型上的修饰符将将其转换为实现注册方法的数据类型,然后将其转换回原始类型。为了帮助这种隐式转换,自定义修饰符必须返回一个扩展 Color
的对象——理想情况下,它应该是实现该方法的数据类型的副本。
public function gray($gray): self { $gray = $this->adjustValue($this->gray, $gray, 100); if ($gray < 0) { $gray = 0; } if ($gray > 100) { $gray = 100; } return new self($gray, $this->alpha); }
Color::adjustValue($originalValue, $newValue, $max = 0, $min = 0)
处理各种相对参数(±50 或 ±50%)以生成正在修改的属性的新计算值。
修饰符还负责确保新的值对于正在修改的属性是有效的。
对颜色关键字的支持
Color::fromString()
和 Hex::fromString()
可以接受 CSS 颜色关键字以创建十六进制颜色。
如果您希望从一个关键字生成不同类型的颜色,您可以在创建后将其从十六进制颜色转换。
$hsl = Color::fromString('deepskyblue')->toHsl();
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件。