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

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

delights/color
==============

A simple library to work and smartly generate colors in PHP

1.0.1(1y ago)0787↓37.5%MITPHPPHP ^8.3

Since Sep 17Pushed 1y ago1 watchersCompare

[ Source](https://github.com/felixdorn/php-color)[ Packagist](https://packagist.org/packages/delights/color)[ RSS](/packages/delights-color/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (6)Versions (3)Used By (0)

PHP Color
=========

[](#php-color)

[![Tests](https://github.com/felixdorn/php-color/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/felixdorn/php-color/actions/workflows/tests.yml)[![Formats](https://github.com/felixdorn/php-color/actions/workflows/formats.yml/badge.svg?branch=master)](https://github.com/felixdorn/php-color/actions/workflows/formats.yml)[![Version](https://camo.githubusercontent.com/b99f303f589d26d6934f4b1a9bf8214bb8db6080013e3d6d595368973721ea02/68747470733a2f2f706f7365722e707567782e6f72672f66656c6978646f726e2f7068702d636f6c6f722f76657273696f6e)](https://packagist.org/packages/felixdorn/php-color)[![Total Downloads](https://camo.githubusercontent.com/90356732ec4c94332b6a0e17b6796d67239167f8618bdedd1372f974ba2e2adc/68747470733a2f2f706f7365722e707567782e6f72672f66656c6978646f726e2f7068702d636f6c6f722f646f776e6c6f616473)](https://packagist.org/packages/felixdorn/php-color)[![License](https://camo.githubusercontent.com/f127f174a5f82b2338f1deb3a721007293eff1b34d1c71a2173ed63e6014bff3/68747470733a2f2f706f7365722e707567782e6f72672f66656c6978646f726e2f7068702d636f6c6f722f6c6963656e7365)](https://packagist.org/packages/felixdorn/php-color)

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

[](#installation)

> Requires [PHP 8.3+](https://php.net/releases)

You can install the package via composer:

```
composer require felixdorn/php-color
```

### Features

[](#features)

- Support HSLA (and HSL)&amp;, HEX, RGBA (and RGB)
- Generate a color for a given seed (like a user email)
- Darken, lighten the color.
- Compute the luminance, lightness, darkness
- Check the contrast of two colors

### TOC

[](#toc)

- [Generating nice looking colors](#generating-nice-looking-colors)
- [Working with the HSLA object](#working-with-the-hsla-object-)
    - [From RGB to HSLA](#from-rgb-to-hsla)
    - [From Hex to HSLA](#from-hex-to-hsla)
    - [From CSS to HSLA](#from-css-to-hsla)

Usage
-----

[](#usage)

### Generating nice looking colors

[](#generating-nice-looking-colors)

You can generate colors on the fly:

```
use Felix\PHPColor\Generator;

Generator::one();
Generator::many(n: 10)
Generator::manyLazily(n: 10_000)
```

**Important:** the colors generated are generate with the following defaults

- Hue: \[0, 360\] (all hues)
- Saturation: \[50, 90\] (out of \[0, 100\])
- Lightness \[50, 70\] (out of \[0, 100\])
- Alpha \[100, 100\] (out of \[0, 100\])

This generates bright, saturated colors.

You may change the defaults for all generated colors used by the `Generator`.

```
use Felix\PHPColor\Generator;

Generator::withDefaults(
    hue: [100, 200],
    saturation: 50,
    lightness: [40, 60],
    alpha: [100, 100]
);
```

Or some of the defaults

```
Generator::withDefaults(
    hue: [120, 140] // just restrict the hue but keep the saturation and lightness settings
);
```

You may force the generator to use a certain seed:

```
$avatarColor = Generator::one(seed: $email); // will always return the same color for the given seed.
```

This also works for `Generator::many` and `Generator::manyLazily`.

You may override the default hue, saturation, and lightness ranges used to generate a color:

```
use Felix\PHPColor\Generator;

$avatarColor = Generator::one(
    hue: [100, 200],
    lightness: [40, 80]
);
$avatarColor = Generator::many(
    hue: null, // use global defaults
    saturation: [100, 100]
    lightness: [40, 80]
);
$avatarColor = Generator::manyLazily(
    lightness: [50, 60]
)
```

Or specify a single number instead of a range:

```
use Felix\PHPColor\Generator;

$avatarColor = Generator::one(hue: [0, 360], lightness: 50, saturation: 100)
```

The generator returns `Hsla` objects. Let us see how they work.

### Working with the HSLA object.

[](#working-with-the-hsla-object)

You may be getting a color from somewhere which is not HSLA, you can convert them:

#### From RGB to HSLA

[](#from-rgb-to-hsla)

```
\Felix\PHPColor\Hsla::fromRGB(255, 0, 0);
```

#### From Hex to HSLA

[](#from-hex-to-hsla)

```
\Felix\PHPColor\Hsla::fromHex("#FF0000")
\Felix\PHPColor\Hsla::fromHex("FF0000")
```

#### From scratch

[](#from-scratch)

```
use Felix\PHPColor\Hsla;

$color = new Hsla(100, 20, 20);
$color = new Hsla(100, .2, .2); // automatically normalized to 0-100

Hsla::boundedRandom([0, 360], [0,100], [0,100], [0, 100], $seed)

Hsla::random($seed);
```

You may convert your HSLA color back to hex, RGB, HSLA...

```
$color->toHex(); // #000000
$color->toRgba(); // rgb(0, 0, 0)
$color->toHsla(); // hsl(0, 0, 0)
```

You may access the properties of the color:

```
$color->hue; # between 0-360
$color->saturation; # between 0-100
$color->lightness; # between 0-100
$color->alpha; # between 0-100

$color->setHue(...)->setSaturation(...)->setLightness(...)->setAlpha(...); // modifies the color
$color->withHue(...) // returns a new instance
$color->withSaturation(...); // returns a new instance
$color->withLightness(...); // returns a new instance
$color->withAlpha(...); // returns a new instance

// If you chain more than one with...(), use clone() + set...() instead:
$color->clone()
     ->setHue(...)
     ->setSaturation(...)
     ->setLightness()
     ->setAlpha();

$color->colorChannels(); // returns [r, g, b]
$color->red(); // 0-255
$color->green(); // 0-255
$color->blue(); // 0-255
```

And check the brightness of a color:

```
$color->isDark();
$color->isBright();
```

You may also specify a threshold, a number between 0 (darkest) and 100 (brightest):

```
$color->isDark(threshold: 5);
```

You may darken or lighten a given color:

```
// Returns a new instance of the color
$color->darken($percentage = 15);
$color->lighten($percentage = 15);
```

Luminance
---------

[](#luminance)

As in .

```
$color->luminance(); // 0.0 - 1.0
```

Contrast
--------

[](#contrast)

As in . Very useful for accessibility testing. Returns a value between 1 and 21. Usually, this is written as 1:1 or 21:1. This returns "n:1".

```
$color->contrast($otherColor); // 1 - 21
```

Testing
-------

[](#testing)

```
composer test
```

**PHP Color** was created by **Félix Dorn** under the **[MIT license](https://opensource.org/licenses/MIT)**.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity55

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

2

Last Release

608d ago

### Community

Maintainers

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

---

Top Contributors

[![felixdorn](https://avatars.githubusercontent.com/u/55788595?v=4)](https://github.com/felixdorn "felixdorn (36 commits)")

---

Tags

package

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[geniusts/laravel-hijri-dates

Hijri dates package for Laravel

2722.5k](/packages/geniusts-laravel-hijri-dates)

PHPackages © 2026

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