delights/color

此包已被废弃且不再维护。作者建议使用felixdorn/php-color包。

一个简单的PHP库,用于智能生成颜色

4.3.0 2024-05-26 14:00 UTC

This package is auto-updated.

Last update: 2024-09-17 17:45:05 UTC


README

Tests Formats Version Total Downloads License

安装

需要 PHP 8.3+

您可以通过composer安装此包

composer require felixdorn/php-color

特性

  • 支持HSLA(及HSL)、HEX、RGBA(及RGB)
  • 根据给定的种子(如用户邮箱)生成颜色
  • 加深、变亮颜色。
  • 计算亮度、明度、暗度
  • 检查两种颜色的对比度

目录

用法

生成好看的颜色

您可以在运行时生成颜色

use Felix\PHPColor\Generator;

Generator::one();
Generator::many(n: 10)
Generator::manyLazily(n: 10_000)

重要:生成的颜色使用以下默认值

  • 色调:[0, 360](所有色调)
  • 饱和度:[50, 90](在[0, 100]之外)
  • 明度 [50, 70](在[0, 100]之外)
  • 透明度 [100, 100](在[0, 100]之外)

这会生成明亮、饱和的颜色。

您可以为使用Generator生成的所有颜色更改默认值。

use Felix\PHPColor\Generator;

Generator::withDefaults(
    hue: [100, 200],
    saturation: 50,
    lightness: [40, 60],
    alpha: [100, 100]
);

或更改一些默认值

Generator::withDefaults(
    hue: [120, 140] // just restrict the hue but keep the saturation and lightness settings
);

您可以强制生成器使用特定的种子

$avatarColor = Generator::one(seed: $email); // will always return the same color for the given seed.

这同样适用于Generator::manyGenerator::manyLazily

您可以覆盖用于生成颜色的默认色调、饱和度和明度范围

use Felix\PHPColor\Generator;

$avatarColor = Generator::one(
    hue: [100, 200],
    lightness: [40, 80]
);
$avatarColor = Generator::many(
    hue: null, // use global defaults
    saturation: [100, 100]
    lightness: [40, 80]
);
$avatarColor = Generator::manyLazily(
    lightness: [50, 60]
)

或指定单个数字而不是范围

use Felix\PHPColor\Generator;

$avatarColor = Generator::one(hue: [0, 360], lightness: 50, saturation: 100)

生成器返回Hsla对象。让我们看看它们是如何工作的。

使用HSLA对象。

您可能从某个地方获取了一个不是HSLA的颜色,您可以将其转换

从RGB到HSLA

\Felix\PHPColor\Hsla::fromRGB(255, 0, 0);

从十六进制到HSLA

\Felix\PHPColor\Hsla::fromHex("#FF0000")
\Felix\PHPColor\Hsla::fromHex("FF0000")

从头开始

use Felix\PHPColor\Hsla;

$color = new Hsla(100, 20, 20);
$color = new Hsla(100, .2, .2); // automatically normalized to 0-100

Hsla::boundedRandom([0, 360], [0,100], [0,100], [0, 100], $seed)

Hsla::random($seed);

您可以将HSLA颜色转换回十六进制、RGB、HSLA...

$color->toHex(); // #000000
$color->toRgba(); // rgb(0, 0, 0)
$color->toHsla(); // hsl(0, 0, 0)

您可以访问颜色的属性

$color->hue; # between 0-360
$color->saturation; # between 0-100
$color->lightness; # between 0-100
$color->alpha; # between 0-100

$color->setHue(...)->setSaturation(...)->setLightness(...)->setAlpha(...); // modifies the color
$color->withHue(...) // returns a new instance
$color->withSaturation(...); // returns a new instance
$color->withLightness(...); // returns a new instance
$color->withAlpha(...); // returns a new instance

// If you chain more than one with...(), use clone() + set...() instead:
$color->clone() 
     ->setHue(...)
     ->setSaturation(...)
     ->setLightness()
     ->setAlpha();

$color->colorChannels(); // returns [r, g, b]
$color->red(); // 0-255
$color->green(); // 0-255
$color->blue(); // 0-255

并检查颜色的亮度

$color->isDark();
$color->isBright();

您还可以指定一个阈值,一个介于0(最暗)和100(最亮)之间的数字

$color->isDark(threshold: 5);

您也可以加深或变亮给定的颜色

// Returns a new instance of the color
$color->darken($percentage = 15);
$color->lighten($percentage = 15);

亮度

https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef所述。

$color->luminance(); // 0.0 - 1.0

对比度

https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef所述。这对于可访问性测试非常有用。返回1到21之间的值。通常,这被写成1:1或21:1。这返回“n:1”。

$color->contrast($otherColor); // 1 - 21

测试

composer test

PHP ColorFélix Dorn 创建,遵循 MIT许可