tags:

views:

119

answers:

4
+1  Q: 

C# opposite color

Possible Duplicate:
How do I invert a colour?

Is it possible to find the opposing color for a given rgb combo? For example I have the following in a basic app.

    myColor red = new myColor(0, true), green = new myColor(0, true), blue = new myColor(0, true), theColor;
    Random rand = new Random(DateTime.Now.Millisecond);
    int color = 0;
    void timer_Tick(object sender, EventArgs e)
    {
        switch (color)
        {
            case 0: theColor = red; break;
            case 1: theColor = green; break;
            case 2: theColor = blue; break;
        }

        if (theColor.Up)
        {
            theColor.Value += 0.01F;
            if (theColor.Value + 0.01F > 1) { theColor.Up = false; color = rand.Next(3); }
        }
        else if (!theColor.Up)
        {
            theColor.Value -= 0.01F;
            if (theColor.Value - 0.01F < 0) { theColor.Up = true; color = rand.Next(3); }
        }
        label1.Text = red.Value + Environment.NewLine + green.Value + Environment.NewLine + blue.Value;
        FadeBackground(red.Value, green.Value, blue.Value);
    }
    void FadeBackground(float red, float green, float blue)
    {
        BackColor = Color.FromArgb((int)((1 - red) * 255), (int)((1 - green) * 255), (int)((1 - blue) * 255));
        label1.ForeColor = BackColor;
        //label1.BackColor;
    }
    class myColor
    {
        public myColor(float value, bool up)
        {
            Value = value;
            Up = up;
        }
        public float Value;
        public bool Up;
    }

But I'd like to be able to change the background color of the label so it's always the opposite, or an opposing, color of whatever the background is.

Thanks in advance and I hope this makes sense.

+3  A: 

Checkout this SO question. The accepted answer seems to be your ticket.

http://stackoverflow.com/questions/1165107/how-do-i-invert-a-colour

Caladain
+1  A: 

You can convert the RGB values into Hue, Saturation, Lighting (HSL) values, using the formula here. You then simply move the Hue around by 180 degrees, but leave the saturation and lighting alone. Finally, convert back.

This is a good guide.

Stephen
Now I'm honestly curious if this turns #888888 180 degrees and it lands on itself..
Jimmy Hoffa
Since here `var_Max` and `var_Min` are the same (88), it ends up with a HSL of `(0,0,88/255)`. Now, you would move the H around by 180 degrees, to 0.5, but this step is actually pointless, as due to the 0 saturation, the formula for converting back to RGB is just `R = L * 255, G = L * 255, B = L * 255`, and so you get back to `#888888`. So, yes, it does go to itself.... Oh. I just got that joke. That was a joke? Dammit, I wasted a few minutes typing up this comment! >=F
Stephen
A: 

The opposite color is White's RGB value less the Color's RGB value:

For example, if the color is 100-100-100 then the opposite is 155-155-155.

myermian
Check a colour wheel and you will see that this method does **not** always work. It only works for certain values. It's because RGB is not a properly circular representation. HSL is.
Stephen
+2  A: 
  1. You're going to need to convert your color to HSV. As seen in this SO question

  2. Get the opposite 180 degree hue value:

    hue = (hue + 0.5f) % 1.0f

  3. Convert your color back from HSV to RGB, which is answered in the above post as well.

George