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

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

davidgorges/color-contrast-php
==============================

A tiny library to find color combinations with a contrast threshold

v1.0.0(6y ago)104016[2 PRs](https://github.com/davidgorges/color-contrast-php/pulls)MITPHPPHP &gt;=5.4

Since Feb 27Pushed 1w ago3 watchersCompare

[ Source](https://github.com/davidgorges/color-contrast-php)[ Packagist](https://packagist.org/packages/davidgorges/color-contrast-php)[ Docs](http://www.davidgorges.de)[ RSS](/packages/davidgorges-color-contrast-php/feed)WikiDiscussions master Synced yesterday

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

ColorContrast
=============

[](#colorcontrast)

Small library to find valid color combinations for a given contrast threshold. The contrast calculation algorithm is based on the WCAG 2.0.

 [![Build Status](https://camo.githubusercontent.com/5b08e2b0c93992ff2e93d7462f604164b183a44de466a5d365b545b127788605/68747470733a2f2f7472617669732d63692e6f72672f6461766964676f726765732f636f6c6f722d636f6e74726173742d7068702e737667)](https://travis-ci.org/davidgorges/color-contrast-php) [![Latest Stable Version](https://camo.githubusercontent.com/43e53ca2f5e812a2c4ab3eab893490eeb494f2cc8d036b8017d4270903afed71/68747470733a2f2f706f7365722e707567782e6f72672f6461766964676f726765732f636f6c6f722d636f6e74726173742d7068702f762f737461626c65)](https://packagist.org/packages/davidgorges/color-contrast-php) [![Total Downloads](https://camo.githubusercontent.com/7b8b79f2994d7b83ef4ff30f8658616502b3271d8c0026ab81419ddd91f06dba/68747470733a2f2f706f7365722e707567782e6f72672f6461766964676f726765732f636f6c6f722d636f6e74726173742d7068702f646f776e6c6f616473)](https://packagist.org/packages/davidgorges/color-contrast-php/stats) [![License](https://camo.githubusercontent.com/8b78070f676b26c39d748a493c179b51a2cb6aced4d5a8acd7351b71643498f8/68747470733a2f2f706f7365722e707567782e6f72672f6461766964676f726765732f636f6c6f722d636f6e74726173742d7068702f6c6963656e7365)](https://choosealicense.com/licenses/mit/) [![PHPStan Enabled](https://camo.githubusercontent.com/441b5874ce4df0a2defc892979c96c46889b69cb32119d04f0b48626349f8bc9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d656e61626c65642d627269676874677265656e2e7376673f7374796c653d666c6174)](https://github.com/davidgorges/color-contrast-php)

---

Install
-------

[](#install)

Using composer:

```
composer require davidgorges/color-contrast
```

Usage
-----

[](#usage)

```
    use ColorContrast\ColorContrast;

    /* ... */

    $contrast = new ColorContrast();
    $contrast->addColors(0xff0000, 0x002200, 0x0022ff, 0xffffff);
    $combinations = $contrast->getCombinations(ColorContrast::MIN_CONTRAST_AAA);
    foreach ($combinations as $combination) {
        printf("#%s on the Background color #%s has a contrast value of %f \n", $combination->getForeground(), $combination->getBackground(), $combination->getContrast());
    }
```

this would output:

```
    #FFFFFF on the Background color #002200 has a contrast value of 17.949476
    #FFFFFF on the Background color #0022FF has a contrast value of 8.033817
    #002200 on the Background color #FFFFFF has a contrast value of 17.949476
    #0022FF on the Background color #FFFFFF has a contrast value of 8.033817

```

### Dark or Light Complimentary Color

[](#dark-or-light-complimentary-color)

```
    $contrast = new ColorContrast();

    // imagine having a red background and you need to know if its better
    // to display a white or black text on top if it
    $complimentary = $contrast->complimentaryTheme('#d80000'); // returns ColorContrast::DARK to indicate you should use a dark color on this background

    if($complimentary == ColorContrast::LIGHT) {
        // Use a light font
    } else {
        // use a dark font
    }
```

Thanks
------

[](#thanks)

- [Calculating Color Contrast with PHP](http://www.splitbrain.org/blog/2008-09/18-calculating_color_contrast_with_php) by Andreas Gohr
- [jxnblk/colorable](https://github.com/jxnblk/colorable) JavaScript Library that inspired ColorContrast

License
-------

[](#license)

The MIT License (MIT)

Copyright (c) 2015 David Gorges

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance64

Regular maintenance activity

Popularity22

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity63

Established project with proven stability

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

Total

4

Last Release

2510d ago

Major Versions

v0.1.1 → v1.0.02019-06-28

PHP version history (2 changes)v0.1.0PHP &gt;=5.3.3

v1.0.0PHP &gt;=5.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/6abaff8e3d8d647d1cd8d3479bb3793b16a66ee24877d195e697a646439730ba?d=identicon)[dgorges](/maintainers/dgorges)

---

Tags

librarycolorusabilitycontrastWCAG 2.0

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[davidgorges/color-contrast

A tiny library to find color combinations with a contrast threshold

10550.4k](/packages/davidgorges-color-contrast)[league/iso3166

ISO 3166-1 PHP Library

69536.3M116](/packages/league-iso3166)[spatie/color

A little library to handle color conversions

38018.9M28](/packages/spatie-color)[dekor/php-array-table

PHP Library for printing associative arrays as text table (similar to mysql terminal console)

296.6M2](/packages/dekor-php-array-table)

PHPackages © 2026

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