Skip to main content
 首页 » 编程设计

C#反色

2024年02月24日22lexus

<分区>

Possible Duplicate:
How do I invert a colour?

是否可以为给定的 rgb 组合找到相反的颜色?例如,我在一个基本应用程序中有以下内容。

    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; 
    } 

但我希望能够更改标签的背景颜色,使其始终与背景颜色相反或相反。

在此先感谢,我希望这是有道理的。