2010-12-16 7 views
0

私は、異なる色のhexsの提出を持っています。現時点では、rgb2hex関数を使用していますので、どちらかまたは動作します。 目標は、私が使用する必要がある色のセットを持っている、と私は、配列か何かの最も近い色に変更するには、既存の色の六角またはRGBをしたいです。rbgまたはhexを使用して色を選択できますか?

基本的に、私は約15色の値を持っている、と私は基本的にRGBをとる関数をしたいと私が正しくあなたを理解していれば

+1

変数完全なカラーモデル。 –

答えて

0

(配列の)に最も近い1は、あなたが任意に近似したい見ます16進数の色をあらかじめ定義していますか?

入力して、マトリックスの両方の、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

だから私はあなたが正しい場合は、$配列の中でr g bで簡単に15配列を作る必要があります。色は$ inHexで使用しています – Ugleh

1

編集:あなたが唯一のあなた自身の答えを見つけるために何かのために周りググことがありますか?ここでは、自己完結型だものであれば、より便利かもしれない機能は次のとおりです。ここで


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を生成する場合あなたは完全にDIFを使用して検討する必要があります

$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);  
} 
関連する問題