davidgorges/color-contrast

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

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

This package is auto-updated.

Last update: 2024-09-23 16:52:10 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)

版权所有 © 2015 David Gorges

以下是对此软件及其相关文档文件(“软件”)的副本或实质部分副本的任何人免费许可,以未经限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许向软件提供者提供软件的人这样做,前提是符合以下条件

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

软件按“原样”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是基于合同、侵权或其他方式,源于、产生于或与软件或软件的使用或其他交易有关。