15

I am trying to add color in c# code, with the following color code for example.

ListTreeView.Background = new SolidColorBrush(Colors.White);

This is working..but I want to add this color as color code so I am add as

System.Windows.Media

Could someone give me an example with

System.Drawing

So what I can do the following:

ListTreeView.Background = ColorTranslator.FromHtml("#FFE7EFF2");

This gives me error ; Any Ideas?

Iain
  • 6,392
  • 2
  • 30
  • 50
Jitendra Jadav
  • 603
  • 3
  • 10
  • 20
  • Is this WinForms or WPF? – Dan Puzey Jun 18 '10 at 10:49
  • Is that code right? As @ho1 states `ColorTranslator.FromHtml("#E7EFF2")` will work for an RGB hex value - but you have an 8 digit string. Do you have the alpha value in there too? – Keith Jun 18 '10 at 10:53
  • This is fully WPF, but still it is not working. see the whole code.. tlvi.Background = System.Drawing.ColorTranslator.FromHtml("#FFE7EFF2"); – Jitendra Jadav Jun 19 '10 at 05:20

4 Answers4

21

There isn't an easy way to get the colour with alpha included from a hex string in this way.

I think your answer depends on where you're getting the colour and alpha values from.

The RGB colour alone can be parsed from an HTML hex string:

Color colour = ColorTranslator.FromHtml("#E7EFF2");

If you have a separate alpha value you can then apply this (docs):

Color colour = ColorTranslator.FromHtml("#E7EFF2");
Color transparent = Color.FromArgb(128, colour);

Alternatively you may need to parse the string and split it out to convert the hex pairs into integer values.

PS excuse English spelling, but colour should definitely have a 'u' in it :)

Keith
  • 150,284
  • 78
  • 298
  • 434
  • so is armor/armour, defense/defence, grey/gray etc. the point gets across tho. but i wonder, since wpf has its own "fromArgb"function but it somehow doesnt use the correct alpha setting? why – ThrowingDwarf May 11 '16 at 06:48
  • 1
    @ThrowingDwarf .Net's `Color` components aren't very good. There are lots of basic functions missing and bugs like `GetBrightness()` returning the luminosity/lightness (0 is black, 1 is white) rather than brightness (0 still black, but 1 is the colour at full intensity) http://stackoverflow.com/questions/24732836 – Keith May 11 '16 at 06:59
  • yes but for example if i get the hex generated from the color selector (#FFFBB510) its goldish yellow (rgb 251,181,16) full alpha. color converters like hexcolortool.com doesn't even accept is as a valid color. and NEITHER does wpf accept this hex, well as far as ive experienced. i ended up using "newBtn.Background = new SolidColorBrush(Color.FromArgb(255,251,181,16));" – ThrowingDwarf May 11 '16 at 08:26
  • if only they would add the ability to use a CSS stylesheet smh – ThrowingDwarf May 11 '16 at 08:27
7

ListTreeView isn't a standard Control provided by the framework, so you'll have to consult their documentation. In general, though, you can use System.Drawing.ColorTranslator.FromHtml or System.Drawing.Color.FromArgb. Here's how you'd do it with a TreeView:

TreeView t = ...
t.BackgroundColor = Color.FromArgb(0xff00ff00); // Fully opaque, 100% green.
// or:
t.BackgroundColor = ColorTranslator.FromHtml("green");
John Feminella
  • 303,634
  • 46
  • 339
  • 357
2

I think you could use System.Drawing.ColorTranslator.FromHtml.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • This isn't the right way to do it if you want (A)RGB values, which is apparently what he's after. `FromHtml` will throw an exception if you use anything but an HTML color. – John Feminella Jun 18 '10 at 10:52
  • @John: Sorry, don't know what you're talking about. `Color c = System.Drawing.ColorTranslator.FromHtml("#FFE7EFF2")` works fine and if I then do `button1.BackColor = c;` it'll make the button some blueish colour. Are you saying that it's the wrong colour? – Hans Olsson Jun 18 '10 at 10:57
  • @ho1: When I use "#FFE7EFF2" with ColorTranslator, it's exactly the same color as "#00E7EFF2" (which should be invisible since the alpha channel is zero, right?). – John Feminella Jun 18 '10 at 11:03
  • @John: Don't know, I don't know much about graphics and colours, just confused by your comment about an exception. – Hans Olsson Jun 18 '10 at 11:16
  • see things is that I am trying to add background color into the ListTreeView so ListTreeView.Background =System.Drawing.ColorTranslator.FromHtml("#00E7EFF2"); it will give error so what i can do.. – Jitendra Jadav Jun 19 '10 at 05:25
  • @Jitendra: First thing you need to do is to actually post what the error is. Since `ListTreeView` isn't a standard control (as far as I know) I can't try to replicate the error since I don't know what code you're using, the error message might be enough for someone to know what the problem is but it would also be useful if you could say where you got the `ListTreeView` from? Is it a control you've bought or something you've made yourself or from some article on codeproject etc? – Hans Olsson Jun 19 '10 at 21:32
  • ya this is my own control so I had made this control and I am using this control... for eg.. I had made a class for ListTreeView and i am use this control every where.. so tell me what to do... – Jitendra Jadav Jun 21 '10 at 10:08
  • ColorTranslator.FromHtml() works with both a 6 char hex value and an 8 char hex value. This should have been selected as the correct answer. – Flipster Jun 06 '13 at 23:01
0

Use the Color.FromArgb method.

Btw, shouldn't it be Treeview.BackColor instead?

Grz, Kris.

Kris van der Mast
  • 16,343
  • 8
  • 39
  • 61