如何用PHP检测"浅色"颜色

Ahm*_*uad 19 php

我正在研究一个动态商店项目,我使用一个循环来打印产品的所有颜色选项作为颜色框,但是我真的需要为这些颜色添加"边框".我尝试了类似下面的东西,但它非常有限,它实际上仅限于白色,它不会捕捉像#ddd,#eea ......等

这是我的循环:

foreach($colors as $color) {
    $color = trim($color);
    if (!empty($color)) {
        if (in_array($color, array('white','White','#fff','#FFF','#FFFFFF','#ffffff'))) {
            $bordercolor = '#bbb';
        } else {
            $bordercolor = $color;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

颜色是来自后端的数组,如:White,#000,#cc0000等.在if/else条件中添加所有异常也是不实际的,任何快速的想法?

Pet*_*ter 20

将HTML颜色转换为RGB,然后转换为Hue-Saturation-Lightnes(HSV)

<?php

function HTMLToRGB($htmlCode)
  {
    if($htmlCode[0] == '#')
      $htmlCode = substr($htmlCode, 1);

    if (strlen($htmlCode) == 3)
    {
      $htmlCode = $htmlCode[0] . $htmlCode[0] . $htmlCode[1] . $htmlCode[1] . $htmlCode[2] . $htmlCode[2];
    }

    $r = hexdec($htmlCode[0] . $htmlCode[1]);
    $g = hexdec($htmlCode[2] . $htmlCode[3]);
    $b = hexdec($htmlCode[4] . $htmlCode[5]);

    return $b + ($g << 0x8) + ($r << 0x10);
  }

function RGBToHSL($RGB) {
    $r = 0xFF & ($RGB >> 0x10);
    $g = 0xFF & ($RGB >> 0x8);
    $b = 0xFF & $RGB;

    $r = ((float)$r) / 255.0;
    $g = ((float)$g) / 255.0;
    $b = ((float)$b) / 255.0;

    $maxC = max($r, $g, $b);
    $minC = min($r, $g, $b);

    $l = ($maxC + $minC) / 2.0;

    if($maxC == $minC)
    {
      $s = 0;
      $h = 0;
    }
    else
    {
      if($l < .5)
      {
        $s = ($maxC - $minC) / ($maxC + $minC);
      }
      else
      {
        $s = ($maxC - $minC) / (2.0 - $maxC - $minC);
      }
      if($r == $maxC)
        $h = ($g - $b) / ($maxC - $minC);
      if($g == $maxC)
        $h = 2.0 + ($b - $r) / ($maxC - $minC);
      if($b == $maxC)
        $h = 4.0 + ($r - $g) / ($maxC - $minC);

      $h = $h / 6.0; 
    }

    $h = (int)round(255.0 * $h);
    $s = (int)round(255.0 * $s);
    $l = (int)round(255.0 * $l);

    return (object) Array('hue' => $h, 'saturation' => $s, 'lightness' => $l);
  }

$colour = '#F12346';
$rgb = HTMLToRGB($colour);
$hsl = RGBToHSL($rgb);

var_dump($hsl);
Run Code Online (Sandbox Code Playgroud)

用法:

$colour = '#F12346';
$rgb = HTMLToRGB($colour);
$hsl = RGBToHSL($rgb);
if($hsl->lightness > 200) {
  // this is light colour!
}
Run Code Online (Sandbox Code Playgroud)

资源:

演示:


jer*_*emy 10

在这种情况下我要做的是使用HSL检测颜色的亮度,并将其与特定百分比进行比较.例如,HSL算法中的亮度属性取色度(M-m,其中M是最大RGB值,m是最小RGB值)并将其除以2.

function lightness($R = 255, $G = 255, $B = 255) {
    return (max($R, $G, $B) + min($R, $G, $B)) / 510.0; // HSL algorithm
}
Run Code Online (Sandbox Code Playgroud)

上面的函数将返回你选择的颜色的亮度的百分比(简单的十六进制 - > rgb转换也是必需的,但这应该很简单).我除以510而不是2的原因是因为为了得到除以2 之后的百分比,你除以255.为了使它更快,你可以简单地说:(x / 2) / 255 = x / 510.然后我将上述函数返回的值与80%进行比较.

$r = hexdec($hex[0].$hex[1]);
$g = hexdec($hex[2].$hex[3]);
$b = hexdec($hex[4].$hex[5]);

if(lightness($r, $g, $b) >= .8) {
    // add border
} else {
    // no border
}
Run Code Online (Sandbox Code Playgroud)


Mat*_*hew 7

除了其他答案给出的其他公式外,您可能还需要考虑Luma

function luma($r, $g, $b)
{
  return (0.2126 * $r + 0.7152 * $g + 0.0722 * $b) / 255;
}

$l = luma(0, 15, 255);
Run Code Online (Sandbox Code Playgroud)

接近 0 的值会更暗。接近 1 的值会更轻。


Luc*_* C. 6

如果您有 RGB 颜色作为十六进制字符串,则简写方式:

$hexRGB = "4488BB";
if(hexdec(substr($hexRGB,0,2))+hexdec(substr($hexRGB,2,2))+hexdec(substr($hexRGB,4,2))> 381){
    //bright color
}else{
    //dark color
}
Run Code Online (Sandbox Code Playgroud)

:阈值 381 是平均水平值的总和。如果您希望阈值更亮或更暗,请在 1 - 765 范围内设置上限或下限 381。