2013-12-18 42 views
2

我有一個類(顏色),有一堆用於顏色操作的函數。我有一個單獨的函數來返回一個圖像顏色的數組。在每個循環中使用一個php類對象

調色板功能已成功運行 - 將數組輸出到$ pallette。

沒有循環,我可以手動將$ palletteColor var設置爲十六進制,getClosestMatch函數返回距離我的參考數組最近的顏色。一切都好。

當我把它放到一個循環中,我沒有得到正確的結果 - 似乎每個循環的返回是相同的,所以我從getClosestMatch返回10個相同的值。 $調色板是從類以外的功能,所以仍然運行,因爲它應該。 - 編輯:澄清 - $ pallette數組值不一樣 - 請參閱下面的var_dump。

我想我可能誤解了如何創建一個新的類對象工作或應該影響返回的變量。任何人都可以闡明過程應該如何工作?

編輯! 基於以下...當我測試硬編碼的單個值時,它是一個整數(0x003cfe)。在我添加的循環中('0x'。$ palleteColor)。這是否使它成爲一個字符串?我怎樣才能構建一個int來測試?

foreach($palette as $palleteColor) 
{ 

    $color1 = new Color('0x'.$palleteColor); 
    $closestmatch = $color1->getClosestMatch($colors); 
    echo "<tr><td style='background-color:#$palleteColor;width:2em;'>&nbsp;   
    </td><td>#$palleteColor $closestmatch</td></tr>\n"; 
} 

類的構造函數:

public function __construct($intColor = null) 
    { 
     if ($intColor) { 
      $this->fromInt($intColor); 
     } 
    } 

fromINT功能:

public function fromInt($intValue) 
{ 
    $this->color = $intValue; 

    return $this; 
} 

getClosestMatch功能:

public function getClosestMatch(array $colors) 
{ 
    $matchDist = 15; 
    $matchKey = null; 
    foreach($colors as $key => $color) { 
     if (false === ($color instanceof Color)) { 
      $c = new Color($color); 
     } 
     $dist = $this->getDistanceLabFrom($c); 
     if ($dist < $matchDist) { 
      $matchDist = $dist; 
      $matchKey = $key; 
     } 
    } 
    echo $dist; 
    return $matchKey; 
} 

$調色板中的var_dump:

array(10) { [0]=> string(6) "ffffff" [1]=> string(6) "ff3333" 
[2]=> string(6) "cc3333" [3]=> string(6) "ff6666" [4]=> string(6) "cc6666" 
[5]=> string(6) "ffcccc" [6]=> string(6) "ffffcc" [7]=> string(6) "ff9999" 
[8]=> string(6) "ff6633" [9]=> int(993333) } 
+0

我想說這是更多的問題,你傳遞給'Color'構造函數。你確定它需要一個字符串嗎? – Phil

+1

我建議添加更多信息。可能是getClosestMatch的代碼,或者var_dump($ palette)來查看數組內容。 – jpatiaga

+0

$ palette數組中的值是什麼? – user4035

回答

-1

問題解決了:

使用硬編碼值最初測試 -

$color1 = new Color(0x003cfe) 

用於打圈版本:

$color1 = new Color('0x'$palleteColor); 

這使得它的字符串不是一個整數 與固定:

$color1 = new Color(hexdec($palleteColor)); 

謝謝大家。

相關問題