From f80709287715d96cf9e3c85493d168b228aec05a Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Sat, 8 May 2021 19:58:57 +0800 Subject: [PATCH] Heavily simplify SCB animator --- .../Animation/Animators/ColorAnimator.cs | 7 ++- .../Animators/SolidColorBrushAnimator.cs | 60 ++----------------- 2 files changed, 11 insertions(+), 56 deletions(-) diff --git a/src/Avalonia.Visuals/Animation/Animators/ColorAnimator.cs b/src/Avalonia.Visuals/Animation/Animators/ColorAnimator.cs index 6d1f6c39bd..1686cafeb9 100644 --- a/src/Avalonia.Visuals/Animation/Animators/ColorAnimator.cs +++ b/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; diff --git a/src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs b/src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs index 42962e219e..80eb13d2c4 100644 --- a/src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs +++ b/src/Avalonia.Visuals/Animation/Animators/SolidColorBrushAnimator.cs @@ -8,64 +8,14 @@ namespace Avalonia.Animation.Animators /// /// Animator that handles . /// - public class SolidColorBrushAnimator : Animator + public class SolidColorBrushAnimator : Animator { - 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 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; } }