How to convert color name to the corresponding hexadecimal representation?

前端 未结 5 1380
面向向阳花
面向向阳花 2020-12-19 13:56

For example:

blue 

converts to:

#0000FF

I wrote it as:

Color color = Color.FromName(\"blue\

5条回答
  •  不思量自难忘°
    2020-12-19 14:22

    Ahmed's answer is close, but based on your comment, I'll just add a little more.

    The code that should make this work is:

    Color color = Color.FromName("blue");
    string myHexString = String.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
    

    Now you can do whatever you want with the string myHexString.

提交回复
热议问题