PHPackages                             wdelfuego/spatie-color - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. wdelfuego/spatie-color

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

wdelfuego/spatie-color
======================

A little library to handle color conversions

v1.8.1(10mo ago)02MITPHPPHP ^7.3|^8.0

Since Aug 16Pushed 10mo agoCompare

[ Source](https://github.com/wdelfuego/color)[ Packagist](https://packagist.org/packages/wdelfuego/spatie-color)[ Docs](https://github.com/spatie/color)[ GitHub Sponsors](https://github.com/spatie)[ RSS](/packages/wdelfuego-spatie-color/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (2)Versions (2)Used By (0)

A little library to handle color conversions and comparisons
============================================================

[](#a-little-library-to-handle-color-conversions-and-comparisons)

[![Latest Version on Packagist](https://camo.githubusercontent.com/0758ed8c31377b959e0d65986928ae1de5d43b66d1891131b556a085cb7e4a50/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f636f6c6f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/color)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/bc9956cc40e4b36438b1a6fa7ecb9b4a0f30eba1e64f620728893bd48949d25b/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7370617469652f636f6c6f722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/spatie/color)[![Quality Score](https://camo.githubusercontent.com/064e423e0cc92fd12ab446fdc5596cb3f519d1d522e7ba95bb5b7ad98468f932/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7370617469652f636f6c6f722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/spatie/color)[![Total Downloads](https://camo.githubusercontent.com/75c427de4701d3ded8ebaf4368fb5b525a771bd8522f2dd162a8afb56ec358e0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f636f6c6f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/color)[![Tests](https://github.com/spatie/color/workflows/Tests/badge.svg)](https://github.com/spatie/color/workflows/Tests/badge.svg)

A little library to handle color conversions and comparisons. Currently, supports CSS names, rgb, rgba, 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)

$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
----------

[](#support-us)

[![](https://camo.githubusercontent.com/d4e9cdfba7ae0a564d16bdbcdaec27cf1beb563134b761884f8614dc7c336b19/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f636f6c6f722e6a70673f743d31)](https://spatie.be/github-ad-click/color)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

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](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/color
```

Usage
-----

[](#usage)

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

There are seven classes which implement the `Color` interface:

- `CIELab`
- `Cmyk`
- `Hex`
- `Hsb`
- `Hsl`
- `Hsla`
- `Rgb`
- `Rgba`
- `Xyz`

### `interface Spatie\Color\Color`

[](#interface-spatiecolorcolor)

#### `fromString(): 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)');
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`

[](#red-intstring)

Return the value of the `red` color channel.

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

#### `green(): int|string`

[](#green-intstring)

Return the value of the `green` color channel.

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

#### `blue(): int|string`

[](#blue-intstring)

Return the value of the `blue` color channel.

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

#### `toCmyk(): Cmyk`

[](#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`

[](#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`

[](#tohsb-hsb)

Convert a color to a `Hsb` color.

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

#### `toHsl(): Hsl`

[](#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`

[](#tohslafloat-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)'
```

#### `toRgb(): Rgb`

[](#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`

[](#torgbafloat-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`

[](#__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`

[](#factoryfromstring-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
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security
--------

[](#security)

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

Credits
-------

[](#credits)

- [Sebastian De Deyne](https://github.com/sebastiandedeyne)
- [All Contributors](../../contributors)

About Spatie
------------

[](#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](https://spatie.be/opensource).

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance54

Moderate activity, may be stable

Popularity2

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

322d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1326136?v=4)[wdelfuego](/maintainers/wdelfuego)[@wdelfuego](https://github.com/wdelfuego)

---

Top Contributors

[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (49 commits)")[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (49 commits)")[![Nielsvanpach](https://avatars.githubusercontent.com/u/10651054?v=4)](https://github.com/Nielsvanpach "Nielsvanpach (18 commits)")[![AyoobMH](https://avatars.githubusercontent.com/u/37803924?v=4)](https://github.com/AyoobMH "AyoobMH (15 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (11 commits)")[![kodie](https://avatars.githubusercontent.com/u/603949?v=4)](https://github.com/kodie "kodie (9 commits)")[![horuskol](https://avatars.githubusercontent.com/u/290549?v=4)](https://github.com/horuskol "horuskol (6 commits)")[![nesk](https://avatars.githubusercontent.com/u/817508?v=4)](https://github.com/nesk "nesk (5 commits)")[![danielebarbaro](https://avatars.githubusercontent.com/u/4376886?v=4)](https://github.com/danielebarbaro "danielebarbaro (4 commits)")[![AstroCorp](https://avatars.githubusercontent.com/u/22180346?v=4)](https://github.com/AstroCorp "AstroCorp (3 commits)")[![isaeken](https://avatars.githubusercontent.com/u/57031552?v=4)](https://github.com/isaeken "isaeken (2 commits)")[![riasvdv](https://avatars.githubusercontent.com/u/3626559?v=4)](https://github.com/riasvdv "riasvdv (2 commits)")[![overclokk](https://avatars.githubusercontent.com/u/4604932?v=4)](https://github.com/overclokk "overclokk (2 commits)")[![jcogs-design](https://avatars.githubusercontent.com/u/13821249?v=4)](https://github.com/jcogs-design "jcogs-design (2 commits)")[![Afrowson](https://avatars.githubusercontent.com/u/8524263?v=4)](https://github.com/Afrowson "Afrowson (2 commits)")[![thaqebon](https://avatars.githubusercontent.com/u/29402142?v=4)](https://github.com/thaqebon "thaqebon (1 commits)")[![Tjoosten](https://avatars.githubusercontent.com/u/5157609?v=4)](https://github.com/Tjoosten "Tjoosten (1 commits)")[![vanodevium](https://avatars.githubusercontent.com/u/16780069?v=4)](https://github.com/vanodevium "vanodevium (1 commits)")[![ElRochito](https://avatars.githubusercontent.com/u/1737307?v=4)](https://github.com/ElRochito "ElRochito (1 commits)")[![Angel5a](https://avatars.githubusercontent.com/u/55488380?v=4)](https://github.com/Angel5a "Angel5a (1 commits)")

---

Tags

spatieconversioncolorrgb

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/wdelfuego-spatie-color/health.svg)

```
[![Health](https://phpackages.com/badges/wdelfuego-spatie-color/health.svg)](https://phpackages.com/packages/wdelfuego-spatie-color)
```

###  Alternatives

[spatie/color

A little library to handle color conversions

38221.2M36](/packages/spatie-color)[ozdemirburak/iris

PHP library for color manipulation and conversion.

1201.9M23](/packages/ozdemirburak-iris)[ssnepenthe/color-utils

A PHP library for performing SASS-like color manipulations.

631.2M16](/packages/ssnepenthe-color-utils)[tecnickcom/tc-lib-color

PHP library to manipulate various color representations

247.9M26](/packages/tecnickcom-tc-lib-color)[fjw/color-compare

A library for converting colors (Hex, RGB, HSL, CIELAB (LAB), DIN-99) and calculating color distances based on DIN-99.

1310.8k](/packages/fjw-color-compare)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
