Set System.Drawing.Color values

后端 未结 6 1852
耶瑟儿~
耶瑟儿~ 2021-02-04 23:13

Hi how to set R G B values in System.Drawing.Color.G ?

which is like System.Drawing.Color.G=255; is not allowed because its read o

相关标签:
6条回答
  • 2021-02-04 23:25

    You could create a color using the static FromArgb method:

    Color redColor = Color.FromArgb(255, 0, 0);
    

    You can also specify the alpha using the following overload.

    0 讨论(0)
  • 2021-02-04 23:32

    The Color structure is immutable (as all structures should really be), meaning that the values of its properties cannot be changed once that particular instance has been created.

    Instead, you need to create a new instance of the structure with the property values that you want. Since you want to create a color using its component RGB values, you need to use the FromArgb method:

    Color myColor = Color.FromArgb(100, 150, 75);
    
    0 讨论(0)
  • 2021-02-04 23:37

    You can make extension to just change one color component

    static class ColorExtension
    {
        public static Color ChangeG(Color this color,byte g) 
        {
            return Color.FromArgb(color.A,color.R,g,color.B);
        }
    }
    

    then you can use this:

      yourColor = yourColor.ChangeG(100);
    
    0 讨论(0)
  • 2021-02-04 23:41

    You could do:

    Color c = Color.FromArgb(red, green, blue); //red, green and blue are integer variables containing red, green and blue components
    
    0 讨论(0)
  • 2021-02-04 23:42
    using System;
    using System.Drawing;
    public struct MyColor
        {
            private byte a, r, g, b;        
            public byte A
            {
                get
                {
                    return this.a;
                }
            }
            public byte R
            {
                get
                {
                    return this.r;
                }
            }
            public byte G
            {
                get
                {
                    return this.g;
                }
            }
            public byte B
            {
                get
                {
                    return this.b;
                }
            }       
            public MyColor SetAlpha(byte value)
            {
                this.a = value;
                return this;
            }
            public MyColor SetRed(byte value)
            {
                this.r = value;
                return this;
            }
            public MyColor SetGreen(byte value)
            {
                this.g = value;
                return this;
            }
            public MyColor SetBlue(byte value)
            {
                this.b = value;
                return this;
            }
            public int ToArgb()
            {
                return (int)(A << 24) || (int)(R << 16) || (int)(G << 8) || (int)(B);
            }
            public override string ToString ()
            {
                return string.Format ("[MyColor: A={0}, R={1}, G={2}, B={3}]", A, R, G, B);
            }
    
            public static MyColor FromArgb(byte alpha, byte red, byte green, byte blue)
            {
                return new MyColor().SetAlpha(alpha).SetRed(red).SetGreen(green).SetBlue(blue);
            }
            public static MyColor FromArgb(byte red, byte green, byte blue)
            {
                return MyColor.FromArgb(255, red, green, blue);
            }
            public static MyColor FromArgb(byte alpha, MyColor baseColor)
            {
                return MyColor.FromArgb(alpha, baseColor.R, baseColor.G, baseColor.B);
            }
            public static MyColor FromArgb(int argb)
            {
                return MyColor.FromArgb(argb & 255, (argb >> 8) & 255, (argb >> 16) & 255, (argb >> 24) & 255);
            }   
            public static implicit operator Color(MyColor myColor)
            {           
                return Color.FromArgb(myColor.ToArgb());
            }
            public static implicit operator MyColor(Color color)
            {
                return MyColor.FromArgb(color.ToArgb());
            }
        }
    
    0 讨论(0)
  • 2021-02-04 23:48

    You must use Color.FromArgb method to create new color structure

    var newColor = Color.FromArgb(0xCC,0xBB,0xAA);
    
    0 讨论(0)
提交回复
热议问题