3

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.

CompuChip
  • 9,143
  • 4
  • 24
  • 48
mateos
  • 1,405
  • 1
  • 17
  • 26
  • Possible duplicate of [Set System.Drawing.Color values](http://stackoverflow.com/questions/6016456/set-system-drawing-color-values) – JNF Nov 26 '15 at 09:42
  • Eh formatting? `String.Format("{0:X2}{1:X2}{2:X2}", EndColor.Background.Red, EndColor.Background.Green, EndColor.Background.Blue);` – Dmitry Bychenko Nov 26 '15 at 09:43
  • @GurfX this [link](http://stackoverflow.com/questions/12160141/converting-system-windows-media-brush-to-hex-color-code) might help you – E.Solicito Nov 26 '15 at 09:52

3 Answers3

9

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();
Richard Irons
  • 1,433
  • 8
  • 16
  • Now for my control if I wanted to change something around, How would i make it do something like this `EndColor.Background = hex;` – mateos Nov 26 '15 at 10:00
  • You mean you want to use the hex string to set the colour of the visual component? If you really must, then you can write a small parser that throws away the hash symbol and then reads two characters for each of red, green and blue, and then constructs a colour for that. Don't do that though. `System.Drawing.Color` exists and that's how you should pass around colour information between the internals of your code. Write yourself a function that converts a `System.Drawing.Color` into a hex string (you could even make it an extension method), and use that whenever you need the string. – Richard Irons Nov 26 '15 at 10:03
  • Is it possible to do it all in one line? If so, do you mind writing it in the comments? – mateos Nov 26 '15 at 10:06
1

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]

simonalexander2005
  • 4,338
  • 4
  • 48
  • 92
  • Background Does not contain a definition named color – mateos Nov 26 '15 at 09:45
  • 2
    Try `(EndColor.Background as SolidColorBrush).Color.ToString()` – CompuChip Nov 26 '15 at 09:49
  • `Color.ToString()` does not return a string in the format '#FF0DBB'. It returns something like: `Color [A=255, R=255, G=13, B=187]` – Richard Irons Nov 26 '15 at 10:17
  • Me too, Also with this is there a way to remove the Alpha of the hex code because mine ends up with #FFFFFFFF if it is fully opaque and white – mateos Nov 26 '15 at 10:29
  • 1
    you can certainly do it manually - `if (hexcode.Text.Length > 7) hexcode.Text= hexcode.Text.Remove(1,2);` – simonalexander2005 Nov 26 '15 at 10:32
  • Very strange that we're getting different results from `Color.ToString`! Perhaps a good reason not to use it. – Richard Irons Nov 26 '15 at 11:57
  • This could explain it: https://msdn.microsoft.com/en-us/library/50cb8sdx(v=vs.110).aspx vs https://msdn.microsoft.com/en-us/library/ms606572(v=vs.110).aspx (System.Drawing vs. System.Windows.Media). As SolidColorBrush is part of the System.Windows.Media library, that one will be the .ToString() used - i.e. the hex representation. – simonalexander2005 Nov 26 '15 at 15:15
0

For this you can use ColorHelper library.

RGB to HEX:

using ColorHelper;
...
HEX hex = ColorHeConverter.RgbToHex(new RGB(10, 20, 30));

Links:

progm
  • 2,782
  • 3
  • 14
  • 32