davidgorges/color-contrast

A tiny library to find color combinations with a contrast threshold

v1.0.0 2019-06-28 13:49 UTC

This package is auto-updated.

Last update: 2024-03-23 15:45:50 UTC


README

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 Latest Stable Version Total Downloads License PHPStan Enabled

Install

Using composer:

composer require davidgorges/color-contrast

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

    $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

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.