2011-09-01 98 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()方法获取它。