24

我正在开发一个动态商店项目,我使用循环将产品的所有颜色选项打印为颜色框,但是我确实需要为这些浅色添加“边框”。我尝试了类似以下的东西,但它非常有限,它实际上仅限于白色,它不会捕捉到像#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;
        }
    }
}

Colors 是来自后端的数组,例如:White、#000、#cc0000 等。在 if/else 条件中添加所有异常也是不切实际的,有什么快速的想法吗?

4

5 回答 5

34

将 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);

用法:

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

来源:

演示:

于 2012-09-01T14:50:11.230 回答
12

在这种情况下,我要做的是使用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
}

上面的函数将返回您选择的颜色的亮度百分比(这也需要简单的 hex -> 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
}
于 2012-09-01T14:50:02.710 回答
7

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

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

$l = luma(0, 15, 255);

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

于 2012-09-01T15:13:08.477 回答
4

如果您有 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
}

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

于 2017-07-26T13:36:56.370 回答
0

还有一种更简单的方法,代码更少:

<?php
//Functions

function getRGB($colorCode) {
    //Turn html color code into RGB
    $var_R = substr($colorCode, 0, 2);
    $var_G = substr($colorCode, 2, 2);
    $var_B = substr($colorCode, 4, 2);  

    //Get Hex values
    $val_R = hexdec($var_R);
    $val_G = hexdec($var_G);
    $val_B = hexdec($var_B);

    //Red is seen as light too, gets fixed with this
    $remRed = hexdec('99');
    if ($val_R > $remRed) {
        $RGB = $val_G.' '.$val_B;
    } else {
        $RGB = $val_R.' '.$val_G.' '.$val_B;
    }

    return $RGB;
}

function getHSL($R = 255, $G = 255, $B = 255) {
    $hsl = (max($R, $G, $B) + min($R, $G, $B)) / 510.0;
    return $hsl;
}
?>

现在调用:

$color = 0000FF; //Blue
$RGBcode = getRGB($color); //Returns 0 0 255
$RGBcode = str_replace(' ', ', ', $RGBcode); //Replaces an empty space with a ,
$val_HSL = getHSL($RGBcode); //Returns value from 0.5 to 1
if ($val_HSL >= 0.8) {
    //Reject color
} else {
    //Accept Color
    $color = '#'.$color; //Sets it to html: #0000FF
}
于 2017-06-02T14:41:32.820 回答