Reputation: 7218
Using C# I was trying to develop the following two. 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.
Upvotes: 157
Views: 220636
Reputation: 5534
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 c.R.ToString()
, c.G.ToString()
, and c.B.ToString()
can't actually fail (the only way I can see them failing is with a NullReferenceException
, and none of them can actually be null
).
You could clean the whole thing up using the following:
private static String ToHex(System.Drawing.Color c)
=> $"#{c.R:X2}{c.G:X2}{c.B:X2}"
private static String ToRGB(System.Drawing.Color c)
=> $"RGB({c.R},{c.G},{c.B})"
Upvotes: 259
Reputation: 23521
If you can use C#6 or higher, you can benefit from Interpolated Strings and rewrite @Ari Roth's solution like this:
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.string
instead of the class name.Edit: If you want to support the alpha channel:
public static class ColorConverterExtensions
{
// #RRGGBB
public static string ToHexString(this Color c) => $"#{c.R:X2}{c.G:X2}{c.B:X2}";
// RGB(R, G, B)
public static string ToRgbString(this Color c) => $"RGB({c.R}, {c.G}, {c.B})";
// #RRGGBBAA
public static string ToHexaString(this Color c) => $"#{c.R:X2}{c.G:X2}{c.B:X2}{c.A:X2}";
private static double ToProportion(byte b) => b / (double)Byte.MaxValue;
// RGBA(R, G, B, A)
public static string ToRgbaString(this Color c) => $"RGBA({c.R}, {c.G}, {c.B}, {ToProportion(c.A):N2})";
}
Fun fact: I have to search about the proportion name, because we want for a value in the interval [0, 1]
not a percent which is in the interval [0, 100]
Upvotes: 58
Reputation: 20387
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);
Upvotes: 218
Reputation: 186668
For hexadecimal code try this
& 0x00FFFFFF
For RGB one
Red
, Green
, Blue
valuesImplementation
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);
}
Upvotes: 4
Reputation: 18789
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"));
}
Upvotes: 3
Reputation: 428
e.g.
ColorTranslator.ToHtml(Color.FromArgb(Color.Tomato.ToArgb()))
This can avoid the KnownColor trick.
Upvotes: 34