Set System.Drawing.Color values

C#.NetColorsImmutabilitysystem.drawing.color

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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

Solution 3 - C#

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

var newColor = Color.FromArgb(0xCC,0xBB,0xAA);

Solution 4 - C#

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

Solution 5 - C#

You could do:

Color c = Color.FromArgb(red, green, blue); //red, green and blue are integer variables containing red, green and blue components

Solution 6 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSudantha View Question on Stackoverflow
Solution 1 - C#Darin DimitrovView Answer on Stackoverflow
Solution 2 - C#Cody GrayView Answer on Stackoverflow
Solution 3 - C#Viacheslav SmityukhView Answer on Stackoverflow
Solution 4 - C#StecyaView Answer on Stackoverflow
Solution 5 - C#FIre PandaView Answer on Stackoverflow
Solution 6 - C#user2991535View Answer on Stackoverflow