2011-09-01 167 views
0

如何以編程方式將色環分割爲多個部分,以及如何在此部分中獲取所有RGB顏色?將色環分割爲多個部分

Color Circle

我想FCE返回我RGB顏色由輸入parament彩色部分。

int getColorSection(RGB color); 
+0

所以你想找到一個顏色的位置,在你的形象?你的返回值需要2個值,x和y(或極座標時的角度和r) –

+0

我把它當作了;編號1-25以上的部分,找到給定的顏色部分。這比@喬治的解釋更難。 – Jamiec

+0

不,我想比較兩個RGB顏色,我想知道如果兩者都在同一顏色部分 –

回答

2

我不知道我是否正確地理解了這個問題,但我想你問的是一種顏色是更綠,更藍還是藍紅? 這會給你提供哪些部分(紅色,綠色或藍色)的信息。

做這件事,沒有顏色的賬戶人的感知一致,你可以這樣做:

public enum ColorSection 
{ 
    Red, 
    Green, 
    Blue 
} 

public ColorSection GetColorSection(int rgb) 
{ 
    int r = rgb & 0xFF; 
    int g = (rgb >> 8) & 0xFF; 
    int b = (rgb >> 16) & 0xFF; 

    return (r > g && r > b) ? ColorSection.Red : ((g > b) ? ColorSection.Green : ColorSection.Blue); 
} 

當然,這不很好地工作,如果你有兩個相等值的顏色,如果你有紅=綠色,它返回紅色。 如果您有白色,則會返回紅色,如果您有黑色,則會返回藍色。

如果你需要更多的東西,可能正如你問的那樣,那麼你需要使用最近鄰居近似和極座標。

您可以使用(如指出的那樣)使用Hue來計算角度。 從角度來看,你可以把它轉換成一個整數。

爲了計算色調:

public static double GetHue(int rgb) 
    { 
     double result = 0.0; 
     int r = rgb & 0xFF; 
     int g = (rgb >> 8) & 0xFF; 
     int b = (rgb >> 16) & 0xFF; 

     if (r != g && g != b) 
     { 
      double red = r/255.0; 
      double green = g/255.0; 
      double blue = b/255.0; 

      double max = Math.Max(Math.Max(red, green), blue); 
      double min = Math.Min(Math.Min(red, green), blue); 

      double delta = max - min; 
      if (delta != 0) 
      { 
       if (red == max) 
        result = (green - blue)/delta; 
       else if (green == max) 
        result = 2 + ((blue - red)/delta); 
       else if (blue == max) 
        result = 4 + ((red - green)/delta); 

       result *= 60.0; 
       if (result < 0) 
        result += 360.0; 
      } 
     } 

     return result; 
    } 

它返回角度,單位爲度,從0到360,的你在哪裏在色輪。 你可以計算比使用最近鄰居的部分。

例如:

const int NumberOfSections = 10; 
int section = (int)(GetHue() * NumberOfSections/360.0); 

你的車輪但是似乎對於C#色輪旋轉。 您可能需要減去完全不同的恆定角度。

如果我沒有錯,差異是180°。

public static double GetMyHue(int rgb) 
    { 
     return (GetHue(rgb) + 180.0) % 360.0; 
    } 
+0

完美thax很多! –

2

使用HSL顏色空間。 H是色調,即色圈上的顏色的角度。你可以直接從System.Drawing.Color.GetHue()方法獲取它。