mateos
mateos

Reputation: 1563

Converting RGB/Brush to Hexadecimal value

So, In my program I have 3 sliders, SliderRed, SliderGreen, Slider Blue. They all have a maximum value of 255. The control called EndColor changes colors correctly when I move the sliders but I haven't figured out a way to get the hexcode.text (Textblock) to convert the brush or color into a hexadecimal value such as #FF0000.

What should I be using for this to work?

public void SliderChanged()
{
    byte r = byte.Parse(sliderRed.Value.ToString());
    byte g = byte.Parse(sliderGreen.Value.ToString());
    byte b = byte.Parse(sliderBlue.Value.ToString());

    EndColor.Background = new SolidColorBrush(Color.FromArgb(255, r, g, b));
    hexcode.Text = EndColor.Background.ToString(); //Something like this
}

All I need is the hexcode.Text to show a hexadecimal value.

Upvotes: 3

Views: 3241

Answers (2)

simonalexander2005
simonalexander2005

Reputation: 4577

hexcode.Text = ((SolidColorBrush)(EndColor.Background)).Color.ToString(); should do it.

Note that the Color.ToString() used here is the System.Windows.Media.Color.ToString() implementation (because SolidColorBrush is part of the System.Windows.Media classes.

System.Drawing.Color.ToString() will give different results - see see https://msdn.microsoft.com/en-us/library/50cb8sdx(v=vs.110).aspx [Drawing] vs https://msdn.microsoft.com/en-us/library/ms606572(v=vs.110).aspx [Media]

Upvotes: 1

Richard Irons
Richard Irons

Reputation: 1473

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();

Upvotes: 10

Related Questions