PHPackages                             breadthe/php-contrast - 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. breadthe/php-contrast

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

breadthe/php-contrast
=====================

Color and contrast tools

v0.2.0(5y ago)73.1k↑55.2%5MITPHPPHP &gt;=7.3

Since Feb 20Pushed 2y ago1 watchersCompare

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

READMEChangelog (2)Dependencies (2)Versions (4)Used By (0)

PHP Contrast Tools
==================

[](#php-contrast-tools)

[![Latest Version on Packagist](https://camo.githubusercontent.com/c117161a3a3dcc4d03e151506f7b0e3a7d8814b027adb946497f23209fd32d31/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f62726561647468652f7068702d636f6e74726173742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/breadthe/php-contrast)[![Build Status](https://camo.githubusercontent.com/b443ab0acb7eb2388eadc96fd3ae4c6030e5ebd0842e69bd59feccf17b9016e2/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f62726561647468652f7068702d636f6e74726173742f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/breadthe/php-contrast)[![Quality Score](https://camo.githubusercontent.com/274966f62367a7873c797238dde2f870bde3ce43b57a76a2241421f307f33a77/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f62726561647468652f7068702d636f6e74726173742e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/breadthe/php-contrast)[![Total Downloads](https://camo.githubusercontent.com/d65d5769377d992e7b319524da7742b407fafaf6d55f779a2d3078893e0f6a7d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f62726561647468652f7068702d636f6e74726173742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/breadthe/php-contrast)

Provides various utilities for working with color contrast.

The primary purpose is to facilitate easy generation of accessible (minimum 3.0 contrast ratio) hex color pairs. A variation of these tools restricts the generated colors to [TailwindCSS's](https://tailwindcss.com/) default palette.

I've created a [demo page](https://mumu.pw/contrast) to show how color pairs can be generated by this package.

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

[](#installation)

You can install the package via composer:

```
composer require breadthe/php-contrast
```

Usage
-----

[](#usage)

Import the class.

```
use Breadthe\PhpContrast\HexColor; // factory

use Breadthe\PhpContrast\HexColorPair; // hex pair utilities

use Breadthe\PhpContrast\TailwindColor; // Tailwind color pair utilities
```

Check the contrast ratio between 2 colors
-----------------------------------------

[](#check-the-contrast-ratio-between-2-colors)

```
$hexColorPair = HexColorPair::make(HexColor::make('000000'), HexColor::make('ffffff'));
$hexColorPair->ratio; // 21
$hexColorPair->fg; // '#000000'
$hexColorPair->bg; // '#ffffff'
```

Get a random color pair (with the resulting ratio), with minimum 3:1 contrast ratio
-----------------------------------------------------------------------------------

[](#get-a-random-color-pair-with-the-resulting-ratio-with-minimum-31-contrast-ratio)

```
$hexColorPair = HexColorPair::random();
$hexColorPair->ratio; // 3.8
$hexColorPair->fg->hex; // '#36097e'
$hexColorPair->fg->name; // null
$hexColorPair->bg->hex; // '#ed4847'
$hexColorPair->bg->name; // null
```

Get a random color pair (with the resulting ratio), with minimum specified contrast ratio (but no less than 3:1)
----------------------------------------------------------------------------------------------------------------

[](#get-a-random-color-pair-with-the-resulting-ratio-with-minimum-specified-contrast-ratio-but-no-less-than-31)

**⚠️ Warning** For performance reasons, the minimum requested contrast ratio is capped at 4.5, although the generated pairs can go up to the theoretical maximum 21:1 ratio.

**⚠️ Caution** When chaining with `minContrast()`, make sure to use `getRandom()` instead of `random()`.

```
$hexColorPair = HexColorPair::minContrast(4.5)->getRandom();
$hexColorPair->ratio; // 7.6
$hexColorPair->fg->hex; // '#0c402f'
$hexColorPair->fg->name; // null
$hexColorPair->bg->hex; // '#badd73'
$hexColorPair->bg->name; // null
```

Get a random accessible sibling for the given color, with minimum specified contrast ratio (but no less than 3:1)
-----------------------------------------------------------------------------------------------------------------

[](#get-a-random-accessible-sibling-for-the-given-color-with-minimum-specified-contrast-ratio-but-no-less-than-31)

```
// Minimum 3:1 contrast ratio
HexColorPair::sibling('000000')->hex; // '#ffffff'

// Minimum specified contrast ratio (no less than 3:1)
HexColorPair::minContrast(4.5)->getSibling('000000')->hex; // '#ffffff'
```

Generate a random TailwindCSS color
-----------------------------------

[](#generate-a-random-tailwindcss-color)

```
$twColor = TailwindColor::random();
$twColor->hex; // '#e2e8f0'
$twColor->name; // 'gray-300'
```

Generate a pair of random accessible TailwindCSS colors
-------------------------------------------------------

[](#generate-a-pair-of-random-accessible-tailwindcss-colors)

```
$twColorpair = TailwindColor::randomPair();
$twColorpair->ratio; // 3.3
$twColorpair->fg->hex; // '#63b3ed'
$twColorpair->fg->name; // 'blue-400'
$twColorpair->bg->hex; // '#4a5568'
$twColorpair->bg->name; // 'green-700'
```

Generate a pair of random accessible TailwindCSS colors, with minimum specified contrast ratio (but no less than 3:1)
---------------------------------------------------------------------------------------------------------------------

[](#generate-a-pair-of-random-accessible-tailwindcss-colors-with-minimum-specified-contrast-ratio-but-no-less-than-31)

**⚠️ Warning** For performance reasons, the minimum requested contrast ratio is capped at 4.5, although the generated pairs can go up to the theoretical maximum 21:1 ratio.

**⚠️ Caution** When chaining with `minContrast()`, make sure to use `getRandomPair()` instead of `randomPair()`.

```
$twColorpair = TailwindColor::minContrast(4.5)->getRandomPair();
$twColorpair->ratio; // 7.0
$twColorpair->fg->hex; // '#faf5ff'
$twColorpair->fg->name; // 'purple-100'
$twColorpair->bg->hex; // '#9b2c2c'
$twColorpair->bg->name; // 'red-800'
```

Merge the default Tailwind colors with a custom palette
-------------------------------------------------------

[](#merge-the-default-tailwind-colors-with-a-custom-palette)

You may extend the default Tailwind colors with your own custom palette. Here's an example of how to import a custom palette from a JSON file.

```
{
    "background": "#FFFFFF",
    "headline": "#1f1235",
    "sub-headline": "#1b1425",
    "button": "#ff6e6c",
    "button-text": "#1f1235"
}
```

```
$customPalette = json_decode(file_get_contents('custom-palette.json'), true);

$colors = TailwindColor::merge($customPalette)->getColors();
```

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

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

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Omigosh Dev](https://github.com/breadthe)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

PHP Package Boilerplate
-----------------------

[](#php-package-boilerplate)

This package was generated using the [PHP Package Boilerplate](https://laravelpackageboilerplate.com).

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90.9% 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 ~365 days

Total

2

Last Release

1905d ago

PHP version history (2 changes)0.1PHP 7.\*

v0.2.0PHP &gt;=7.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/555692bc62844933d570637ec8f699ec25755e0d2b101dc0eee8370f66bc9916?d=identicon)[breadthe](/maintainers/breadthe)

---

Top Contributors

[![breadthe](https://avatars.githubusercontent.com/u/17433578?v=4)](https://github.com/breadthe "breadthe (10 commits)")[![danjohnson95](https://avatars.githubusercontent.com/u/6871504?v=4)](https://github.com/danjohnson95 "danjohnson95 (1 commits)")

---

Tags

color-pairscontrast-ratiogenerated-pairsphpphp-contrastbreadthephp-contrast

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/breadthe-php-contrast/health.svg)

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

###  Alternatives

[illuminate/support

The Illuminate Support package.

583107.1M34.4k](/packages/illuminate-support)[pragmarx/countries

PHP Countries and Currencies

1.9k3.3M18](/packages/pragmarx-countries)[illuminate/events

The Illuminate Events package.

13454.3M1.7k](/packages/illuminate-events)[illuminate/config

The Illuminate Config package.

10842.7M2.2k](/packages/illuminate-config)[illuminate/pagination

The Illuminate Pagination package.

10532.5M858](/packages/illuminate-pagination)[illuminate/broadcasting

The Illuminate Broadcasting package.

7126.5M177](/packages/illuminate-broadcasting)

PHPackages © 2026

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