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

ActiveLibrary

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

Color parsing, conversion, manipulation, and WCAG contrast checking

v1.0.3(1mo ago)11[1 PRs](https://github.com/philiprehberger/php-color/pulls)MITPHPPHP ^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)[ RSS](/packages/philiprehberger-php-color/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (5)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)[![License](https://camo.githubusercontent.com/f5e3e7f87216771cd022950906e320d2faa969d4292935c5af615d039bbb07d1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7068696c69707265686265726765722f7068702d636f6c6f72)](LICENSE)

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

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

### 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::shades($color, 5);         // 5 progressively darker shades
Palette::tints($color, 5);          // 5 progressively lighter tints
```

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`->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`->invert(): Color`Invert the color`->grayscale(): Color`Convert to grayscale`->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::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
vendor/bin/phpstan analyse
```

License
-------

[](#license)

MIT

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance89

Actively maintained with recent releases

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

4

Last Release

56d 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 (6 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.

1201.7M16](/packages/ozdemirburak-iris)[ssnepenthe/color-utils

A PHP library for performing SASS-like color manipulations.

631.1M10](/packages/ssnepenthe-color-utils)[spatie/color

A little library to handle color conversions

38018.9M28](/packages/spatie-color)[tecnickcom/tc-lib-color

PHP library to manipulate various color representations

247.2M9](/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.0k](/packages/fjw-color-compare)[davidgorges/color-contrast

A tiny library to find color combinations with a contrast threshold

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

PHPackages © 2026

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