How to create a System.Drawing.Color from its hexadecimal RGB string?
ColorTranslator.FromHtml("#FF00FF");
Convert System.Drawing.Color to RGB and Hex Value
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 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() + ")";
}
How to convert Hexadecimal #FFFFFF to System.Drawing.Color
string hex = "#FFFFFF";
Color _color = System.Drawing.ColorTranslator.FromHtml(hex);
Note: the hash is important!
Get System.Drawing.Color value as ARGB Hex code
The ARGB Hex code may be obtained from the Name
property for known colors by converting it to ARGB and back to System.Drawing.Color
struct:
using System.Drawing;
var color = Color.Red;
color = Color.FromArgb(color.ToArgb());
Console.WriteLine(color.Name);
output: ffff0000
How do I get the color from a hexadecimal color code using .NET?
I'm assuming that's an ARGB code... Are you referring to System.Drawing.Color
or System.Windows.Media.Color
? The latter is used in WPF for example. I haven't seen anyone mention it yet, so just in case you were looking for it:
using System.Windows.Media;
Color color = (Color)ColorConverter.ConvertFromString("#FFDFD991");
System.Drawing.Color convert FromArgb issue
Try this
System.Drawing.ColorTranslator.FromHtml("#" + "Hex value").Name
Convert Drawing.Color into HTML color value
You can't return a System.Drawing.Color
object from your function because browsers only understand text. So instead, you should return the string representation of the color, be in RGB, HEX format or what have you.
Your method then should look like this:
protected string GetRandColor()
{
return ColorTranslator.ToHtml(Color.Red);
}
And you can set the background of your form as so:
<body style="background-color:<%=GetRandColor()%>;">
Converting RGB/Brush to Hexadecimal value
Firstly let me point out that, assuming that your sliders' value
property returns an int, you're converting an int
to a string
and then back again. This is not necessary. Instead of
byte r = byte.Parse(sliderRed.Value.ToString());
all you need to do is
byte r = (byte)sliderRed.Value;
This bypasses the string conversion. Converting something to a string and then converting it back from a string to something else is a code smell that should make you stop and think if there isn't a better way.
To turn the colour into its hex code is easy, because you already have the R, G and B values. All you need is:
hexCode.Text = string.Format("#{0:X2}{1:X2}{2:X2}", r, g, b);
Formatting a number with the format string "X2"
forces it to render in hexadecimal, with 2 digits. So you just do that for all three next to each other, and stick the hash symbol at the front.
edit
If you're passing colour data around between parts of your code, you should always do that with a System.Drawing.Color
object, and then whenever you need to display a hex string, generate that at the time. Don't pass around the hex string and convert it back to a Color
when it's needed. Remember how I said converting things to strings and back again was a code smell?
If you find you're doing it a lot then it makes sense to add an extension method to Color
so that you can call it easily. Here's a class that implements that method:
static class ColorExtensions
{
public static string ToHexString(this System.Drawing.Color color)
{
return string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
}
}
This will give all Color
values a ToHexString()
method, which in your code above you could use as follows:
var color = Color.FromArgb(255, r, g, b);
EndColor.Background = new SolidColorBrush(color);
hexcode.Text = color.ToHexString();
Related Topics
How to "Flatten" or "Index" 3D-Array in 1D Array
Wpf Binding a Listbox to an Enum, Displaying the Description Attribute
Recursive Hierarchy - Recursive Query Using Linq
Why Was "Switchto" Removed from Async Ctp/Release
Very Slow Compile Times on Visual Studio 2005
What Is the Correct Performance Counter to Get CPU and Memory Usage of a Process
Regex: I Want This and That and That... in Any Order
Set Up Dot Instead of Comma in Numeric Values
How to Get and Set the Window Position of Another Application in C#
Linq to SQL and a Running Total on Ordered Results
Why Is Array.Length an Int, and Not an Uint
An Attempt Was Made to Access a Socket in a Way Forbidden by Its Access Permissions
How to Have an Enum Bound Combobox with Custom String Formatting for Enum Values