Calculate color of selection brush in WPF

I noticed that when setting the text box selection in wpf to red and using a set of colors to check the color, the color faded to # FF9999.

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

Currently, I need the selection brush to be # 6599D1, but after setting it, the color is # C1D6ED.

How to calculate the initial color so that the final color is what I need?

+8
colors wpf textbox
source share
2 answers

In response to the answer of HB

I calculated opacity with the following formula in the past

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

which appeals to

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

TextBox has the default Background for white and SelectionOpacity set to 0.4.
As HB explained, you cannot achieve your color with these default settings, since the red value will be -130. This gives you 3 options, change Background , change SelectionOpacity or don’t do this :)

Changing the TextBox Background possible, not what you want to do to leave option 2, change SelectionOpacity

We do not want red to fall 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 SelectionBrush needs to be set to #0056B3 to become #6599D1 after applying opacity.

Here's what a TextBox looks like with these values

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

enter image description here

+3
source share

Good question, but I think this is not possible. If there is some overlay in the ControlTemplate, you cannot formulate a function that calculates a darker color, which then ends as the intended color.

eg. when you enter the red color which: 255,0,0 you get 255,153,153 , now the function that should be applied to your original color would be to darken the red color, this, of course, could only be done in the red channel as green and blue is already zero. However, the problem is not the red channel (which ends at 255 and therefore is not affected), so any changes in this will only discolor the color even more, instead of darkening it.

Edit: To make this a little more mathematical, the function used by selection transparency:

f (x) = 0.4x + 153

If you apply this to all the channels of your color, you will see that this is true. Now, how do we get the values ​​we need? Simple enough, invert the function, which is as follows:

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

Fine! Now apply this to your color:

R: -130.0
G: 0
B: 140

Hmm, not so good, I suppose.

This is a negative value precisely because it is not always possible; every color that has components below 153 is not reversible.

+2
source share

All Articles