spatie/color 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

spatie/color

Composer 安装命令:

composer require spatie/color

包简介

A little library to handle color conversions

README 文档

README

Latest Version on Packagist Tests Total Downloads

A little library to handle color conversions and comparisons. Currently, supports CSS names, rgb, rgba, argb, hex, hsl, hsla, CIELab, and xyz color formats as well as CIE76, CIE94, and CIEDE2000 color comparison algorithms.

$named = Named::fromString('peru'); // case-insensitive

echo $named->red(); // 205
echo $named->green(); // 133
echo $named->blue(); // 63

echo $named->toHex(); // #cd853f

$rgb = Rgb::fromString('rgb(55,155,255)');

echo $rgb->red(); // 55
echo $rgb->green(); // 155
echo $rgb->blue(); // 255

echo $rgb; // rgb(55,155,255)

$rgba = $rgb->toRgba(); // `Spatie\Color\Rgba`
$rgba->alpha(); // 1
echo $rgba; // rgba(55,155,255,1)

$hex = $rgb->toHex(); // `Spatie\Color\Hex`
$rgba->alpha(); // ff
echo $hex; // #379bff

$cmyk = $rgb->toCmyk(); // `Spatie\Color\Cmyk`
echo $cmyk; // cmyk(78,39,0,0)

$hsl = $rgb->toHsl(); // `Spatie\Color\Hsl`
echo $hsl; // hsl(210,100%,100%)

$hsb = $rgb->toHsb(); // `Spatie\Color\Hsb`
echo $hsb; // hsl(210,78.4%,100%)

$lab = $rgb->toCIELab();
echo $lab; // CIELab(62.91,5.34,-57.73)

$xyz = $rgb->toXyz();
echo $xyz; // xyz(31.3469,31.4749,99.0308)

$argb = $rgb->toArgb(0.5); // `Spatie\Color\Argb`
echo $argb; // argb(0.50,55,155,255)

$hex2 = Hex::fromString('#2d78c8');

$ratio = Contrast::ratio(Hex::fromString('#f0fff0'), Hex::fromString('#191970')); 
echo $ratio; // 15.0

$cie76_distance = Distance::CIE76($rgb, $hex2);
$cie76_distance = Distance::CIE76('rgba(55,155,255,1)', '#2d78c8'); // Outputs the same thing, Factory is built-in to all comparison functions
echo $cie76_distance; // 55.89468042667388

$cie94_distance = Distance::CIE94($rgb, $hex2);
echo $cie94_distance; // 13.49091942790753

$cie94_textiles_distance = Distance::CIE94($rgb, $hex2, 1); // Third parameter optionally sets the application type (0 = Graphic Arts [Default], 1 = Textiles)
echo $cie94_textiles_distance; // 7.0926538068477

$ciede2000_distance = Distance::CIEDE2000($rgb, $hex2);
echo $ciede2000_distance; // 12.711957696300898

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Requirements

PHP 8.2 or higher is required.

Installation

You can install the package via composer:

composer require spatie/color

Usage

The Color package contains a separate class per color format, which each implement a Color interface.

There are eleven classes which implement the Color interface:

  • Argb
  • CIELab
  • Cmyk
  • Hex
  • Hsb
  • Hsl
  • Hsla
  • Named
  • Rgb
  • Rgba
  • Xyz

interface Spatie\Color\Color

fromString(): Color

Parses a color string and returns a Color implementation, depending on the format of the input string.

Named::fromString('blue');
Hex::fromString('#000000');
Rgba::fromString('rgba(255, 255, 255, 1)');
Argb::fromString('argb(1, 255, 255, 255)');
Hsla::fromString('hsla(360, 100%, 100%, 1)');

Throws an InvalidColorValue exception if the string can't be parsed.

Rgb, Rgba, Hsl and Hsla strings are allowed to have spaces. rgb(0,0,0) is just as valid as rgb(0, 0, 0).

red(): int|string

Return the value of the red color channel.

Hex::fromString('#ff0000')->red(); // 'ff'
Rgb::fromString('rgb(255, 0, 0)')->red(); // 255

green(): int|string

Return the value of the green color channel.

Hex::fromString('#00ff00')->green(); // 'ff'
Rgb::fromString('rgb(0, 255, 0)')->green(); // 255

blue(): int|string

Return the value of the blue color channel.

Hex::fromString('#0000ff')->blue(); // 'ff'
Rgb::fromString('rgb(0, 0, 255)')->blue(); // 255

toCmyk(): Cmyk

Convert a color to a Cmyk color.

Rgb::fromString('rgb(0, 0, 255)')->toCmyk();
// `Cmyk` instance; 'cmyk(100,100,0,0)'

toHex(): Hex

Convert a color to a Hex color.

Rgb::fromString('rgb(0, 0, 255)')->toHex();
// `Hex` instance; '#0000ff'

When coming from a color format that doesn't support opacity, it can be added by passing it to the $alpha parameter.

toHsb(): Hsb

Convert a color to a Hsb color.

Rgb::fromString('rgb(0, 0, 255)')->toHsb();
// `Hsl` instance; 'hsb(240, 100%, 100%)'

toHsl(): Hsl

Convert a color to a Hsl color.

Rgb::fromString('rgb(0, 0, 255)')->toHsl();
// `Hsl` instance; 'hsl(240, 100%, 50%)'

When coming from a color format that supports opacity, the opacity will simply be omitted.

Rgba::fromString('rgba(0, 0, 255, .5)')->toHsl();
// `Hsl` instance; 'hsl(240, 100%, 50%)'

toHsla(float $alpha = 1): Hsla

Convert a color to a Hsla color.

Rgb::fromString('rgb(0, 0, 255)')->toHsla();
// `Hsla` instance; 'hsla(240, 100%, 50%, 1.0)'

When coming from a color format that doesn't support opacity, it can be added by passing it to the $alpha parameter.

Rgb::fromString('rgb(0, 0, 255)')->toHsla(.5);
// `Hsla` instance; 'hsla(240, 100%, 50%, 0.5)'

toArgb(float $alpha = 1): Argb

Convert a color to an Argb color. ARGB places the alpha channel first, commonly used in Android development and Windows APIs.

Rgb::fromString('rgb(0, 0, 255)')->toArgb();
// `Argb` instance; 'argb(1.00,0,0,255)'

When coming from a color format that doesn't support opacity, it can be added by passing it to the $alpha parameter.

Rgb::fromString('rgb(0, 0, 255)')->toArgb(.5);
// `Argb` instance; 'argb(0.50,0,0,255)'

toRgb(): Rgb

Convert a color to an Rgb color.

Hex::fromString('#0000ff')->toRgb();
// `Rgb` instance; 'rgb(0, 0, 255)'

When coming from a color format that supports opacity, the opacity will simply be omitted.

Rgba::fromString('rgb(0, 0, 255, .5)')->toRgb();
// `Rgb` instance; 'rgb(0, 0, 255)'

toRgba(float $alpha = 1): Rgba

Convert a color to a Rgba color.

Rgb::fromString('rgb(0, 0, 255)')->toRgba();
// `Rgba` instance; 'rgba(0, 0, 255, 1)'

When coming from a color format that doesn't support opacity, it can be added by passing it to the $alpha parameter.

Rgba::fromString('rgb(0, 0, 255)')->toRgba(.5);
// `Rgba` instance; 'rgba(0, 0, 255, .5)'

__toString(): string

Cast the color to a string.

(string) Rgb::fromString('rgb(0, 0, 255)'); // 'rgb(0,0,255)'
(string) Rgba::fromString('rgb(0, 0, 255, .5)'); // 'rgb(0,0,255,0.5)'
(string) Hex::fromString('#0000ff'); // '#0000ff'
(string) Hsl::fromString('hsl(240, 100%, 50%)'); // 'hsl(240, 100%, 50%)'
(string) Hsla::fromString('hsla(240, 100%, 50%, 1.0)'); // 'hsla(240, 100%, 50%, 1.0)'

Factory::fromString(): Color

With the Factory class, you can create a color instance from any string (it does an educated guess under the hood). If the string isn't a valid color string in any format, it throws an InvalidColorValue exception.

Factory::fromString('rgb(0, 0, 255)'); // `Rgb` instance
Factory::fromString('blue'); // `Named` instance
Factory::fromString('#0000ff'); // `Hex` instance
Factory::fromString('hsl(240, 100%, 50%)'); // `Hsl` instance
Factory::fromString('Hello world!'); // `InvalidColorValue` exception

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.

Credits

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.

spatie/color 适用场景与选型建议

spatie/color 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.45M 次下载、GitHub Stars 达 381, 最近一次更新时间为 2016 年 09 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「conversion」 「color」 「rgb」 「spatie」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 spatie/color 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 spatie/color 我们能提供哪些服务?
定制开发 / 二次开发

基于 spatie/color 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 21.45M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 385
  • 点击次数: 29
  • 依赖项目数: 30
  • 推荐数: 0

GitHub 信息

  • Stars: 381
  • Watchers: 10
  • Forks: 41
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-09-20