2

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.

Community
  • 1
  • 1
Nyight
  • 211
  • 1
  • 7
  • 15
  • Opposite color? What's the opposite of #888888 ? FFFFFF and 00000 are equally distant from it... I don't really know what an opposite color is.. – Jimmy Hoffa Jul 30 '10 at 20:11
  • You want to make sure you can read the label no matter what color the background is, right? – Justin Jul 30 '10 at 20:12

3 Answers3

4
  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.

Community
  • 1
  • 1
George Johnston
  • 29,901
  • 26
  • 117
  • 169
1

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
  • 5,659
  • 3
  • 33
  • 52
  • Now I'm honestly curious if this turns #888888 180 degrees and it lands on itself.. – Jimmy Hoffa Jul 30 '10 at 20:15
  • 1
    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 Jul 30 '10 at 21:00
0

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
  • 29,999
  • 21
  • 111
  • 199
  • 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 Jul 30 '10 at 20:14