Set System.Drawing.Color values

后端 未结 6 1876
耶瑟儿~
耶瑟儿~ 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: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());
            }
        }
    

提交回复
热议问题