Question

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);
Was it helpful?

Solution

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.

OTHER TIPS

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;

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top