[英]Convert System.Drawing.Color to RGB and Hex Value
Using C# I was trying to develop the following two.使用 C# 我试图开发以下两个。 The way I am doing it may have some problem and need your kind advice.
我这样做的方式可能有一些问题,需要您的善意建议。 In addition, I dont know whether there is any existing method to do the same.
另外,我不知道是否有任何现有的方法可以做到这一点。
private static String HexConverter(System.Drawing.Color c)
{
String rtn = String.Empty;
try
{
rtn = "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
}
catch (Exception ex)
{
//doing nothing
}
return rtn;
}
private static String RGBConverter(System.Drawing.Color c)
{
String rtn = String.Empty;
try
{
rtn = "RGB(" + c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString() + ")";
}
catch (Exception ex)
{
//doing nothing
}
return rtn;
}
Thanks.谢谢。
I'm failing to see the problem here.我没有看到这里的问题。 The code looks good to me.
代码对我来说看起来不错。
The only thing I can think of is that the try/catch blocks are redundant -- Color is a struct and R, G, and B are bytes, so c can't be null and cRToString()
, cGToString()
, and cBToString()
can't actually fail (the only way I can see them failing is with a NullReferenceException
, and none of them can actually be null).我唯一能想到的就是 try/catch 块是多余的——颜色是一个结构体,R、G 和 B 是字节,所以 c 不能为 null 和
cRToString()
、 cGToString()
和cBToString()
实际上不能失败(我看到它们失败的唯一方法是使用NullReferenceException
,并且它们实际上都不能为空)。
You could clean the whole thing up using the following:您可以使用以下方法清理整个内容:
private static String HexConverter(System.Drawing.Color c)
{
return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
}
private static String RGBConverter(System.Drawing.Color c)
{
return "RGB(" + c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString() + ")";
}
You could keep it simple and use the native color translator:您可以保持简单并使用本机颜色转换器:
Color red = ColorTranslator.FromHtml("#FF0000");
string redHex = ColorTranslator.ToHtml(red);
Then break the three color pairs into integer form:然后将三个颜色对分解为整数形式:
int value = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
If you can use C#6 or higher, you can benefit from Interpolated Strings and rewrite @Ari Roth's solution like this:如果您可以使用 C#6 或更高版本,则可以从Interpolated Strings 中受益,并像这样重写@Ari Roth 的解决方案:
C# 6 : C# 6:
public static class ColorConverterExtensions
{
public static string ToHexString(this Color c) => $"#{c.R:X2}{c.G:X2}{c.B:X2}";
public static string ToRgbString(this Color c) => $"RGB({c.R}, {c.G}, {c.B})";
}
Also:还:
this
to use them as extensions methods.this
以将它们用作扩展方法。string
instead of the class name.string
而不是类名。eg例如
ColorTranslator.ToHtml(Color.FromArgb(Color.Tomato.ToArgb()))
This can avoid the KnownColor trick.这可以避免已知颜色技巧。
I found an extension method that works quite well我发现了一个非常有效的扩展方法
public static string ToHex(this Color color)
{
return String.Format("#{0}{1}{2}{3}"
, color.A.ToString("X").Length == 1 ? String.Format("0{0}", color.A.ToString("X")) : color.A.ToString("X")
, color.R.ToString("X").Length == 1 ? String.Format("0{0}", color.R.ToString("X")) : color.R.ToString("X")
, color.G.ToString("X").Length == 1 ? String.Format("0{0}", color.G.ToString("X")) : color.G.ToString("X")
, color.B.ToString("X").Length == 1 ? String.Format("0{0}", color.B.ToString("X")) : color.B.ToString("X"));
}
Ref: https://social.msdn.microsoft.com/Forums/en-US/4c77ba6c-6659-4a46-920a-7261dd4a15d0/how-to-convert-rgba-value-into-its-equivalent-hex-code?forum=winappswithcsharp参考: https : //social.msdn.microsoft.com/Forums/en-US/4c77ba6c-6659-4a46-920a-7261dd4a15d0/how-to-convert-rgba-value-into-its-equivalent-hex-code?论坛=winappswithcsharp
For hexadecimal code try this对于十六进制代码试试这个
& 0x00FFFFFF
& 0x00FFFFFF
For RGB one对于 RGB 一
Red
, Green
, Blue
valuesRed
, Green
, Blue
值Implementation执行
private static string HexConverter(Color c) {
return String.Format("#{0:X6}", c.ToArgb() & 0x00FFFFFF);
}
public static string RgbConverter(Color c) {
return String.Format("RGB({0},{1},{2})", c.R, c.G, c.B);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.