color

A little library to deal with color conversions

Github星跟蹤圖

A little library to handle color conversions

Latest Version on Packagist
Software License
Build Status
SensioLabsInsight
Quality Score
StyleCI
Total Downloads

A little library to handle color conversions. Currently supports rgb, rgba and hex formats.

$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`
echo $hex; // #379bff

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

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Installation

You can install the package via composer:

composer require spatie/color

Usage

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

There are three classes which implement the Color interface:

  • Hex
  • Rgb
  • Rgba

interface Spatie\Color\Color

fromString(): Color

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

Hex::fromString('#000000');
Rgba::fromString('rgba(255, 255, 255, 1)');

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

Rgb and Rgba 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

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 supports opacity, the opacity will simply be omitted.

Rgba::fromString('rgba(0, 0, 255, .5)')->toHex();
// `Hex` instance; '#0000ff'

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'

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('#0000ff'); // `Hex` 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 discover any security related issues, please email freek@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
主編程語言PHP
編程語言PHP (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2016-09-20 12:32:45
推送於2025-02-10 09:24:41
最后一次提交
發布數25
最新版本名稱1.8.0 (發布於 )
第一版名稱0.1.0 (發布於 )
用户参与
星數367
關注者數11
派生數39
提交數198
已啟用問題?
問題數29
打開的問題數3
拉請求數41
打開的拉請求數0
關閉的拉請求數20
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?