davidgorges / color-contrast
A tiny library to find color combinations with a contrast threshold
Installs: 345 245
Dependents: 1
Suggesters: 0
Security: 0
Stars: 10
Watchers: 3
Forks: 6
Open Issues: 0
Requires
- php: >=5.4
- mischiefcollective/colorjizz: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15
- phpstan/phpstan: ^0.11.8
- phpunit/phpunit: ^8.2
This package is auto-updated.
Last update: 2024-12-23 17:29:02 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.
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
- Calculating Color Contrast with PHP by Andreas Gohr
- jxnblk/colorable JavaScript Library that inspired ColorContrast
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.