84

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 only

Property or indexer 'System.Drawing.Color.G' cannot be assigned toit is read only

i just need to create a Color Object by assigning custom R G B values

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Sudantha
  • 15,684
  • 43
  • 105
  • 161
  • 1
    Note for others that come to this question, this problem only affects System.Drawing. If you're using WPF you should be using System.Windows.Media For more info see this post: http://stackoverflow.com/questions/2428930/what-is-the-difference-between-system-drawing-color-and-system-windows-media-col – HK1 Feb 18 '14 at 17:52

6 Answers6

155

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
13

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);
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
10

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

var newColor = Color.FromArgb(0xCC,0xBB,0xAA);
Viacheslav Smityukh
  • 5,652
  • 4
  • 24
  • 42
5

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);
Stecya
  • 22,896
  • 10
  • 72
  • 102
  • 4
    Yeah, but don't do this. It hides the fact that you're actually creating a new instance of the structure, very confusing for someone who doesn't already understand immutability. And fairly useless for someone who does. – Cody Gray - on strike May 16 '11 at 11:14
  • 1
    First of all you **NEED** to understand immutability. Code becomes uglier if you will write `yourColor = Color.FromArgb(yourColor.A,yourColor.R,100,yourColor.B);` all the time. So it is usefull – Stecya May 16 '11 at 11:18
  • 1
    Hard to understand what you think is ugly about code that's explicit. – Cody Gray - on strike May 16 '11 at 11:24
  • 1
    @CodyGray: A major problem with writing out everything is that it's hardly visually obvious whether the intention and/or effect of the code will be to merely change `G`, or whether it will have other effects as well. Given the design of the Color struct, I don't think there's any nice way to fix that; if there had been a simple `ArgbColor` struct with four exposed single-byte fields, and a `Color` class to which it was implicitly convertible, then `var temp = myColor.AsArgb(); temp.Green=100; myColor = temp;` would make clear that if e.g. `myColor` had been a named or system color... – supercat Nov 14 '13 at 18:09
  • 1
    ...that any such charateristics would be filtered out by the `AsArgb` method, but that any traits other than `Green` which had not been filtered out by that method would be retained. Actually, if one defined an extension method to convert to the open-field structure type, that approach might be workable even with `Color` as defined, and might be the best way to do things. – supercat Nov 14 '13 at 18:11
3

You could do:

Color c = Color.FromArgb(red, green, blue); //red, green and blue are integer variables containing red, green and blue components
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
FIre Panda
  • 6,537
  • 2
  • 25
  • 38
1
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());
        }
    }
user2991535
  • 119
  • 8