matthieumastadenis / couleur
一个现代的PHP 8.1+颜色库,与CSS颜色模块第4级兼容
Requires
- php: ~8.1
Requires (Dev)
- php: ~8.1
- phpunit/phpunit: ^9.5
README
👋 介绍
Couleur 是一个现代的 PHP 8.1+ 颜色库,旨在与 CSS颜色模块第4级 兼容,并受到 Color.js 的启发,该库由 Lea Verou 和 Chris Lilley 创建。
此包的主要目标是允许在多个、旧的和新的 🌈 颜色空间 之间进行 颜色转换,例如著名的 LCH,它为设计目的提供了许多优点。
Couleur 设计为可以使用 面向对象编程 方法,也可以使用 函数式编程 方法。
- 如果您喜欢 面向对象编程,则可以使用 🏭 不变对象和ColorFactory ;
- 如果您喜欢 函数式编程,则可以直接使用多个 🧰 纯函数 ;
注意:此包目前正在开发中。
当前版本可能包含错误、未测试的代码、未记录的代码、未完成的代码,或者简单的代码,这些代码将发生变化。更具体地说,目前缺少 单元测试,以及一些 颜色空间 以及 距离计算函数 和 色域校正 仍需实现。所有这些都将很快实现。
在此期间,建议不要在生产中使用此包。
⚙️ 安装
使用以下命令将 Couleur 添加到您的项目中,使用 Composer
composer require matthieumastadenis/couleur
别忘了包括Composer提供的 自动加载器
<?php require 'vendor/autoload.php';
🏁 快速入门
以下是一个基于面向对象方法的简单使用 Couleur 的快速示例。有关更详细的说明,请参阅 📚 使用方法 部分。
<?php use matthieumastadenis\couleur\ColorFactory; use matthieumastadenis\couleur\ColorSpace; require 'vendor/autoload.php'; // Create a new colors\Css instance from an HSL array: $css1 = ColorFactory::newCss([ 0, 100, 50 ], ColorSpace::Hsl); echo $css1; // Prints 'red' // Convert to RGB: $rgb1 = $css1->toRgb(); // Stringify: echo $rgb1; // Prints 'rgb(100% 0% 0%)' echo $rgb1->stringify(); // Prints 'rgb(100% 0% 0%)' echo $rgb1->stringify(legacy : false, alpha : true); // Prints 'rgb(100% 0% 0% / 100%)' echo $rgb1->stringify(legacy : true); // Prints 'rgb(255,0,0)' echo $rgb1->stringify(legacy : true, alpha : true); // Prints 'rgba(255,0,0,1)' // Create a variant color: $rgb2 = $rgb1->change('-35', '+20', 60); echo $rgb2->stringify(legacy : true); // Prints 'rgb(220,20,60)'; // Convert to CSS: $css2 = $rgb2->toCss(); echo $css2; // Prints 'crimson' // Convert to Lch: $lch = $css2->toLch(); echo $lch->stringify(alpha : true); // Prints 'lch(47.878646049% 79.619059282 26.464486652deg / 100%)' // Convert to P3: $p3 = $lch->toP3(); echo $p3; // Prints 'color(display-p3 0.791710722 0.191507424 0.257366748)'
📚 使用方法
🏭 不变对象和ColorFactory
直接实例化
颜色为每个支持的🌈 颜色空间提供了一种不可变类。当然,您也可以手动实例化这些类。
<?php use matthieumastadenis\couleur\colors\Rgb; use matthieumastadenis\couleur\colors\Hsl; require 'vendor/autoload.php'; // Create a new colors\Css instance: $rgb = new Css('red'); // Create a new colors\HexRgb instance (with 50% opacity): $hex = new HexRgb('FF', '00', '00', '80'); // Create a new colors\Hsl instance (with 50% opacity): $hsl = new Hsl(0, 100, 50, 50); // Create a new colors\Rgb instance (with 50% opacity): $rgb = new Rgb(255, 0, 0, 127.5);
注意:您可能已经注意到了前一个示例中,这意味着需要向每个构造函数传递正确格式的值。
例如,
Rgb
类期望接收透明度,其大小与红色、绿色和蓝色值相同,即一个介于0到255之间的数字。同样,HexRgb
类期望接收四个参数的十六进制字符串(包括透明度)。因此,您可能更喜欢避免自己实例化这些类。一个更简单的解决方案是使用下面的例子中的ColorFactory。它将自动为您处理值转换。
使用ColorFactory
创建颜色对象的最佳和最简单方法是使用ColorFactory
抽象类,它为每个支持的🌈 颜色空间提供了一个特定的静态方法。
<?php use matthieumastadenis\couleur\ColorFactory; require 'vendor/autoload.php'; // Returns a new colors\Rgb instance (with 50% opacity): $rgb1 = ColorFactory::newRgb('rgba(255,0,0,.5)'); // Returns a new colors\Lab instance: $lab1 = ColorFactory::newLab('lab(54.29%,80.80,69.89,1)'); // Using a string value formatted with modern CSS syntax works as well: $rgb2 = ColorFactory::newRgb('rgb(100% 0% 0% / 50%)'); $lab2 = ColorFactory::newLab('lab(54.29% 80.80 69.89 / 100%)'); // Using an array as a value also works: $rgb3 = ColorFactory::newRgb([ 255, 0, 0, 127.5 ]); $lab3 = ColorFactory::newLab([ 54.29, 80.80, 69.89, 100 ]);
注意:默认情况下,这些方法是自动猜测输入语法的。这意味着可以提供一个与预期输出格式不同的输入值,转换将自动发生。
<?php use matthieumastadenis\couleur\ColorFactory; require 'vendor/autoload.php'; // Returns a new colors\Rgb instance from a CSS input: $rgb = ColorFactory::newRgb('red'); // Returns a new colors\Css instance from a HSL input: $css = ColorFactory::newCss('hsl(0deg,100%,50%)'); // Returns a new colors\XyzD65 instance from a Lab input: $xyzD65 = ColorFactory::newXyzD65('lab(54.29% 80.80 69.89 / 100%)');
如果您使用格式不正确的值,将抛出一个UnknownColorSpace
异常。
<?php use matthieumastadenis\couleur\ColorFactory; require 'vendor/autoload.php'; try { // Throws a UnknownColorSpace Exception: $rgb = ColorFactory::newRgb('not valid'); } catch (\Exception $e) { die($e); // Unknown color space }
此外,如果您使用不完整的值,将抛出一个MissingColorValue
异常。
<?php use matthieumastadenis\couleur\ColorFactory; require 'vendor/autoload.php'; try { // Throws a MissingColorValue Exception: $rgb = ColorFactory::newRgb('rgb(255,0)'); } catch (\Exception $e) { die($e); // Color value "blue" is missing }
通过使用$from
参数,您可以使用字符串别名或使用ColorSpace 枚举指定输入颜色空间。与之前一样,值将自动从它转换到目标空间。
当您使用数组作为输入时,指定这一点特别有用,以确保它不会被当作RGB(这是数字数组的默认值)处理。
<?php use matthieumastadenis\couleur\ColorFactory; use matthieumastadenis\couleur\ColorSpace; require 'vendor/autoload.php'; // Without the $from parameter, array values are considered as RGB values by default: $rgb1 = ColorFactory::newRgb([ 0, 100, 50 ]); \var_dump($rgb1->coordinates()); // [ 0, 100, 50, 255 ] // With the $from parameter, we can ensure that the input value will be treated like we want // The following line creates a new colors\Rgb instance from an HSL input: $rgb2 = ColorFactory::newRgb([ 0, 100, 50 ], 'hsl'); \var_dump($rgb2->coordinates()); // [ 255, 0, 0, 255 ] // Same result, but with usage of the ColorSpace enum: $rgb3 = ColorFactory::newRgb([ 0, 100, 50 ], ColorSpace::Hsl); \var_dump($rgb3->coordinates()); // [ 255, 0, 0, 255 ]
您还可以使用new()
静态方法,该方法在输入值之后添加了一个$to
参数。如果没有指定此参数,将根据值的格式自动确定目标颜色空间。
<?php use matthieumastadenis\couleur\ColorFactory; use matthieumastadenis\couleur\ColorSpace; require 'vendor/autoload.php'; // Returns a new colors\Rgb instance (space guessed automatically): $rgb = ColorFactory::new('rgb(255,0,0)'); // Returns a new colors\HexRgb instance (space guessed automatically): $hex = ColorFactory::new('#ff0000'); // Returns a new colors\Css instance (space guessed automatically): $css = ColorFactory::new('red'); // Returns a new colors\Css instance from an RGB value: $css = ColorFactory::new('rgb(255,0,0)', 'css'); // Same result but using the ColorSpace enum: $css = ColorFactory::new('rgb(255,0,0)', ColorSpace::Css); // Returns a new colors\Lch instance (using the ColorSpace enum): $lch = ColorFactory::new([ 54.29, 106.84, 40.86 ], ColorSpace::Lch); // Returns a new colors\OkLab instance from an RGB value (using the ColorSpace enum): $okLab = ColorFactory::new([ 255, 0, 0 ], ColorSpace::OkLab, ColorSpace::Rgb);
使用不可变颜色对象
一旦您有一个颜色实例,您就可以使用其专用的to...()
方法之一轻松将其转换为另一个颜色空间,这将返回一个新的对象。
<?php use matthieumastadenis\couleur\ColorFactory; use matthieumastadenis\couleur\ColorSpace; require 'vendor/autoload.php'; $rgb = ColorFactory::newRgb([ 255, 0, 0 ]); // Converting to a new colors\Css instance: $css = $rgb->toCss(); // Converting to a new colors\XyzD50 instance: $xyzD50 = $css->toXyzD50(); // Converting to a new colors\OkLch instance (using the to() method): $okLch = $xyzD50->to(ColorSpace::OkLch);
注意:任何颜色都可以使用toCss()
方法转换为CSS。它将自动选择最接近的CSS颜色。
<?php use matthieumastadenis\couleur\ColorFactory; require 'vendor/autoload.php'; $rgb = ColorFactory::newRgb([ 250, 10, 10 ]); $css = $rgb->toCss(); // Prints 'red': echo $css;
所有颜色对象都是直接可转换为字符串的。它们还提供了一个stringify()
方法,它提供了更多的可能性。
<?php use matthieumastadenis\couleur\ColorFactory; use matthieumastadenis\couleur\ColorSpace; require 'vendor/autoload.php'; $rgb = ColorFactory::newRgb([ 255, 0, 0 ]); // Prints 'rgb(100% 0% 0%)': echo $rgb; // Prints 'rgb(100% 0% 0% / 100%)' ($alpha parameter): echo $rgb->stringify(null, true); // Prints 'rgba(255,0,0,1)' (using $legacy and $alpha parameters): echo $rgb->stringify(true, true); $lch = ColorFactory::newLch([ 54.2905429, 106.837191, 40.8576688 ], ColorSpace::Lch); // Prints 'lch(54.29% 106.84 40.86deg)' Using the $precision parameter: echo $lch->stringify(precision : 2);
所有颜色对象还有一个coordinates()
方法,它返回一个数组。
<?php use matthieumastadenis\couleur\ColorFactory; require 'vendor/autoload.php'; $hex = ColorFactory::newHexRgb('#F00'); // Returns [ 'FF', '00', '00', 'FF' ]: $values = $hex->coordinates();
您还可以直接访问每个颜色对象的只读属性。
<?php use matthieumastadenis\couleur\ColorFactory; require 'vendor/autoload.php'; $rgb = ColorFactory::newRgb([ 255, 0, 0 ]); // Prints 255: echo $rgb->red; $hsl = ColorFactory::newHsl([ 0, 100, 50 ]); // Prints 50: echo $hsl->lightness;
所有颜色对象都有一个change()
方法,它始终返回一个与当前颜色相对应的新对象的变体。
<?php use matthieumastadenis\couleur\ColorFactory; use matthieumastadenis\couleur\ColorSpace; require 'vendor/autoload.php'; $hsl1 = ColorFactory::newHsl([ 0, 100, 50, 100 ], ColorSpace::Hsl); echo $hsl1; // hsl(0deg 100% 50% / 100%) // Redefining coordinates: $hsl2 = $hsl1->change(hue : 180, opacity : 80); echo $hsl2; // hsl(180deg 100% 50% / 80%) // Add, subtract, multiply, divide coordinates: $hsl3 = $hsl2->change('+20', '-10', '*1.5', '/2'); echo $hsl3; // hsl(200deg 90% 75% / 40%) // Reduce coordinates by modulus: $hsl4 = $hsl3->change(opacity : '%6'); echo $hsl4; // hsl(200deg 90% 75% / 4%) // Calculate the percentage of coordinates: $hsl5 = $hsl4->change('50%'); echo $hsl5; // hsl(100deg 90% 75% / 4%) // Add, subtract, multiply, divide coordinates by a percentage: $hsl6 = $hsl5->change('+50%', '-50%', '/10%', '*200%'); echo $hsl6; // hsl(150deg 45% 10% / 32%) // Reduce coordinates by a percentage modulus: $hsl7 = $hsl6->change(saturation : '%20%'); echo $hsl7; // hsl(150deg 0% 10% / 32%)
注意:根据您要求的操作,
change()
方法的行为可能不同,特别是对于HexRgb
类。
- 对于替换坐标,您必须提供一个十六进制值;
- 对于加法和减法,您必须提供一个十六进制值;
- 对于所有其他操作,您必须提供一个十进制值;
请参考下一个示例中的详细演示。
<?php use matthieumastadenis\couleur\ColorFactory; use matthieumastadenis\couleur\ColorSpace; require 'vendor/autoload.php'; $hex1 = ColorFactory::newHexRgb('#F00'); echo $hex1; // #F00 // When replacing coordinates, provide hexadecimal numbers: $hex2 = $hex1->change('80', 'AA', 'BB', 'AA'); echo $hex2; // #80AABBAA // When adding or subtracting coordinates, provide hexadecimal numbers: $hex3 = $hex2->change('+8', '-11'); echo $hex3; // #89BA (88 99 BB AA) // When multiplying, dividing or reducing coordinates by modulo, provide decimal numbers: $hex4 = $hex3->change(null, '*1.5', '/2', '%3'); echo $hex4; // #88E65E02 (88 dechex(153*1.5) dechex(187/2) dechex(170%3)) // When using percentages, provide decimal numbers: $hex5 = $hex4->change('20%'); echo $hex5; // #1BE65E02 (dechex(136*20/100) E6 5E 02) // When using percentages with addition, substraction, multiplication or division, provide decimal numbers: $hex6 = $hex5->change('+50%', '-20%', '/2%', '*200%'); echo $hex6; // #29B83208 (dechex(27+(27*50/100)) dechex(230-(230*20/100)) dechex(94/(84*2/100)) dechex(2*(2*200/100))) // When using percentages with modulo, provide decimal numbers: $hex7 = $hex6->change(green : '%4%'); echo $hex7; // #29023208 (29 dechex(184%(184*4/100)) 32 08)
注意:对于
Css
类的change()
方法,其行为也不同:它只接受可转换为字符串的颜色名称或CssColor 枚举的实例,这将替换颜色而不进行变体。请参考下一个示例。
<?php use matthieumastadenis\couleur\ColorFactory; use matthieumastadenis\couleur\CssColor; require 'vendor/autoload.php'; $css1 = ColorFactory::newCss(CssColor::red); echo $css1; // red $css2 = $css1->change(CssColor::purple); echo $css2; // purple $css2 = $css1->change(CssColor::purple); echo $css2; // purple $css3 = $css2->change('hotpink'); echo $css3; // hotpink // Throws an UnsupportedCssColor Exception: $css4 = $css3->change('invalid');
🧰 纯函数
在 Couleur 中的对象都是在底层基于一组 纯函数 构建的。如果您不想使用对象,可以直接使用这些函数。
注意:选择这种 函数式编程方法 在性能方面更优,但可能稍微繁琐一些,因为您需要操作值数组而不是对象。
Couleur 提供了三种主要的函数类型:专门的 颜色空间函数,专门的 转换函数,以及 通用函数
颜色空间函数
每个支持的 🌈 颜色空间 都有自己的专用函数,可以在 matthieumastadenis\couleur\utils\[space]
命名空间下访问。这些函数对于每个颜色空间都是相同的:clean()
、from()
、stringify()
和 verify()
。
clean()
函数用于将输入值转换为正确格式的值集合,根据相应的颜色空间。除了 css\clean()
直接返回 CssColor
枚举 的实例外,所有这些函数都返回一个数组。
<?php use matthieumastadenis\couleur\utils\css; use matthieumastadenis\couleur\utils\rgb; use matthieumastadenis\couleur\utils\lch; require 'vendor/autoload.php'; // All of the following return ColorSpace::red: $css1 = css\clean('red'); $css2 = css\clean(' red '); $css3 = css\clean('RED'); // All of the following return [ 255, 0, 0, 255 ]: $rgb1 = rgb\clean([ '100%', '0%', '0%', '100%' ]); $rgb2 = rgb\clean([ 255, 0, 0, '100%' ]); $rgb3 = rgb\clean('rgb(255,0,0)'); $rgb4 = rgb\clean('rgba(255,0,0,1)'); $rgb5 = rgb\clean('rgb(100% 0 0 / 100%)'); $rgb6 = rgb\clean('color(rgb 100% 0 0 / 100%)'); // All of the following return [ 54.2905429, 106.837191, 40.8576688, 100 ]: $lch1 = lch\clean([ 54.2905429, 106.837191, 40.8576688 ]); $lch2 = lch\clean([ '54.2905429%', '106.837191', '40.8576688deg' ]); $lch3 = lch\clean('lch(54.2905429%,106.837191,40.8576688deg)'); $lch4 = lch\clean('lch(54.2905429% 106.837191 40.8576688deg / 100%)'); $lch5 = lch\clean('color(lch 54.2905429% 106.837191 40.8576688deg)'); $lch6 = lch\clean('color(lch 54.2905429% 106.837191 40.8576688deg / 100%)');
from()
函数将指定颜色空间(使用 $from
参数)的输入值转换为相应命名空间的颜色空间(转换和清理)。如果没有指定 输入颜色空间,则将自动从 $value
的格式中猜测。
<?php use matthieumastadenis\couleur\utils\rgb; use matthieumastadenis\couleur\utils\lch; use matthieumastadenis\couleur\ColorSpace; require 'vendor/autoload.php'; // All of the following convert and clean from CSS to RGB, // returning [ 255, 0, 0, 255 ]: $rgb1 = rgb\from('red'); $rgb2 = rgb\from('red', ColorSpace::Css); // All of the following convert and clean from RGB to HSL, // returning [ 0, 100, 50, 100 ]: $hsl1 = hsl\from([ 255, 0, 0, 255 ]); $hsl2 = hsl\from('rgb(100% 0% 0% / 100%)'); $hsl3 = hsl\from('rgba(255,0,0,1)', 'rgb'); $hsl4 = hsl\from('rgba(255,0,0,1)', ColorSpace::Rgb);
stringify()
函数返回一个完全兼容 CSS 语法 颜色字符串。根据每个颜色空间,这些函数可以有以下参数
$sharp
:仅用于HexRgb
颜色,此参数可用于包含或不包含十六进制尖号字符 (#);$short
:仅用于HexRgb
颜色,此参数可用于强制或防止值的缩短(例如,使用 #f00 而不是 #ff0000);$uppercase
:仅用于HexRgb
颜色,此参数可用于强制转换为大写或小写(默认情况下保留大小写);$alpha
:可用于强制或防止包含不透明度(默认情况下,只有当不透明度值不是 100% 时才包含不透明度);$precision
:用于四舍五入值的十进制位数(默认使用COULEUR_PRECISION
常量);$legacy
:如果为 true,则颜色字符串将按照传统的 CSS 语法格式化,而不是现代的语法(例如,rgba(255,0,0,1) 而不是 rgb(100% 0% 0% / 100%));
<?php use matthieumastadenis\couleur\utils\hexRgb; use matthieumastadenis\couleur\utils\rgb; use matthieumastadenis\couleur\utils\xyzD65; require 'vendor/autoload.php'; // Prints '#F00': echo hexRgb\stringify('FF', '00', '00'); // Prints 'FF0' (using the $sharp parameter): echo hexRgb\stringify('FF', '00', '00', sharp : false); // Prints '#F00' (using array destructuring on clean() result): echo hexRgb\stringify(... hexRgb\clean('#FF0000')); // Prints '#FF0000' (using $short parameter): echo hexRgb\stringify('FF', '00', '00', short : false); // Prints '#F00F' (using $alpha parameter): echo hexRgb\stringify('FF', '00', '00', alpha : true); // Prints '#FF0000FF' (using $alpha and $short parameters): echo hexRgb\stringify('FF', '00', '00', alpha : true, short : false); // Prints 'rgb(100% 0% 0%)': echo rgb\stringify(255, 0, 0); // Prints 'rgb(100% 0% 0%)' (using array destructuring on clean() result): echo rgb\stringify(... rgb\clean('rgb(255,0,0,1)')); // Prints 'rgb(100% 0% 0% / 100%)' (using $alpha parameter): echo rgb\stringify(255, 0, 0, alpha : true); // Prints 'rgb(255,0,0)' (using $legacy parameter): echo rgb\stringify(255, 0, 0, legacy : true); // Prints 'rgba(255,0,0,1)' (using $legacy and $alpha parameters): echo rgb\stringify(255, 0, 0, legacy : true, alpha : true); // Prints 'color(xyz-d65 0.412390799 0.212639006 0.019330819)': echo xyzD65\stringify(0.412390799, 0.212639006, 0.019330819); // Prints 'color(xyz-d65 0.412390799 0.212639006 0.019330819 / 100%)' (using $alpha parameter): echo xyzD65\stringify(0.412390799, 0.212639006, 0.019330819, alpha : true);
verify()
函数简单地返回一个 布尔值,指示输入值是否与相应的 颜色空间 匹配
<?php use matthieumastadenis\couleur\utils\css; use matthieumastadenis\couleur\utils\hexRgb; use matthieumastadenis\couleur\utils\hsl; require 'vendor/autoload.php'; // Returns true: \var_dump(css\verify('red')); // Returns false: \var_dump(css\verify('invalid')); // All of the following return true: \var_dump(hexRgb\verify('f00')); \var_dump(hexRgb\verify('f00f')); \var_dump(hexRgb\verify('ff0000')); \var_dump(hexRgb\verify('ff0000ff')); \var_dump(hexRgb\verify('#f00')); \var_dump(hexRgb\verify('#f00f')); \var_dump(hexRgb\verify('#ff0000')); \var_dump(hexRgb\verify('#ff0000ff')); // Returns false: \var_dump(hexRgb\verify('invalid')); // The following also return false, because they eventually could be mistaken for RGB values: \var_dump(hexRgb\verify([ 'ff', '00', '00' ])); \var_dump(hexRgb\verify([ 'ff', '00', '00', 'ff' ])); // All of the following return true: \var_dump(hsl\verify('hsl(0,100,50)')); \var_dump(hsl\verify('hsl(0deg,100%,50%)')); \var_dump(hsl\verify('hsla(0,100,50,1)')); \var_dump(hsl\verify('hsla(0deg,100%,50%,1)')); \var_dump(hsl\verify('color(hsl,0,100,50,1)')); \var_dump(hsl\verify('color(hsl,0deg,100%,50%,1)')); \var_dump(hsl\verify('color(hsl 0 100 50 / 1)')); \var_dump(hsl\verify('color(hsl 0deg 100% 50% / 1)')); // All of the following return false: \var_dump(hsl\verify('0,100,50')); \var_dump(hsl\verify('hsl 0,100')); // The following also return false, because they eventually could be mistaken for RGB values: \var_dump(hexRgb\verify([ 0, 100, 50 ])); \var_dump(hexRgb\verify([ '0deg', '100%', '50%' ]));
转换函数
每个支持的 🌈 颜色空间 都有一套完整的专用函数,用于将颜色转换为其他 颜色空间。这些函数也可以在 matthieumastadenis\couleur\utils\[space]
命名空间下访问
<?php use matthieumastadenis\couleur\utils\css; use matthieumastadenis\couleur\utils\rgb; use matthieumastadenis\couleur\CssColor; require 'vendor/autoload.php'; // Returns [ 255, 0, 0, 255 ]: $rgb = css\toRgb(CssColor::red); // Returns [ 0, 100, 50, 100 ]: $hsl = rgb\toHsl(... $rgb); // Returns [ 0.43606574282481, 0.22249319175624, 0.013923904500943, 1 ] $xyzD50 = hsl\toXyzD50(... $hsl);
通用函数
Couleur 还提供了一套 通用实用函数,所有这些函数都位于 matthieumastadenis\couleur\utils
命名空间下。
虽然这些函数中的大多数都是为内部使用而设计的,但如果您更喜欢使用 Couleur 与 函数式编程方法 一起使用,其中一些可能对您很有用。以下是对这些函数的描述。
constant()
函数可用于直接访问和声明 配置常量,无需使用 Constant
枚举。
<?php use matthieumastadenis\couleur\utils; require 'vendor/autoload.php'; // Returns null: \var_dump(utils\constant('unknown')); // Returns 7: \var_dump(utils\constant('precision', 7)); // Creates the constant with a value of 3, then returns 3: \var_dump(utils\constant('precision', 3, true)); // Now that the constant was created, always returns 3: \var_dump(utils\constant('precision'));
findColorSpace()
函数可以帮助您通过解释提供的 $value
来猜测一个 🌈 颜色空间。
如果函数成功,它将返回一个 颜色空间枚举 的实例。
在失败的情况下,默认情况下函数会抛出一个 UnknownColorSpace
,除非您将 $throw
参数设置为 false
或者提供了 $fallback
值。
<?php use matthieumastadenis\couleur\utils; use matthieumastadenis\couleur\ColorSpace; require 'vendor/autoload.php'; // Returns ColorSpace::Css: $space = utils\findColorSpace('red'); // Returns ColorSpace::Rgb: $space = utils\findColorSpace('rgba(255,0,0,1)'); // Also returns ColorSpace::Rgb: $space = utils\findColorSpace([ 255, 0, 0, 255 ]); // Throws a UnknownColorSpace Exception: $space = utils\findColorSpace('invalid'); // Returns ColorSpace::Rgb (using the $fallback parameter): $space = utils\findColorSpace('invalid', ColorSpace::Rgb); // Returns null (using the $throw parameter): $space = utils\findColorSpace('invalid', throw : false);
函数 isColorString()
返回一个 布尔值,指示提供的 $value
是否为有效的 CSS 颜色字符串。
默认情况下它非常宽容,对于任何与有效 CSS 语法对应的字符串,无论您如何编写函数名称(例如 'myCustomRgb(255,0,0)' 将被视为有效)都将返回 true
。
如果您想进行更精确的检查,可以使用 $spaces
参数提供以下之一:
- 一个唯一的可字符串化的值,如
'rgb'
; - 一个接受值的数组,如
[ 'rgb', 'rgba' ]
; - 一个 颜色空间枚举 的实例(接受所有别名) ;
<?php use matthieumastadenis\couleur\utils; use matthieumastadenis\couleur\ColorSpace; require 'vendor/autoload.php'; // All of the following return true: \var_dump(utils\isColorString('myCustomColor(255,0,0,1)')); \var_dump(utils\isColorString('rgb(100% 0% 0% / 100%)', 'rgb')); \var_dump(utils\isColorString('rgba(255,0,0,1)', [ 'rgb', 'rgba' ])); \var_dump(utils\isColorString('color(srgb 100% 0% 0% / 100%)', ColorSpace::Rgb)); // All of the following return false: \var_dump(utils\isColorString('invalid')); \var_dump(utils\isColorString('rgb 100%')); \var_dump(utils\isColorString('255,0,0')); \var_dump(utils\isColorString('rgba(255,0,0,1)', 'rgb')); \var_dump(utils\isColorString('srgb(255,0,0,1)', [ 'rgb', 'rgba' ])); \var_dump(utils\isColorString('myCustomRgb( 100% 0% 0% / 100%)', ColorSpace::Rgb)); \var_dump(utils\isColorString('color(myCustomRgb 100% 0% 0% / 100%)', ColorSpace::Rgb));
函数 parseColorValue()
将 CSS 颜色字符串 转换为一个值数组。如果提供的 $value
不可字符串化,它将简单地作为一个数组返回。
参数 $opacityFactor
有助于将不透明度转换为正确的范围(例如将 1 转换为 100 或 255)。
注意:此函数不会 清理 数组中的值。对于典型用法,您可能希望将结果传递给相应的
clean()
函数(有关更多详细信息,请参阅颜色空间函数部分)。
<?php use matthieumastadenis\couleur\utils; use matthieumastadenis\couleur\CssColor; require 'vendor/autoload.php'; // Returns [ 255, 0, 0 ]: $values = utils\parseColorValue('rgb(255,0,0)'); // Returns [ 255, 0, 0, 1 ]: $values = utils\parseColorValue('rgb(255,0,0,1)'); // Returns [ 255, 0, 0, 255 ] (using the $opacityFactor parameter): $values = utils\parseColorValue('rgb(255,0,0,1)', 255); // Returns [ '100%', '0%', '0%', '100%' ]: $values = utils\parseColorValue('rgb(100% 0% 0% / 100%)'); // Returns [ CssColor::red ] $values = utils\parseColorValue(CssColor::red); // Returns [ 255, 0, 0, 255 ]: $values = utils\parseColorValue([ 255, 0, 0, 255 ]);
函数 to()
是用于将任何颜色值转换为任何颜色空间的最顶级函数。
在成功的情况下,其结果始终是数组。
其 $to
和 $from
参数分别对应输出和输入颜色空间,并接受 颜色空间枚举 的实例或与有效颜色空间别名对应的可字符串化值(您可以在下面的🌈 颜色空间部分找到所有有效别名)。
如果这些参数为空,它们将通过解释 $value
的格式(使用 findColorSpace()
函数)来猜测。
<?php use matthieumastadenis\couleur\utils; use matthieumastadenis\couleur\ColorSpace; require 'vendor/autoload.php'; // Returns [ CssColor::red ]: // ('red' is a valid CSS color so we can omit the $from parameter): $css = utils\to('red', 'css'); // Returns [ 255, 0, 0, 255 ]: // ([ CssColor::red ] is a valid CSS color so we can omit the $from parameter): $rgb = utils\to($css, ColorSpace::Rgb); // Returns [ 0, 100, 50, 100 ]: // ([ 255, 0, 0, 255 ] is a valid RGB color so we can omit the $from parameter): $hsl = utils\to($rgb, ColorSpace::Hsl); // Returns [ 54.29054294697, 80.804920334624, 69.890988258963, 100 ] // (the $from parameter avoids HSL array being interpreted as RGB): $lab = utils\to($hsl, ColorSpace::Lab, ColorSpace::Hsl); // Returns [ 54.29054294697, 106.83719104366, 40.857668782131, 100 ] // (the $from parameter avoids Lab array being interpreted as RGB): $lch = utils\to($lab, ColorSpace::Lch, ColorSpace::Lab); // Returns [ 0.41239079028139, 0.21263903420017, 0.01933077971095, 100 ] // (the $from parameter avoids Lch array being interpreted as RGB): $xyzD65 = utils\to($lch, ColorSpace::XyzD65, ColorSpace::Lch); // Returns [ 0.70226883304033, 0.27562276714962, 0.10344904551878, 1 ] // (here we use a valid string input so we can omit the $from paramter): $proPhoto = utils\to('color(xyz-d65 0.4124 0.2126 0.0193 / 100%)', ColorSpace::ProPhoto);
🛠️ 枚举和常量
Constant枚举
颜色 可以使用专用 常量 进行预配置。这些作为默认值,当相应参数缺失或设置为空时,由多个函数使用。所有常量都写成大写并带有前缀 COULEUR_
。
目前使用的常量如下
COULEUR_LEGACY
(默认0
):如果设置为1
,则默认使用 遗留 CSS 语法;COULEUR_PRECISION
(默认9
):字符串化颜色值时的默认舍入精度;
您可以使用 Constant
枚举轻松访问和管理这些常量和它们的值
<?php use matthieumastadenis\couleur\Constant; require 'vendor/autoload.php'; // Returns all Constant cases: Constant::cases(); // Always returns 0, which is the default value for the COULEUR_LEGACY constant: Constant::LEGACY->value; // Always returns 9, which is the default value for the COULEUR_PRECISION constant: Constant::PRECISION->value; // Returns the value of the COULEUR_LEGACY constant if defined, or 0 by default: Constant::LEGACY->value(); // Returns the value of the COULEUR_PRECISION constant if defined, or 9 by default: Constant::PRECISION->value(); // Returns the value of the COULEUR_LEGACY constant if defined, or 1 as fallback: Constant::LEGACY->value(1); // Returns the value of the COULEUR_PRECISION constant if defined, or 3 as fallback: Constant::PRECISION->value(3); // Returns the value of the COULEUR_LEGACY constant if defined, or define it with 1 as value then returns 1: Constant::LEGACY->value(1, true); // Returns the value of the COULEUR_PRECISION constant if defined, or define it with 3 as value then returns 3: Constant::PRECISION->value(3, true);
ColorSpace枚举
此枚举是访问 Couleur 支持的所有 颜色空间 的最简单方法
<?php use matthieumastadenis\couleur\ColorSpace; require 'vendor/autoload.php'; // Returns all ColorSpace cases: ColorSpace::cases();
您可以通过相应的颜色 class
访问 ColorSpace
实例
<?php use matthieumastadenis\couleur\ColorSpace; use matthieumastadenis\couleur\colors\Lch; use matthieumastadenis\couleur\colors\LinP3; use matthieumastadenis\couleur\colors\Rgb; require 'vendor/autoload.php'; // Returns ColorSpace::Lch: ColorSpace::from(Lch::class); // Returns ColorSpace::LinP3: ColorSpace::from(LinP3::class); // Returns ColorSpace::Rgb: ColorSpace::from(Rgb::class);
每个 ColorSpace
都可以通过 fromAlias()
方法以多个 别名 访问。所有别名都不区分大小写
<?php use matthieumastadenis\couleur\ColorSpace; require 'vendor/autoload.php'; // Returns all ColorSpace aliases: ColorSpace::allAliases(); // Returns aliases of the HexRgb space: ColorSpace::HexRgb->aliases(); // Returns ColorSpace::Rgb: ColorSpace::fromAlias('rgb'); ColorSpace::fromAlias('srgb'); ColorSpace::fromAlias('RGBA'); // Returns ColorSpace::Lab: ColorSpace::fromAlias('lab'); ColorSpace::fromAlias('cielab'); ColorSpace::fromAlias('CIE-LAB');
您可以通过每个 ColorSpace
的 cleanCallback()
、fromCallback()
、stringifyCallback()
和 verifyCallback()
方法轻松访问 专用函数
<?php use matthieumastadenis\couleur\ColorSpace; use matthieumastadenis\couleur\CssColor; require 'vendor/autoload.php'; // Returns 'matthieumastadenis\couleur\utils\xyzD50\clean': ColorSpace::XyzD50->cleanCallback(); // Returns [ 255, 127.5, 0, 255 ]: ColorSpace::Rgb->cleanCallback()('rgb(100%,50%,0)'); // Returns [ 'FF', '00', '00', 'FF' ]: ColorSpace::HexRgb->cleanCallback()('#f00'); // Returns 'matthieumastadenis\couleur\utils\xyzD50\from': ColorSpace::XyzD50->fromCallback(); // Returns [ 255, 0, 0, 255 ]: ColorSpace::Rgb->fromCallback()('hsl(0deg,100%,50%)'); // Returns CssColor::red: ColorSpace::Css->fromCallback()('#f00'); // Returns 'matthieumastadenis\couleur\utils\xyzD50\stringify': ColorSpace::XyzD50->stringifyCallback(); // Returns 'rgb(100% 0% 0% / 50%): ColorSpace::Rgb->stringifyCallback()(255, 0 , 0, 127.5); // Returns '#F00': ColorSpace::Css->stringifyCallback()(CssColor::red); // Returns 'matthieumastadenis\couleur\utils\css\verify': ColorSpace::Css->verifyCallback(); // Returns true: ColorSpace::Rgb->verifyCallback()('rgb(100%,50%,0)'); // Returns false: ColorSpace::HexRgb->verifyCallback()('#ff');
CssColor枚举
此枚举有助于管理 CSS
的 命名颜色。通过其多个方法,您可以轻松地知道是否存在 CssColor
以及从其中获取相应的 RGB
或 HexRGB
坐标
<?php use matthieumastadenis\couleur\CssColor; require 'vendor/autoload.php'; // Returns all CssColor cases: CssColor::cases(); // Returns RGB coordinates for all supported CSS colors: CssColor::allRgbCoordinates(); // Returns HexRgb coordinates for all supported CSS colors: CssColor::allHexRgbCoordinates(); // Returns true (the 'red' color exists): CssColor::exists('red'); // Returns [ 255, 0, 0 ]: CssColor::red->toRgbCoordinates(); // Returns [ 'FF', '00', '00' ]: CssColor::red->toHexRgbCoordinates();
fromCss()
方法允许您通过名称获取特定的 CssColor
。如果没有支持的色彩与 $name
参数匹配,默认情况下将抛出 UnsupportedCssColor
异常,除非您提供了 $fallback
或将 $throw
参数设置为 false。
<?php use matthieumastadenis\couleur\CssColor; require 'vendor/autoload.php'; // Returns CssColor::red: CssColor::fromCss('red'); // Throws a UnsupportedCssColor Exception: CssColor::fromCss('unknown'); // Returns CssColor::pink Exception: CssColor::fromCss('unknown', CssColor::pink); // Returns null: CssColor::fromCss('unknown', null, false);
您还可以使用 fromRgb()
和 fromHexRgb()
查找与精确的 RGB
或 HexRGB
坐标对应的 CssColor
。
默认情况下,这些函数将返回最接近提供的坐标的支持 CSS 色彩,除非您将 $closest
参数设置为 false。在这种情况下,如果没有支持的色彩与您提供的精确坐标匹配,默认情况下将抛出 UnsupportedCssColor
异常,除非您提供了 $fallback
或将 $throw
参数设置为 false。
<?php use matthieumastadenis\couleur\CssColor; require 'vendor/autoload.php'; // Returns CssColor::red which is the closest to #FA1111: CssColor::fromHexRgb('FA', '11', '11'); // Throws a UnsupportedCssColor Exception: CssColor::fromHexRgb('FA', '11', '11', false); // Returns CssColor::pink: CssColor::fromHexRgb('FA', '11', '11', false, CssColor::pink); // Returns null: CssColor::fromHexRgb('FA', '11', '11', false, null, false); // Returns CssColor::red which is the closest to rgb(250,10,10): CssColor::fromRgb(250, 10, 10); // Throws a UnsupportedCssColor Exception: CssColor::fromRgb(250, 10, 10, false); // Returns CssColor::pink: CssColor::fromRgb(250, 10, 10, false, CssColor::pink); // Returns null: CssColor::fromRgb(250, 10, 10, false, null, false);
您可以使用 toHexRgbString()
和 toRgbString
方法将 CssColor
转换为字符串。
<?php use matthieumastadenis\couleur\CssColor; require 'vendor/autoload.php'; // Returns '#F00': CssColor::red->toHexRgbString(); // Returns '#f00' (using the $uppercase parameter): CssColor::red->toHexRgbString(uppercase : false); // Returns 'F00' (using the $sharp parameter): CssColor::red->toHexRgbString(sharp : false); // Returns '#F00F' (using the $alpha parameter): CssColor::red->toHexRgbString(true); // Returns '#FF0000' (using the $short parameter): CssColor::red->toHexRgbString(short : false); // Returns '#FF0000FF' (using $alpha and $short parameters): CssColor::red->toHexRgbString(true, false); // Returns '#ff0000ff' (using $alpha, $short and $uppercase parameters): CssColor::red->toHexRgbString(true, false, false); // Returns 'ff0000ff' (using $alpha, $short, $uppercase and $sharp parameters): CssColor::red->toHexRgbString(true, false, false, false); // Returns 'rgb(100% 0% 0%)': CssColor::red->toRgbString(); // Returns 'rgb(100% 0% 0% / 100%)' (using the $alpha parameter): CssColor::red->toRgbString(alpha : true); // Returns 'rgb(255,0,0)' (using the $legacy parameter): CssColor::red->toRgbString(true); // Returns 'rgba(255,0,0,1)' (using the $legacy and $alpha parameters): CssColor::red->toRgbString(true, true);
您还可以通过使用 toCss()
、toHexRgb()
或 toRgb()
方法从任何 CssColor
获取新的 Color
对象。
<?php use matthieumastadenis\couleur\CssColor; require 'vendor/autoload.php'; // Returns a new colors\Css instance: CssColor::red->toCss(); // Returns a new colors\HexRgb instance: CssColor::red->toHexRgb(); // Returns a new colors\Rgb instance: CssColor::red->toRgb();
🌈 颜色空间
Couleur 目前支持以下 色彩空间 和格式
CSS
在 Couleur 中,Css
色彩空间指的是根据 CSS 规范 命名的色彩。因为它们是一个预定义且标准化的精确色彩列表,所以它们都可以通过 CssColor 枚举 容易地访问。
注意:
Css
色彩 不能 有不透明度值。如果您想将不透明度应用于Css
色彩,首先 必须 将其转换为另一个色彩空间。同样,如果您将带有透明度的色彩转换为其Css
相等物,它将 丢失 透明度。
- ColorSpace 枚举情况:
ColorSpace::Css
; - Color 类:
matthieumastadenis\couleur\colors\Css
; - 专用函数命名空间:
matthieumastadenis\couleur\utils\css
; - 接受的别名:
css
、html
、web
;
十六进制RGB
- ColorSpace 情况:
ColorSpace::HexRgb
; - Color 类:
matthieumastadenis\couleur\colors\HexRgb
; - 专用函数 命名空间:
matthieumastadenis\couleur\utils\hexRgb
; - 接受的别名:
hex
、hexrgb
、hex-rgb
、hex_rgb
、hexadecimal
; - 坐标:
red
、green
、blue
;
HSL
- ColorSpace 情况:
ColorSpace::Hsl
; - Color 类:
matthieumastadenis\couleur\colors\Hsl
; - 专用函数 命名空间:
matthieumastadenis\couleur\utils\hsl
; - 接受的别名:
hsl
、hsla
; - 坐标:
hue
、saturation
、lightness
;
HSV
- ColorSpace 情况:
ColorSpace::Hsv
; - Color 类:
matthieumastadenis\couleur\colors\Hsv
; - 专用函数 命名空间:
matthieumastadenis\couleur\utils\hsv
; - 接受的别名:
hsv
、hsb
; - 坐标:
hue
、saturation
、value
;
HWB
- ColorSpace 情况:
ColorSpace::Hwb
; - Color 类:
matthieumastadenis\couleur\colors\Hwb
; - 专用函数 命名空间:
matthieumastadenis\couleur\utils\hwb
; - 接受的别名:
hwb
; - 坐标:
hue
、whiteness
、blackness
;
Lab
- ColorSpace 情况:
ColorSpace::Lab
; - Color 类:
matthieumastadenis\couleur\colors\Lab
; - 专用函数 命名空间:
matthieumastadenis\couleur\utils\lab
; - 接受的别名 :
lab
,cielab
,cie-lab
,cie_lab
; - 坐标 :
lightness
,b
,a
;
Lch
- 颜色空间 情况:
ColorSpace::Lch
; - 颜色类 :
matthieumastadenis\couleur\colors\Lch
; - 专用函数 命名空间:
matthieumastadenis\couleur\utils\lch
; - 接受的别名 :
lch
,cielch
,cie-lch
,cie_lch
; - 坐标 :
lightness
,chroma
,hue
;
线性RGB
- 颜色空间 情况:
ColorSpace::LinRgb
; - 颜色类 :
matthieumastadenis\couleur\colors\LinRgb
; - 专用函数 命名空间:
matthieumastadenis\couleur\utils\linRgb
; - 接受的别名 :
srgb-linear
,linrgb
,linsrgb
,lin-rgb
,lin_rgb
,lin-srgb
,lin_srgb
; - 坐标:
red
、green
、blue
;
线性P3
- 颜色空间 情况:
ColorSpace::LinP3
; - 颜色类 :
matthieumastadenis\couleur\colors\LinP3
; - 专用函数 命名空间:
matthieumastadenis\couleur\utils\linP3
; - 接受的别名 :
p3-linear
,p3_linear
,linp3
,lin-p3
,lin_p3
; - 坐标:
red
、green
、blue
;
线性ProPhoto
- 颜色空间 情况:
ColorSpace::LinProPhoto
; - 颜色类 :
matthieumastadenis\couleur\colors\LinProPhoto
; - 专用函数 命名空间:
matthieumastadenis\couleur\utils\linProPhoto
; - 接受的别名 :
prophoto-linear
,prophoto_linear
,linprophoto
,lin-prophoto
,lin_prophoto
; - 坐标:
red
、green
、blue
;
OkLab
- 颜色空间 情况:
ColorSpace::OkLab
; - 颜色类 :
matthieumastadenis\couleur\colors\OkLab
; - 专用函数 命名空间:
matthieumastadenis\couleur\utils\okLab
; - 接受的别名 :
oklab
,ok-lab
,ok_lab
; - 坐标 :
lightness
,a
,b
;
OkLch
- 颜色空间 情况:
ColorSpace::OkLch
; - 颜色类 :
matthieumastadenis\couleur\colors\OkLch
; - 专用函数 命名空间:
matthieumastadenis\couleur\utils\okLch
; - 接受的别名 :
oklch
,ok-lch
,ok_lch
; - 坐标 :
lightness
,chroma
,hue
;
P3
- 颜色空间 情况:
ColorSpace::LinP3
; - 颜色类 :
matthieumastadenis\couleur\colors\LinP3
; - 专用函数 命名空间:
matthieumastadenis\couleur\utils\linP3
; - 接受的别名 :
display-p3
,display_p3
,p3
; - 坐标:
red
、green
、blue
;
ProPhoto
- 颜色空间 情况:
ColorSpace::ProPhoto
; - 颜色类 :
matthieumastadenis\couleur\colors\ProPhoto
; - 专用函数 命名空间:
matthieumastadenis\couleur\utils\proPhoto
; - 接受的别名 :
prophoto
,prophoto-rgb
,prophoto_rgb
; - 坐标:
red
、green
、blue
;
RGB
- 颜色空间 情况:
ColorSpace::Rgb
; - 颜色类 :
matthieumastadenis\couleur\colors\Rgb
; - 专用函数 命名空间:
matthieumastadenis\couleur\utils\rgb
; - 接受的别名 :
rgb
,rgba
,srgb
,s-rgb
,s_rgb
; - 坐标:
red
、green
、blue
;
XYZ-D50
- 颜色空间 情况:
ColorSpace::XyzD50
; - 颜色类 :
matthieumastadenis\couleur\colors\XyzD50
; - 专用函数 命名空间:
matthieumastadenis\couleur\utils\xyzD50
; - 接受的别名 :
xyz-d50
,xyz_d50
,xyzd50
; - 坐标 :
x
,y
,z
;
XYZ-D65
- 颜色空间 情况:
ColorSpace::XyzD65
; - 颜色类 :
matthieumastadenis\couleur\colors\XyzD65
; - 专用函数 命名空间:
matthieumastadenis\couleur\utils\xyzD65
; - 接受的别名 :
xyz-d65
,xyz_d65
,xyzd65
,xyz
; - 坐标 :
x
,y
,z
;
🤝 贡献
欢迎使用issues 和 pull requests 为此包做出贡献。
在提交任何破坏性更改之前,请考虑联系我(您可以直接提交问题,或者在您认为更合适的情况下,通过 Twitter 发送私信给我)。
📜 许可证
颜色在MIT许可证下公开发布。您可以在任何项目中自由使用它。更多信息,请阅读包含的许可证文件。
❤️ 感谢
衷心感谢Lea Verou和Chris Lilley的杰出工作和他们关于该主题的精确文章。特别感谢Chris在实现这个库的过程中给予的时间和解答。