开发者

Color harmonies:triada, complement, analogous, monochromatic

I need help in color math. I have one main color and i need to get other colors of chosen harmony. I need such color harmonies: triada, complement, analogous, monochromatic. I need them in C#. Any help is appreciated. Th开发者_如何学Pythonanks, Dima.


Ok, everything revolves around the color wheel described in your link. I suggest hardcoding those colors in an array. I'm assuming your main color is always one of those 12.

One helper method we're going to need is one that will wrap values around the array, such that color -1 becomes color 12 (index 11 in the array):

int WrapColor(int colorIndex, int numWheelColors)
{
    while(colorIndex < 0)
    {
        colorIndex += numWheelColors;
    }

    colorIndex = colorIndex % numWheelColors;
}

We also need a helper method that will get the index of a color on the color wheel:

int GetColorWheelIndex(Color color)
{
    if (ColorWheelArray.Contains(color))
        return ColorWheelArray.IndexOf(color);
    else
        throw new InvalidArgumentException("color");
}

Now everything's in place (assuming you've got an array called ColorWheelArray, containing the colors in order).

Triada:

Color[] GetTriadaColors(Color color)
{
    int colorIndex = GetColorWheelIndex(color, ColorWheelArray.Length);
    return new Color[]
        {
            color,
            ColorWheelArray[WrapColor(colorIndex + ColorWheelArray.Length / 3)],
            ColorWheelArray[WrapColor(colorIndex + 2 * ColorWheelArray.Length / 3)]
        };
}

Compliment:

Color GetComplimentColor(Color color)
{
    int colorIndex = GetColorWheelIndex(color, ColorWheelArray.Length);
    return ColorWheelArray[WrapColor(colorIndex + ColorWheelArray.Length / 2)];
}

Analogous:

Color[] GetAnalogousColors(Color color)
{
    int colorIndex = GetColorWheelIndex(color, ColorWheelArray.Length);
    return new Color[] { color,
                         ColorWheelArray[WrapColor(colorIndex + 1)],
                         ColorWheelArray[WrapColor(colorIndex + 2)] };
}

As i don't know the definition of monochromatic i'll leave that to you. :)


EDIT: If you want it to work with any color then i'm not 100% sure, however i've got an idea.

That site says the wheel is created by picking colors in the RYB color space (not the RGB one that C# uses). So presumably you could work out how 'far' your color is to each of the colors on the wheel (by converting both to RYB and comparing), then use my functions to get the other color(s). Finally add on the difference between your color and the closest wheel color (in RYB color space) to each result, finally translating back into RGB to store as a Color object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜