denis morozov
denis morozov

Reputation: 6316

Can't produce a solid color by specifying directly R, G, B and A

I have a converter that determines colors based on some status items. I was going to tweak the colors, opacity etc, but can't even get the colors to match for starters.

Question: Why isn't the outcome of below 2 lines of code equivalent? I am going to use Green as an example. when I run the the first produces a normal solid green, the second is light green with definite opacity/see through:

return new SolidColorBrush(Colors.Green); 

return new SolidColorBrush{Color = new Color{A = 100, B = 0, G = 128, R = 0}};

As a test, when I create a Rectangle in Blend and fill it with Green:

<Rectangle Fill="Green" 

I see that R is 0, G is 128, B is 0 ,and A is 100%

Upvotes: 1

Views: 51

Answers (2)

Sheridan
Sheridan

Reputation: 69987

The answer is that the alpha channel input parameter is not in per-cent. You have to set it with a maximum value of 255 like this:

return new SolidColorBrush{Color = new Color{A = 255, B = 0, G = 255, R = 0}};

Upvotes: 2

Clemens
Clemens

Reputation: 128106

A is not a percentage, it is a byte value like R, G and B. You have to specify 255 for 100% opacity.

Upvotes: 2

Related Questions