开发者

calculate selection brush color in WPF

开发者 https://www.devze.com 2023-03-27 16:52 出处:网络
I have noticed when setting the selection of a text box in wpf to red, and using a color picker to verify the color, the color faded to #FF9999.

I have noticed when setting the selection of a text box in wpf to red, and using a color picker to verify the color, the color faded to #FF9999.

I have a specific color my client requires for the selection brush. Is there a way to calculate what color i should set the SelectionBrush to so when the text is selected, the exact color is displayed?

Currently I need to have the selection brush as #6599D1, but开发者_如何学JAVA once set, the color is #C1D6ED.

How can I calculate the starting color so the end color is what I need?


Following up on the answer by H.B.

I've calculated opacity with the following formula in the past

Red = OriginalRed * Opacity + (1-Opacity) * BackgroundRed

which inverts to

(Red - (1-Opacity) * BackgroundRed) / Opacity = OriginalRed

The TextBox has default Background set to White and SelectionOpacity set to 0.4.
As H.B. explained, you can't achieve your color with these default settings since the Red value will come out as -130. This leaves you with 3 options, Change Background, change SelectionOpacity or don't do it :)

Changing the TextBox Background probably isn't something you wanna do so that leaves option 2, change SelectionOpacity

We don't want Red to go below 0 so

(101 - (1-Opacity) * 255) = 0

which gives

1 - (101/255) = Opacity

This results in 0,604 so with this SelectionOpacity value you can calculate that the SelectionBrush needs to be set to #0056B3 to become #6599D1 once the Opacity has been applied.

Here's how the TextBox look with these values

<TextBox SelectionBrush="#0056B3"
         SelectionOpacity="0.604" />

calculate selection brush color in WPF


Good question but i think this is impossible. If there is some overlay inside the ControlTemplate you cannot formulate a function which calculates a darker colour which then will end up as the intended colour.

e.g. when you input red which is: 255,0,0 you get 255,153,153, now the function that would need to be applied to your initial colour would need to darken the Red, this of course could only be done in the red channel as green and blue are already zero. The problem is not the red channel however (which ends up as 255 and hence is not affected), so any changes to that will only serve to desaturate the colour even more instead of darkening it.

Edit: To make this a bit more mathmatical, the function that is applied by the transparency of the selection is:

f(x) = 0.4x + 153

If you apply this to all the channels of your colour you will see that this is indeed the case. Now how do we get the values we want? Quite simple, we invert the function, which is this:

f^(-1)(x) = -2.5(153.0 - x)

Great! Now lets apply that to your colour:

R: -130.0
G: 0
B: 140

Hmm, not so great after all i suppose.

This negative value is exactly why this is not always possible, every colour which has components below 153 is not reversible.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号