Browse Source

Heavily simplify SCB animator

pull/5886/head
Jumar Macato 5 years ago
parent
commit
f807092877
No known key found for this signature in database GPG Key ID: B19884DAC3A5BF3F
  1. 7
      src/Avalonia.Visuals/Animation/Animators/ColorAnimator.cs
  2. 60
      src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs

7
src/Avalonia.Visuals/Animation/Animators/ColorAnimator.cs

@ -31,6 +31,11 @@ namespace Avalonia.Animation.Animators
}
public override Color Interpolate(double progress, Color oldValue, Color newValue)
{
return InterpolateCore(progress, oldValue, newValue);
}
internal static Color InterpolateCore(double progress, Color oldValue, Color newValue)
{
// normalize sRGB values.
var oldA = oldValue.A / 255d;
@ -59,7 +64,7 @@ namespace Avalonia.Animation.Animators
var b = oldB + progress * (newB - oldB);
// convert back to sRGB in the [0..255] range
a = a * 255d;
a *= 255d;
r = OECF_sRGB(r) * 255d;
g = OECF_sRGB(g) * 255d;
b = OECF_sRGB(b) * 255d;

60
src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs

@ -8,64 +8,14 @@ namespace Avalonia.Animation.Animators
/// <summary>
/// Animator that handles <see cref="SolidColorBrush"/>.
/// </summary>
public class SolidColorBrushAnimator : Animator<SolidColorBrush>
public class SolidColorBrushAnimator : Animator<IBrush>
{
private ColorAnimator _colorAnimator;
private void InitializeColorAnimator()
{
_colorAnimator = new ColorAnimator();
foreach (AnimatorKeyFrame keyframe in this)
{
_colorAnimator.Add(keyframe);
}
_colorAnimator.Property = SolidColorBrush.ColorProperty;
}
public override IDisposable Apply(Animation animation, Animatable control, IClock clock, IObservable<bool> match, Action onComplete)
public override IBrush Interpolate(double progress, IBrush oldValue, IBrush newValue)
{
// Preprocess keyframe values to Color if the xaml parser converts them to ISCB.
foreach (var keyframe in this)
{
if (keyframe.Value is ISolidColorBrush colorBrush)
{
keyframe.Value = colorBrush.Color;
}
else
{
return Disposable.Empty;
}
}
SolidColorBrush finalTarget;
var targetVal = control.GetValue(Property);
if (oldValue is not ISolidColorBrush oldValS || newValue is not ISolidColorBrush newValS)
return Brushes.Transparent;
switch (targetVal)
{
case null:
finalTarget = new SolidColorBrush(Colors.Transparent);
break;
case ImmutableSolidColorBrush immutableSolidColorBrush:
finalTarget = new SolidColorBrush(immutableSolidColorBrush.Color);
break;
case SolidColorBrush target:
finalTarget = target;
break;
case ISolidColorBrush target:
finalTarget = target as SolidColorBrush;
break;
default:
return Disposable.Empty;
}
if (_colorAnimator == null)
InitializeColorAnimator();
return _colorAnimator.Apply(animation, finalTarget, clock ?? control.Clock, match, onComplete);
return new ImmutableSolidColorBrush(ColorAnimator.InterpolateCore(progress, oldValS.Color, newValS.Color));
}
public override SolidColorBrush Interpolate(double p, SolidColorBrush o, SolidColorBrush n) => null;
}
}

Loading…
Cancel
Save