davidgorges/color-contrast-php

一个用于找到具有对比度阈值的颜色组合的小型库

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

This package is auto-updated.

Last update: 2024-09-23 17:02:14 UTC


README

一个小型库,用于找到给定对比度阈值的有效颜色组合。对比度计算算法基于WCAG 2.0。

Build Status Latest Stable Version Total Downloads License PHPStan Enabled

安装

使用composer

composer require davidgorges/color-contrast

用法

    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());
    }

这将输出

    #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

深色或浅色互补色

    $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
    }

感谢

许可证

MIT许可证 (MIT)

版权所有 (c) 2015 David Gorges

特此授予任何获得此软件及其相关文档副本(“软件”)的人,免费、不可撤销地使用该软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许提供软件的人为此目的使用该软件,受以下条件的约束

上述版权声明和本许可声明应包含在软件的所有副本或实质性部分中。

本软件按“原样”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于适销性、特定用途适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任承担责任,无论源于合同、侵权或其他行为,是否因软件或其使用或其他方式引起。