How I can programmatically split the color circle into sections, and how I can get all R开发者_JAVA技巧GB color in this sections ?
I want fce which returns me the color section by input parament in RGB color.
int getColorSection(RGB color);
I don't know if i understood correctly the question, but i think you are asking if a color is more green, more blue or blue red? This will give you informations on in what section (Red, Green or Blue) it is.
For doing that, without keeping in account human perception of colors, you can do:
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);
}
Of course this don't work very well if you have colors with two equal values, if you have red = green, it returns red. If you have white it returns red, if you have black, it returns blue.
If you need something more fancy, probably as you are asking, then you need to use neirest neighbour approximation and polar coordinates.
You can compute the angle using, as pointed out, the Hue. From the angle then you can just convert it to an integer.
To compute the 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;
}
It returns the angle, in degrees, from 0 to 360, of where are you in the color wheel. You can compute than the section using neirest neighbour.
For example:
const int NumberOfSections = 10;
int section = (int)(GetHue() * NumberOfSections / 360.0);
Your wheel however seems rotated in respect to the C# color wheel. You probably need to substract a constant angle that is the exact difference.
If i'm not wrong, the difference is 180°.
public static double GetMyHue(int rgb)
{
return (GetHue(rgb) + 180.0) % 360.0;
}
Use the HSL color space. The H is the hue, the angle of the color on the color circle. You can get it directly from the System.Drawing.Color.GetHue() method.
精彩评论