C# color value from name
I need to get the RGB value of a color given it's name in C#. I am trying to use the predefined KnownColors enum, but can't figure out how to get the value.
Any he开发者_Go百科lp?
Thank you.
Use FromKnownColor
:
Color blue = Color.FromKnownColor(KnownColor.Blue);
Then blue.R
, blue.G
and blue.B
for the RGB values.
Or, if you just want the int
value for the RGB color, you can do:
int blueRgb = Color.FromKnownColor(KnownColor.Blue).ToArgb();
The Color class has some interesting static methods:
Color.FromName ("Red").ToArgb()
Next to that, there are some properties like:
var c = Color.FromName ("Red"); // Or use Color.FromKnownColor (KnownColor.Red)
Console.WriteLine (String.Format ("RGB: {0} {1} {2}", c.R, c.G, c.B);
Use Color.FromKnownColor
then access RGB values using Color.R
, Color.G
and Color.B
.
Color clr = FromKnownColor(System.Drawing.KnownColor.Blue);
string.Format("R:{0}, G:{1}, B:{2}" clr.R, clr.G, clr.B);
Check this Out
enter code here
You could do
int r = Color.FromName("Purple").ToArgb();
//Or
int r1 = Color.FromKnownColor(KnownColor.Purple).ToArgb();
Color.FromName and Color.FromKnownColor witll return Color object and it has properties for Red
, Green
and Blue
components if you want that.
Color c = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString("Red");
精彩评论