Color​Animation Color​Animation Color​Animation Class

Definition

Animates the value of a Color property between two target values using linear interpolation over a specified Duration.

public : sealed class ColorAnimation : Timeline, IColorAnimationpublic sealed class ColorAnimation : Timeline, IColorAnimationPublic NotInheritable Class ColorAnimation Inherits Timeline Implements IColorAnimation
<ColorAnimation .../>
Inheritance
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Inherited Members

Inherited properties

Inherited methods

Inherited events

Examples

The following example shows how to use ColorAnimation to animate the background color of a StackPanel.

<StackPanel x:Name="myStackPanel" Background="Red"
  Loaded="Start_Animation">
  <StackPanel.Resources>
    <Storyboard x:Name="colorStoryboard">

      <!-- Animate the background color of the canvas from red to green
        over 4 seconds. -->
      <ColorAnimation Storyboard.TargetName="myStackPanel" 
        Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
        From="Red" To="Blue" Duration="0:0:4"/>

    </Storyboard>
  </StackPanel.Resources>
</StackPanel>
<StackPanel x:Name="myStackPanel" Background="Red"
Loaded="Start_Animation">
    <StackPanel.Resources>
        <Storyboard x:Name="colorStoryboard">

            <ColorAnimationUsingKeyFrames Storyboard.TargetName="myStackPanel" 
      Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                
                <!-- Go from green to red in the first 2 seconds. LinearColorKeyFrame creates
                a smooth, linear animation between values. -->
                <LinearColorKeyFrame Value="Blue" KeyTime="00:00:02" />

                <!-- In the next half second, go to yellow. DiscreteColorKeyFrame creates a 
                sudden jump between values. -->
                <DiscreteColorKeyFrame Value="Yellow" KeyTime="00:00:2.5" />

                <!-- In the final 2 seconds of the animation, go from yellow back to green. SplineColorKeyFrame 
                creates a variable transition between values depending on the KeySpline property. In this example,
                the animation starts off slow but toward the end of the time segment, it speeds up exponentially.-->
                <SplineColorKeyFrame Value="Green" KeyTime="00:00:4.5" KeySpline="0.6,0.0 0.9,0.00" />

            </ColorAnimationUsingKeyFrames>
        </Storyboard>
    </StackPanel.Resources>
</StackPanel>
<StackPanel Loaded="Start_Animation">
  <StackPanel.Resources>
    <Storyboard x:Name="colorStoryboard">
      <!-- Animate the background color of the canvas from red to green
        over 4 seconds. -->
      <ColorAnimation Storyboard.TargetName="mySolidColorBrush"
        Storyboard.TargetProperty="Color" From="Red" To="Blue" Duration="0:0:4"/>
    </Storyboard>
  </StackPanel.Resources>

  <StackPanel.Background>
    <SolidColorBrush x:Name="mySolidColorBrush" Color="Red" />
  </StackPanel.Background>

</StackPanel>
// Start the animation when the object loads.
private void Start_Animation(object sender, RoutedEventArgs e)
{
    colorStoryboard.Begin();
}
' Start the animation when the object loads
Private Sub Start_Animation(ByVal sender As Object, ByVal e As EventArgs)
    colorStoryboard.Begin()
End Sub
Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"

Alternatively, you could explicitly create the SolidColorBrush, name it, and target its Color property directly. The example below shows how to create the same animation as the previous one except it uses direct property targeting.

<StackPanel Loaded="Start_Animation">
  <StackPanel.Resources>
    <Storyboard x:Name="colorStoryboard">
      <!-- Animate the background color of the canvas from red to green
        over 4 seconds. -->
      <ColorAnimation Storyboard.TargetName="mySolidColorBrush"
        Storyboard.TargetProperty="Color" From="Red" To="Blue" Duration="0:0:4"/>
    </Storyboard>
  </StackPanel.Resources>

  <StackPanel.Background>
    <SolidColorBrush x:Name="mySolidColorBrush" Color="Red" />
  </StackPanel.Background>

</StackPanel>
// Start the animation when the object loads.
private void Start_Animation(object sender, RoutedEventArgs e)
{
    colorStoryboard.Begin();
}
' Start the animation when the object loads
Private Sub Start_Animation(ByVal sender As Object, ByVal e As EventArgs)
    colorStoryboard.Begin()
End Sub

Remarks

Use ColorAnimation to animate the property value of any dependency property that is of type Color.

Linear interpolation for a Color means that each of the ARGB values is treated as a byte and the interpolation is simply a mathematical operation. You get best results from color interpolation if at least one of the RGB components is the same or close to the same in both the starting value and ending value.

You usually need to use indirect property targeting in order to target a sub-property of another object that's the value of a property on the target. This is because very few properties that display color information in UI elements are actually of type Color. Most are instead of type Brush. To use ColorAnimation on UI elements, you typically are targeting the Color property of a SolidColorBrush that's the sub-property value. Syntax for this is shown in the XAML example in the "Examples" section. For more info on indirect property targeting and other storyboarded animation concepts, see Storyboarded animations or Property-path syntax.

A ColorAnimation typically has at least one of the From, By or To properties set, but never all three.

  • From only: The animation progresses from the value specified by the From property to the base value of the property being animated.
  • From and To: The animation progresses from the value specified by the From property to the value specified by the To property.
  • From and By: The animation progresses from the value specified by the From property to the value specified by the sum of the From and By properties.
  • To only: The animation progresses from the animated property's base value or a previous animation's output value to the value specified by the To property.
  • By only: The animation progresses from the base value of the property being animated or a previous animation's output value to the sum of that value and the value specified by the By property.

The From, By and To properties of a ColorAnimation aren't strictly a Color. Instead these are a Nullable for Color. The default value for these is null, not an uninitialized structure. That null value is how the animation system distinguishes that you haven't specifically set a value. Visual C++ component extensions (C++/CX) doesn't have a Nullable type, so it uses IReference instead.

Constructors

ColorAnimation() ColorAnimation() ColorAnimation()

Initializes a new instance of the ColorAnimation class.

public : ColorAnimation()public ColorAnimation()Public Sub New()
Attributes

Properties

By By By

Gets or sets the total amount by which the animation changes its starting value.

public : IReference<Color> By { get; set; }public Nullable<Color> By { get; set; }Public ReadWrite Property By As Nullable<Color>
<ColorAnimation By="colorString"/>
-or-
<ColorAnimation By="referenceToColor"/>
Value
IReference<Color> Nullable<Color> Nullable<Color>

The total amount by which the animation changes its starting value. The default is null.

If you are programming using C# or Visual Basic, the type of this property is projected as Color?(a nullable Color ).

Attributes

ByProperty ByProperty ByProperty

Identifies the By dependency property.

public : static DependencyProperty ByProperty { get; }public static DependencyProperty ByProperty { get; }Public Static ReadOnly Property ByProperty As DependencyProperty
Value
DependencyProperty DependencyProperty DependencyProperty

The identifier for the By dependency property.

Attributes

EasingFunction EasingFunction EasingFunction

Gets or sets the easing function applied to this animation.

public : EasingFunctionBase EasingFunction { get; set; }public EasingFunctionBase EasingFunction { get; set; }Public ReadWrite Property EasingFunction As EasingFunctionBase
<ColorAnimation>
  <ColorAnimation.EasingFunction>
    singleEasingFunction
  </ColorAnimation.EasingFunction>
</ColorAnimation>

Value
EasingFunctionBase EasingFunctionBase EasingFunctionBase

The easing function applied to this animation.

Attributes

Remarks

Easing functions allow you to apply custom mathematical formulas to your animations. Mathematical operations are often useful to produce animations that simulate real-world physics in a 2-D coordinate system. For example, you may want an object to realistically bounce or behave as though it were on a spring. For a list of easing functions and info on how to use them, see Key-frame animations and easing function animations.

See Also

EasingFunctionProperty EasingFunctionProperty EasingFunctionProperty

Identifies the EasingFunction dependency property.

public : static DependencyProperty EasingFunctionProperty { get; }public static DependencyProperty EasingFunctionProperty { get; }Public Static ReadOnly Property EasingFunctionProperty As DependencyProperty
Value
DependencyProperty DependencyProperty DependencyProperty

The identifier for the EasingFunction dependency property.

Attributes

EnableDependentAnimation EnableDependentAnimation EnableDependentAnimation

Gets or sets a value that declares whether animated properties that are considered dependent animations should be permitted to use this animation declaration.

public : PlatForm::Boolean EnableDependentAnimation { get; set; }public bool EnableDependentAnimation { get; set; }Public ReadWrite Property EnableDependentAnimation As bool
<ColorAnimation EnableDependentAnimation="bool" />
Value
PlatForm::Boolean bool bool

true if the animation can be used for a dependent animation case. false if the animation cannot be used for a dependent animation case. The default is false.

Attributes

Remarks

What's considered a dependent animation?

Not all custom animations you create can run by default in a Windows Runtime app, if the animation system determines that the animation might cause bad performance in your UI. Animations where the system determines there could be a performance impact are called dependent animations. It's dependent because your animation is actively and frequently updating objects on the UI thread, which is also where current user input and other programmatic updates are making runtime changes to UI.

A dependent animation that's consuming extensive system resources on the UI thread can make the app appear unresponsive in certain situations. If your animation causes a layout change or otherwise has the potential to impact performance on the UI thread, you often need to explicitly enable the animation to see it run. That's what the EnableDependentAnimation property on specific animation classes is for. Use this property with caution, because setting it to true means you are deliberately acknowledging that the animation might slow down other operations on the UI thread when it runs.

For more info, see Storyboarded animations. That topic includes a list of the criteria for an independent animation. An animation is a dependent animation if it doesn't satisfy at least one of those criteria. For the specific property you intend to animate, and for the specifics of your animation, compare your intended animation to the criteria to see whether it would be considered dependent or independent by the system.

Another way to discover whether your animations are dependent is that you might receive a warning from your XAML design surface or tool after you compose that animation, and the warning indicates that you'll need to set EnableDependentAnimation to true to see your animation run.

As an app developer, you can also choose to apply an app-wide setting that always disables dependent animations, even those where EnableDependentAnimation is true. See Timeline.AllowDependentAnimations. This is useful to you as an app developer if you're consuming controls where the templates have dependent animations, and you've identified that as a performance problem, but you don't want to have to retemplate the whole control to turn those animations off.

See Also

EnableDependentAnimationProperty EnableDependentAnimationProperty EnableDependentAnimationProperty

Identifies the EnableDependentAnimation dependency property.

public : static DependencyProperty EnableDependentAnimationProperty { get; }public static DependencyProperty EnableDependentAnimationProperty { get; }Public Static ReadOnly Property EnableDependentAnimationProperty As DependencyProperty
Value
DependencyProperty DependencyProperty DependencyProperty

The identifier for the EnableDependentAnimation dependency property.

Attributes

From From From

Gets or sets the animation's starting value.

public : IReference<Color> From { get; set; }public Nullable<Color> From { get; set; }Public ReadWrite Property From As Nullable<Color>
<ColorAnimation From="colorString"/>
-or-
<ColorAnimation From="referenceToColor"/>

Value
IReference<Color> Nullable<Color> Nullable<Color>

The starting value of the animation. The default is null.

If you are programming using C# or Visual Basic, the type of this property is projected as Color?(a nullable Color ).

Attributes

FromProperty FromProperty FromProperty

Identifies the From dependency property.

public : static DependencyProperty FromProperty { get; }public static DependencyProperty FromProperty { get; }Public Static ReadOnly Property FromProperty As DependencyProperty
Value
DependencyProperty DependencyProperty DependencyProperty

The identifier for the From dependency property.

Attributes

To To To

Gets or sets the animation's ending value.

public : IReference<Color> To { get; set; }public Nullable<Color> To { get; set; }Public ReadWrite Property To As Nullable<Color>
<ColorAnimation To="colorString"/>
-or-
<ColorAnimation To="referenceToColor"/>

Value
IReference<Color> Nullable<Color> Nullable<Color>

The ending value of the animation. The default is null.

If you are programming using C# or Visual Basic, the type of this property is projected as Color?(a nullable Color ).

Attributes

ToProperty ToProperty ToProperty

Identifies the To dependency property.

public : static DependencyProperty ToProperty { get; }public static DependencyProperty ToProperty { get; }Public Static ReadOnly Property ToProperty As DependencyProperty
Value
DependencyProperty DependencyProperty DependencyProperty

The identifier for the To dependency property.

Attributes

See Also