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

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

philiprehberger/php-color
=========================

Color parsing, conversion, manipulation, and WCAG contrast checking

v1.2.0(4mo ago)168MITPHPPHP ^8.2CI passing

Since Mar 15Pushed 1mo agoCompare

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

READMEChangelogDependencies (6)Versions (7)Used By (0)

PHP Color
=========

[](#php-color)

[![Tests](https://github.com/philiprehberger/php-color/actions/workflows/tests.yml/badge.svg)](https://github.com/philiprehberger/php-color/actions/workflows/tests.yml)[![Latest Version on Packagist](https://camo.githubusercontent.com/66bd4214150c9ef15647c90ccc7335093204a0f6705cc5df083d0c50f7a77306/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7068696c69707265686265726765722f7068702d636f6c6f722e737667)](https://packagist.org/packages/philiprehberger/php-color)[![Last updated](https://camo.githubusercontent.com/7c38bd53a426d58bd1189564bcc98b88262a336535c769257bdc838cc0539b80/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f7068696c69707265686265726765722f7068702d636f6c6f72)](https://github.com/philiprehberger/php-color/commits/main)

Color parsing, conversion, manipulation, and WCAG contrast checking.

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

[](#requirements)

- PHP 8.2+

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

[](#installation)

```
composer require philiprehberger/php-color
```

Usage
-----

[](#usage)

### Creating Colors

[](#creating-colors)

```
use PhilipRehberger\Color\Color;

$color = Color::hex('#ff6347');       // From hex
$color = Color::rgb(255, 99, 71);    // From RGB
$color = Color::hsl(9, 100, 64);     // From HSL
$color = Color::named('tomato');      // From CSS name
$color = Color::random();            // Random color
```

### Converting Colors

[](#converting-colors)

```
$color = Color::hex('#ff6347');

$color->toHex();    // '#ff6347'
$color->toRgb();    // 'rgb(255, 99, 71)'
$color->toHsl();    // 'hsl(9, 100%, 63.9%)'
$color->toArray();  // ['r' => 255, 'g' => 99, 'b' => 71, 'alpha' => 1.0]
```

### Manipulating Colors

[](#manipulating-colors)

All manipulation methods return new `Color` instances (immutable).

```
$color = Color::hex('#3366cc');

$color->lighten(20);        // Lighten by 20%
$color->darken(10);         // Darken by 10%
$color->saturate(15);       // Increase saturation by 15%
$color->desaturate(15);     // Decrease saturation by 15%
$color->invert();           // Invert the color
$color->grayscale();        // Convert to grayscale

$red = Color::hex('#ff0000');
$blue = Color::hex('#0000ff');
$red->mix($blue, 0.5);     // Mix two colors
```

### Blending Colors

[](#blending-colors)

```
$red = Color::hex('#ff0000');
$blue = Color::hex('#0000ff');

$red->blend($blue, 'multiply');   // Multiply blend
$red->blend($blue, 'screen');     // Screen blend
$red->blend($blue, 'overlay');    // Overlay blend
$red->blend($blue, 'darken');     // Darken blend
$red->blend($blue, 'lighten');    // Lighten blend
```

### Color Analysis

[](#color-analysis)

```
$color = Color::hex('#3366cc');

$color->isLight();  // false
$color->isDark();   // true

$red = Color::hex('#ff0000');
$blue = Color::hex('#0000ff');
$red->distance($blue);  // CIE76 Delta E perceptual distance
```

### WCAG Contrast Checking

[](#wcag-contrast-checking)

```
$text = Color::hex('#333333');
$bg = Color::hex('#ffffff');

$text->contrastRatio($bg);    // 12.63
$text->meetsWcagAA($bg);      // true (>= 4.5:1)
$text->meetsWcagAAA($bg);     // true (>= 7:1)

// Also accepts hex strings directly
$text->contrastRatio('#ffffff');
```

### Generating Palettes

[](#generating-palettes)

```
use PhilipRehberger\Color\Palette;

$color = Color::hex('#ff6347');

Palette::complementary($color);        // [original, complement]
Palette::analogous($color);            // [left, original, right]
Palette::triadic($color);              // [original, +120deg, +240deg]
Palette::splitComplementary($color);   // [original, +150deg, +210deg]
Palette::tetradic($color);             // [original, +90deg, +180deg, +270deg]
Palette::shades($color, 5);            // 5 progressively darker shades
Palette::tints($color, 5);             // 5 progressively lighter tints
```

### Generating Gradients

[](#generating-gradients)

```
$black = Color::hex('#000000');
$white = Color::hex('#ffffff');

Palette::gradient($black, $white, 5);          // 5-step RGB gradient
Palette::gradient($black, $white, 5, 'hsl');   // 5-step HSL gradient
```

API
---

[](#api)

### Color

[](#color)

MethodDescription`Color::hex(string $hex): Color`Create from hex string (3, 4, 6, or 8 digits)`Color::rgb(int $r, int $g, int $b, float $alpha = 1.0): Color`Create from RGB values`Color::hsl(float $h, float $s, float $l, float $alpha = 1.0): Color`Create from HSL values`Color::named(string $name): Color`Create from CSS named color`Color::random(): Color`Generate a random color`->lighten(float $percent): Color`Lighten by percentage`->darken(float $percent): Color`Darken by percentage`->saturate(float $percent): Color`Increase saturation`->desaturate(float $percent): Color`Decrease saturation`->mix(Color $other, float $weight = 0.5): Color`Mix with another color`->blend(Color $other, string $mode): Color`Blend with another color using a CSS blend mode`->invert(): Color`Invert the color`->grayscale(): Color`Convert to grayscale`->distance(Color $other): float`CIE76 Delta E perceptual distance`->isLight(): bool`True if relative luminance &gt; 0.5`->isDark(): bool`True if relative luminance &lt;= 0.5`->contrastRatio(Color|string $other): float`WCAG contrast ratio (1.0-21.0)`->meetsWcagAA(Color|string $background): bool`Meets WCAG AA (4.5:1)`->meetsWcagAAA(Color|string $background): bool`Meets WCAG AAA (7:1)`->toHex(): string`Output as hex string`->toRgb(): string`Output as rgb()/rgba() string`->toHsl(): string`Output as hsl()/hsla() string`->toArray(): array`Output as associative array### Palette

[](#palette)

MethodDescription`Palette::complementary(Color $color): array`Complementary pair`Palette::analogous(Color $color, float $angle = 30.0): array`Three analogous colors`Palette::triadic(Color $color): array`Three triadic colors`Palette::splitComplementary(Color $color): array`Split-complementary triad`Palette::tetradic(Color $color): array`Tetradic (rectangular) quartet`Palette::gradient(Color $start, Color $end, int $steps, string $space = 'rgb'): array`Interpolated color gradient`Palette::shades(Color $color, int $count = 5): array`Progressive darker shades`Palette::tints(Color $color, int $count = 5): array`Progressive lighter tintsDevelopment
-----------

[](#development)

```
composer install
vendor/bin/phpunit
vendor/bin/pint --test
```

Support
-------

[](#support)

If you find this project useful:

⭐ [Star the repo](https://github.com/philiprehberger/php-color)

🐛 [Report issues](https://github.com/philiprehberger/php-color/issues?q=is%3Aissue+is%3Aopen+label%3Abug)

💡 [Suggest features](https://github.com/philiprehberger/php-color/issues?q=is%3Aissue+is%3Aopen+label%3Aenhancement)

❤️ [Sponsor development](https://github.com/sponsors/philiprehberger)

🌐 [All Open Source Projects](https://philiprehberger.com/open-source-packages)

💻 [GitHub Profile](https://github.com/philiprehberger)

🔗 [LinkedIn Profile](https://www.linkedin.com/in/philiprehberger)

License
-------

[](#license)

[MIT](LICENSE)

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance83

Actively maintained with recent releases

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 87.5% of commits — single point of failure

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 ~2 days

Total

6

Last Release

136d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cfd7d24cbbf32400fa13ce0bbe7a31edd2d66a6d4488eafdb3d64c5337bf0435?d=identicon)[philiprehberger](/maintainers/philiprehberger)

---

Top Contributors

[![philiprehberger](https://avatars.githubusercontent.com/u/8218077?v=4)](https://github.com/philiprehberger "philiprehberger (14 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")

---

Tags

colorrgbhexwcaghslcontrast

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[ozdemirburak/iris

PHP library for color manipulation and conversion.

1221.9M24](/packages/ozdemirburak-iris)[ssnepenthe/color-utils

A PHP library for performing SASS-like color manipulations.

631.2M17](/packages/ssnepenthe-color-utils)[spatie/color

A little library to handle color conversions

38222.1M39](/packages/spatie-color)[tecnickcom/tc-lib-color

PHP library to manipulate various color representations

248.3M32](/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.

1311.1k](/packages/fjw-color-compare)[davidgorges/color-contrast

A tiny library to find color combinations with a contrast threshold

10566.7k](/packages/davidgorges-color-contrast)

PHPackages © 2026

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