2010-12-16 149 views
0

我有不同顏色的十六進制提交。目前即時通訊使用rgb2hex功能,所以無論或工作。 我的目標是我有一套我需要使用的顏色,並且我希望現有的顏色hex或rgb改變爲最接近的數組或其他顏色。rbg或十六進制選擇顏色?

基本上,我有大約15色值,我想基本上是一個函數,它的RGB,看看哪一個最接近的。如果我理解正確(數組)

+1

您可能想要考慮使用完全不同完全不同的顏色模型。 – 2010-12-16 22:39:58

回答

0

要任意近似於十六進制顏色預定義?

製作一個函數,可以對輸入和矩陣進行r-g-b排序。

$inHex = array(r,g,b); 
$colArray = array(array(r1,g1,b1),array(...)) 

$minDiff = 10000; 
$color = false; 
for($i=0;$i<sizeof($colArray);$i++) { 
    $diff = abs($inHex(0) - $colArray[$i][0]) + 
    abs($inHex(1) - $colArray[$i][2]) + 
    abs($inHex(2) - $colArray[$i][2]); 
    if ($diff<$minDiff) $color = $i; 
} 
//ok, $color is pointing at closest color.. 

問候, //牛逼

+0

所以,如果我正確地得到你,我首先需要製作15個數組,使用$ colArray內的rg b很容易。 使用$ inHex中的顏色 – Ugleh 2010-12-18 05:54:50

1

編輯:你有沒有用Google搜索周圍的東西,才發現自己的答案嗎?這是因爲它是自包含的,可能是更有用的,只要一個功能:


function convertToClosest($c) { 
    // set minimum difference you'll allow between colors 
    $minDiff = 1000; 
    // generate color array 
    $colorArrayOriginal = array(
     "black" => "000000", 
     "brown" => "6E4700", 
     "gray" => "555555", 
     "white" => "FFFFFF", 
     "red" => "EB0000", 
     "orange" => "FF9914", 
     "yellow" => "FFF71C", 
     "green" => "1BB500", 
     "blue" => "005BB5", 
     "purple" => "4E00B5"   
    ); 
    foreach ($colorArrayOriginal as $colorID => $color) { 
     $r = substr($color,0,2); 
     $g = substr($color,2,2); 
     $b = substr($color,4,2); 
     $colorArray[$colorID] = array($r,$g,$b);  
    } 
    // here, we break apart the color we input, $c 
    $r = substr($c,0,2); 
    $g = substr($c,2,2); 
    $b = substr($c,4,2); 
    $inHex = array($r,$g,$b); 

    $color = false; 
    // we define the "best so far" variable as the min, since we can't have a best that's more 
    $bestDiff = $minDiff; 

    // here, we parse through each of the colors finding the closest, using the native hexdec function to parse 
    // out the best values to compare 
    foreach ($colorArray as $colorID => $cc) {  
     $diff = abs(hexdec($inHex[0]) - hexdec($cc[0])) + abs(hexdec($inHex[1]) - hexdec($cc[1])) + abs(hexdec($inHex[2]) - hexdec($cc[2])); 
    // if the difference in value between the colors is less than the best one of all the ones we've tried... 
    if ($diff<=$bestDiff) { 
     $color = $colorID; 
     $bestDiff = $diff; 
    } 
    } 
return $color; 
} 

以下是原文:

function convertToClosest($c,$colorArray) { 
    // here, we break apart the color we input, $c 
    $r = substr($c,0,2); 
    $g = substr($c,2,2); 
    $b = substr($c,4,2); 
    $inHex = array($r,$g,$b); 
    $color = false; 
    // we define the "best so far" variable as the min, since we can't have a best that's more 
    $bestDiff = $minDiff; 

    // here, we parse through each of the colors finding the closest, using the native hexdec function to parse 
    // out the best values to compare 
    foreach ($colorArray as $colorID => $cc) { 
    $diff = abs(hexdec($inHex[0]) - hexdec($cc[0])) + abs(hexdec($inHex[1]) - hexdec($cc[1])) + abs(hexdec($inHex[2]) - hexdec($cc[2])); 
    // if the difference in value between the colors is less than the best one of all the ones we've tried... 
    if ($diff<=$bestDiff) { 
    $color = $colorID; 
    $bestDiff = $diff; 
    } 
    } 
return $color; 
} 

另外,如果你想生成$ colorArray變量

$colorArrayOriginal = [two dimensional array of your colors ] 
foreach ($colorArrayOriginal as $c) { 
    $r = substr($c["hex"],0,2); 
    $g = substr($c["hex"],2,2); 
    $b = substr($c["hex"],4,2); 
    $colorArray["$c[id]"] = array($r,$g,$b);  
}