2

I'm using the Brushes in my wpf application as shown below, what I want to do is to control the colour intensity. So as shown below the brush is set to Green colour. But what I want to do is to control the intensity of the green colour, so that sometimes it can be lighter or thicker, depending on the value I pass it. so if anyone could please advise.

private readonly Pen P = new Pen(Brushes.Green, 6);
Andy G
  • 19,232
  • 5
  • 47
  • 69
Tak
  • 3,536
  • 11
  • 51
  • 93

4 Answers4

3

You could create your own brush:

private readonly Pen p = new Pen(new SolidColorBrush(Color.FromARGB(...)), 6);

You can then define the color exactly by passing alpha, red, green and blue components to Color.FromARGB.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • 1
    Thanks for your answer! I'm asking if it is possible to set the intensity of the colour using a range. For example the range is from 1-->10 which represents the intensity of the colour where 1 is the lightest and 10 is the darkest. Then the value passed to the brush will be in this range. so if you could please advise, and please let me know if any more clarification needed – Tak Sep 06 '13 at 09:26
  • Would this question and the answers there help you? http://stackoverflow.com/questions/12894605/how-to-make-specific-color-darken-or-lighten-based-on-value-in-wpf – Thorsten Dittmar Sep 06 '13 at 09:52
  • I know the RGB values to pass it to the FromARGB, but I don't know what is the alpha channel – Tak Sep 09 '13 at 08:27
  • The value for the alpha channel determines the transparency from 0 (fully transparent) to 255 (fully opaque). – Thorsten Dittmar Sep 09 '13 at 11:09
  • Thanks for your reply. Actually what I'm trying to do is I want 45 degrees of green same for yellow, orange and red. I've tried `Brushes.LimeGreen Brushes.Chartreuse Brushes.GreenYellow' but there is not enough names for colour. What I need is to have very close colour degrees. For example if I have 45 degrees of green, the difference between each two degrees will be not much noticeable but the difference between the first and last will be obvious. I'm asking if you could please assist me as its fairly important for me – Tak Sep 09 '13 at 12:41
1

An easy solution would be to just manipulate the A-value of a green color like so:

int a = yourAValue;
private readonly Pen p = new Pen(new SolidColorBrush(Color.FromARGB(a,0,255,0)), 6);

Now you can change the colors intensity by changing the a-value;

Florian Gl
  • 5,984
  • 2
  • 17
  • 30
1

You could use the HSL colour model instead of RGB. RGB is good for displays as it is based upon mixing the three primary colours of light. However, it doesn't fit well with the human model.

HSL's elements are hue, saturation and lightness. This does fit with the way we describe colours in real life. Adjusting the hue changes the colour - keeping the hue the same and modifiying the other elements gives a colour that varies but that a person would see as being related.

Varying the saturation changes the deepness of the colour. A saturation of zero removes the colour and you end up with a greyscale. A higher saturation makes the colour more vivid. The lightness is what it says. A lightness of zero always gives black. A lightness of the maximum value is white. In between the two extremes are your colours.

Unfortunately HSL is not natively supported by WPF / .NET. However, it's quite easy to create your own method to convert between RGB and HSL. This would probably work well within a method or value converter in your case.

I wrote an article about the conversion on my web site. There's also a downloadable sample and source code for the conversion that you can use for free (you just have to promise not to sue me if something goes wrong!). It's at http://www.blackwasp.co.uk/RGBHSL.aspx

BlackWasp
  • 4,933
  • 2
  • 30
  • 42
0

Please see this blog post for an HSL Color implementation for WPF, and a XAML markup extension to lighten/darken colors.

ezolotko
  • 1,723
  • 1
  • 21
  • 21