2012-09-01 38 views
12

我正在開發一個動態存儲項目,並使用循環打印產品的所有顏色選項作爲顏色框,但是我真的需要爲這些顏色添加「邊框」這是輕的。我試着像下面卻是非常有限的,它實際上是僅限於白色而已,它不會趕上像#ddd,#eea ...等如何使用PHP檢測「淺色」

這裏是我的循環:

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

顏色是來自後端的數組,例如:White,#000,#cc0000等。在if/else條件中添加所有異常也不切實際嗎?

+7

谷歌的' RGB到HSV(HSL)' - 色相飽和度值(亮度)。較高的亮度值是較淺的顏色。 – Peter

+0

這個問題可以幫助你:[PHP中的RGB到HSV](http://stackoverflow.com/q/1773698/112968)。然後您可以簡單地檢查V值是否超過某個閾值。 – knittl

+0

http://stackoverflow.com/questions/5614011/intelligent-color-detection檢查這個 –

回答

13

變換HTML顏色RGB,然後到色調 - 飽和度 - 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! 
} 

來源:

演示:

+0

是什麼意思? – Scramble

+0

有可能優化這個奇特的解決方案。即#FFFC00(閃亮的黃色)將不會被檢測爲150以上的淺色 –

6

我在這種情況下會做的是使用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 
} 

上述函數將返回一個百分比你選擇的顏色是多麼亮(簡單的十六進制 - > 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 
} 
+0

偉大的解決方案。只是想指出其他人'$ hex'值不應該有'#';例如:'$ hex ='aaaaaa';'; – Alvaro

+0

確實很棒! – ChristianG

4

除了通過其他的答案給予其他的公式,你可能要考慮Luma

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

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

值接近0會變暗。值接近1會更輕。

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

短的方式,如果你有RGB色彩:

$hex = "4488BB"; 
if(hexdec(substr($hex,0,2))+hexdec(substr($hex,2,2))+hexdec(substr($hex,4,2))> 382){ 
    //bright color 
}else{ 
    //dark color 
}