PHPackages                             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. spatie/color

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

spatie/color
============

A little library to handle color conversions

2.1.0(3mo ago)38118.9M↓11.3%4120MITPHPPHP ^8.2CI passing

Since Sep 20Pushed 3mo ago10 watchersCompare

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

READMEChangelog (10)Dependencies (2)Versions (26)Used By (20)

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)[![Tests](https://github.com/spatie/color/workflows/Tests/badge.svg)](https://github.com/spatie/color/workflows/Tests/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/75c427de4701d3ded8ebaf4368fb5b525a771bd8522f2dd162a8afb56ec358e0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f636f6c6f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/color)

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

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

Requirements
------------

[](#requirements)

PHP 8.2 or higher is required.

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 eleven classes which implement the `Color` interface:

- `Argb`
- `CIELab`
- `Cmyk`
- `Hex`
- `Hsb`
- `Hsl`
- `Hsla`
- `Named`
- `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)');
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`

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

#### `toArgb(float $alpha = 1): Argb`

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

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

73

—

ExcellentBetter than 100% of packages

Maintenance81

Actively maintained with recent releases

Popularity68

Solid adoption and visibility

Community41

Growing community involvement

Maturity86

Battle-tested with a long release history

 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

Every ~149 days

Recently: every ~101 days

Total

24

Last Release

99d ago

Major Versions

0.1.0 → 1.0.02016-09-20

1.8.0 → v2.x-dev2026-01-31

PHP version history (4 changes)0.1.0PHP ^7.0

1.2.0PHP ^7.3

1.2.4PHP ^7.3|^8.0

v2.x-devPHP ^8.2

### Community

Maintainers

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

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (56 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (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)")[![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)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![isaeken](https://avatars.githubusercontent.com/u/57031552?v=4)](https://github.com/isaeken "isaeken (2 commits)")[![overclokk](https://avatars.githubusercontent.com/u/4604932?v=4)](https://github.com/overclokk "overclokk (2 commits)")[![riasvdv](https://avatars.githubusercontent.com/u/3626559?v=4)](https://github.com/riasvdv "riasvdv (2 commits)")[![Maxeee09](https://avatars.githubusercontent.com/u/6651496?v=4)](https://github.com/Maxeee09 "Maxeee09 (1 commits)")[![Angel5a](https://avatars.githubusercontent.com/u/55488380?v=4)](https://github.com/Angel5a "Angel5a (1 commits)")[![andypost](https://avatars.githubusercontent.com/u/73713?v=4)](https://github.com/andypost "andypost (1 commits)")[![NiklasBr](https://avatars.githubusercontent.com/u/279826?v=4)](https://github.com/NiklasBr "NiklasBr (1 commits)")

---

Tags

colorconversionhexphprgbspatieconversioncolorrgb

###  Code Quality

TestsPest

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-package-tools

Tools for creating Laravel packages

945125.5M7.0k](/packages/spatie-laravel-package-tools)[spatie/laravel-data

Create unified resources and data transfer objects

1.8k28.9M627](/packages/spatie-laravel-data)[spatie/macroable

A trait to dynamically add methods to a class

72759.6M64](/packages/spatie-macroable)[spatie/regex

A sane interface for php's built in preg\_\* functions

1.1k17.1M59](/packages/spatie-regex)[cuyz/valinor

Dependency free PHP library that helps to map any input into a strongly-typed structure.

1.5k9.2M108](/packages/cuyz-valinor)[spatie/enum

PHP Enums

84529.1M68](/packages/spatie-enum)

PHPackages © 2026

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