Take the 2-minute tour ×

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 once set, the color is #C1D6ED.

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

share|improve this question
Can you give the code you used to set the color? –  Jakub Aug 11 '11 at 16:11
I suspect that your color is being made semitransparent, and you need to make it opaque. Without seeing how you're changing the color, I don't have an easy way to suggest more than that. –  Joe White Aug 11 '11 at 16:46
Doesn't help if you're trying to do a highly custom style, but I tend to prefer to leave the system default colors, and when creating custom controls, borrow system default colors from similar contexts. This is helpful for compatibility with Window's accessibility modes, and makes it so users can use window's theme capabilities without causing nasty visual clashing. –  Merlyn Morgan-Graham Aug 12 '11 at 0:55

2 Answers 2

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" />

enter image description here

share|improve this answer
Interesting idea, but the text admittedly does become significatly less readable at such high values. –  H.B. Aug 12 '11 at 0:39
@H.B.: It sure does, the whole TextBox looks a little bit disorted or blurry in the Image (it's a little bit better in the application) but yes, it's sure harder to read –  Fredrik Hedblad Aug 12 '11 at 0:43
Nevermind my last comment.. it looked blurry on my Windows 7 computer but from another computer it looks sharp... –  Fredrik Hedblad Aug 12 '11 at 19:16
Probably the clear type settings or something like that. –  H.B. Aug 12 '11 at 19:18

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.

share|improve this answer

Your Answer


