• Advertisement
Sign in to follow this  

Changing colors of a ID2D1Linear/RadialGradientBrush at runtime

This topic is 1257 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

if you had a look at my devjournal, I'm trying to build an animatable UI framework. I'm going to use C# terminology because I'm using D2D from SharpDX but it shoud apply to C++ just the same. So, every property that can be written to such as the Opacity of a gradient or its RadiusX/Y (in the case of a Radial gradient) can be animated just fine. 


I'd like to implement the possibility of animating the colors of a Gradient Brush. Once you create a gradient brush, it seems you can only get a copy of the GradientStops you used returned to you through the GetGradientStops method. Since a GradientStop is a structure, if I were to change their colors, it would have no effect on the actual brush being used.


This seems to be a big limitation, so before going any further I'd like to know if someone has attempted to do anything similar. Is there really no other way? The Color property of a SolidColorBrush can be written to, so that could be animated over time. How to do the same with a GradientBrush? Is the only way to do it to create intermediate gradients so that instead of animating the colors I change the whole brush? For a decent animation, you could need to create dozens of slightly different brushes and that seems terribly inefficient.


I have two alternative approaches in mind:

  1. Have the source and target gradient one on top of the other and then animate their opacity so that one goes from 0 to 1 and the other from 1 to 0 in the same interval of time. Though I'm not sure the effect would be the same.
  2. Have a greyscale gradient and animate a blended solidcolorbrush on top of it.

But these seem more like hacks, so I was wondering if there was a better way to do this.

Edited by AvengerDr

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement