How to convert color name to the corresponding hexadecimal representation?

前端 未结 5 1377
面向向阳花
面向向阳花 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:25

    var rgb = color.ToArgb() & 0xFFFFFF; // drop A component
    var hexString = String.Format("#{0:X6}", rgb);
    

    or just

    var hexString = String.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
    

提交回复
热议问题