I have a gradient that changes its colors, I want the text inside it should always be visible.
I rather doing it dynamically if there is any out-the-box resource; I want a ‘magic brush’ that negates the color.
Any experiments?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well, color inversion could possibly be done as a bitmap effect, but there’s a simpler way.
Make a
Gridthat will be the container for 3 child panels so that these child panels will overlap each other completely:Put the text where you want it in a panel that has a
Transparentbackground (they do by default). Name this panel ‘mask’.Make another panel called ‘mainbackground’ and give it the main gradient as its background. Put this after the ‘mask’ panel so that it covers the text
Make another panel called ‘invertedforeground’ and give it the opposite gradient. For each color value in the main gradient, give this one the opposite (e.g., if one color is
#FF0000, put#00FFFF). You could animate this gradient just as you can animate the first, just with opposite values. Then you set theOpacityMaskof this panel to aVisualBrushand set theVisualBrushes‘sVisualproperty to{Binding ElementName=mask}.You could probably use binding and value converters so that you only need to animate one gradient and the other simply follows.
Edit: I tried setting an inverted Foreground brush for the text, but it would stick to the
TextBlock‘s coordinates, so I reverted to the previous solution of using the text as anOpacityMask.Edit 2: I added example usage of a custom
IValueConverterand binding the text gradient’s colors to the original gradient’s. You could also use binding and a converter somewhere higher up, such as bindinginvertedforeground‘sBackgroundproperty tomainbackground‘sBackgroundproperty and the converter takes the input gradient brush and returns a different gradient brush (this allows you to create a gradient with a much different configuration as the original).