如何在PHP中通过对比反转RGB十六进制值 [英] How to invert RGB hex values by contrast in PHP

查看:86
本文介绍了如何在PHP中通过对比反转RGB十六进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我的代码如下:

Thus far I have the code below:

function hexrgb_invert($hex) {
    $arr = str_split($hex, 2);
    foreach ($arr as &$value) {
        $c = base_convert($value, 16, 10);
        $value = str_pad(base_convert(255 - $c, 10, 16), 2, '0', STR_PAD_LEFT);
    }
    return implode('', $arr);
}

问题:我需要反转颜色相反。上面的功能适用于某些事物,但不适用于其他事物。

The Problem: I need to invert colors based on contrast. The above function works for some things but not others.

示例:如果输入的是 9d702f 输出将为 9d702f 。 (两种颜色的对比度较低)

Example: If the input is 9d702f the output will be 9d702f. (2 colors that have a low contrast)

我在StackOverflow上的其他地方都找不到运气,因为大多数答案似乎都使​​用了我已经在使用的相同算法。

I haven't found any luck looking elsewhere on StackOverflow, as most answers seem to use the same algorithm I am already using.

更多示例:

假设我们正在尝试找到 #FFFFFF (白色)的反义词。这非常简单,因为白色是原色,因此可以轻松计算出其相反色。 (上面的功能非常适合。) #FFFFFF 的反义词当然是#000000 (黑色)当您比较这两种颜色时,您会得到对比度21:1

Let's say that I am trying to find the contrasting opposite of #FFFFFF (white). This is very straight forward because white is a primary color so its opposite can be easily calculated. (Which the above function will work perfectly for.) The opposite of #FFFFFF is of course #000000 (black) and when you compare the 2 colors you get a contrast ratio of 21:1.

但是,如果我们尝试对颜色<$ c $使用上面的相同功能c>#808080 ,它将为我们提供颜色#7F7F7F 。这两种颜色几乎相同,并且具有对比度仅为1.01:1 。这是因为您越接近十六进制 80 (十进制 128 ),函数提供的对比度就越小。

However, if we try to use the same function above on the color #808080 it will give us the color #7F7F7F. Those 2 colors are almost identical and have a contrast ratio of only 1.01:1. This is because the closer you get to hex 80 (decimal 128) the less contrast that function can provide.

在特定情况下#808080 颜色#000000 会提供最有争议的是5.32:1

In the specific case of #808080 the color #000000 would provide the most constrast at 5.32:1.

function rgb_best_contrast($r, $g, $b) {
    return array(
        'r' => ($r < 128) ? 255 : 0,
        'g' => ($g < 128) ? 255 : 0,
        'b' => ($b < 128) ? 255 : 0
    );
}


推荐答案

尝试计算与任何给定颜色的最佳对比相反颜色的方法,我最终分解并开始手动测试各个颜色对比。

After trying a few dozen different methods to try and calculate the best contrasting opposite color to any given color, I finally broke down and started manually testing individual color contrasts.

我使用此脚本搜索整个颜色RGB颜色空间,可最佳匹配任何给定颜色:

I used this script to search the entire RGB color space for the best match for any given color:

$test_a = ['r' => 128, 'g' => 128, 'b' => 128];
$best = [
    'color' => ['r' => 128, 'g' => 128, 'b' => 128],
    'diff'  => 0.0
];
foreach (range(0, 255) as $r) {
    foreach (range(0, 255) as $g) {
        foreach (range(0, 255) as $b) {
            $test_b = ['r' => $r, 'g' => $g, 'b' => $b];
            // YQI sensitive contrast check
            $diff = check::rgb_contrast($test_a, $test_b);
            if ($diff > $best['diff']) {
                $best = [
                    'color' => $test_b,
                    'diff'  => $diff
                ];
            }
        }
    }
}
var_dump($best);

简而言之,我的所有结果都出现了一个非常明显的模式。此函数适用于该模式:

In short, a very obvious pattern emerged with all of my results. This function works off that pattern:

function rgb_best_contrast($r, $g, $b) {
    return array(
        'r' => ($r < 128) ? 255 : 0,
        'g' => ($g < 128) ? 255 : 0,
        'b' => ($b < 128) ? 255 : 0
    );
}

完全按预期工作。始终提供最佳的对比颜色。

Works exactly as expected. Always gives the best contrasting color.

这篇关于如何在PHP中通过对比反转RGB十六进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆