简体   繁体   中英

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 help?

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

使用Color.FromKnownColor然后使用Color.RColor.GColor.B访问RGB值。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM