From 7247d53d6dee65fe7cfbb0a4d1330881d5f0b6d6 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Wed, 11 Apr 2018 23:58:24 +0800 Subject: [PATCH] Keeping it simple, ColorTransition will now just use sRGB premultiplied space color interpolation. --- .../ColorInterpolationMode.cs | 33 --------- .../Animation/Transitions/ColorTransition.cs | 67 ++++--------------- 2 files changed, 12 insertions(+), 88 deletions(-) delete mode 100644 src/Avalonia.Animation/ColorInterpolationMode.cs diff --git a/src/Avalonia.Animation/ColorInterpolationMode.cs b/src/Avalonia.Animation/ColorInterpolationMode.cs deleted file mode 100644 index a9e31bf94f..0000000000 --- a/src/Avalonia.Animation/ColorInterpolationMode.cs +++ /dev/null @@ -1,33 +0,0 @@ -namespace Avalonia.Animation -{ - /// - /// Defines the intermediate color space - /// in which the color interpolation will be done. - /// - public enum ColorInterpolationMode - { - /// - /// Premultiply the alpha component and interpolate from RGB color space. - /// - PremultipliedRGB, - - /// - /// Directly interpolate from RGB color space. - /// Fastest but the least accurate, perception-wise. - /// - RGB, - - /// - /// Converts RGB into HSV color space and interpolates. - /// Slow but accurate, perception-wise. - /// - HSV, - - /// - /// Converts RGB into HSV color space and interpolates. - /// Slowest but the most accurate, perception-wise. - /// - LAB, - - } -} \ No newline at end of file diff --git a/src/Avalonia.Visuals/Animation/Transitions/ColorTransition.cs b/src/Avalonia.Visuals/Animation/Transitions/ColorTransition.cs index 4954d4eac9..1f1d8b2855 100644 --- a/src/Avalonia.Visuals/Animation/Transitions/ColorTransition.cs +++ b/src/Avalonia.Visuals/Animation/Transitions/ColorTransition.cs @@ -11,73 +11,30 @@ namespace Avalonia.Animation.Transitions { /// /// Transition class that handles with types. - /// TODO: Must be refactored to handle object creation. + /// TODO: Must be refactored to handle object creation every frame. /// public class ColorTransition : Transition { - - /// - /// Defines the property. - /// - public static readonly DirectProperty InterpolationModeProperty = - AvaloniaProperty.RegisterDirect( - nameof(InterpolationMode), - o => o.InterpolationMode, - (o, v) => o.InterpolationMode = v); - - /// - /// Gets or sets the state of the animation for this - /// control. - /// - public ColorInterpolationMode InterpolationMode - { - get { return _interpolationMode; } - set { SetAndRaise(InterpolationModeProperty, ref _interpolationMode, value); } - } - - private ColorInterpolationMode _interpolationMode; - public override IObservable DoTransition(IObservable progress, SolidColorBrush oldValue, SolidColorBrush newValue) { - var oldColor = new Vector4(oldValue.Color.R, oldValue.Color.G, oldValue.Color.B, oldValue.Color.A); var newColor = new Vector4(newValue.Color.R, newValue.Color.G, newValue.Color.B, oldValue.Color.A); oldColor = oldColor / 255f; newColor = newColor / 255f; var deltaColor = newColor - oldColor; + var premultOV = new Vector4(oldColor.W, oldColor.W, oldColor.W, 1); + var premultNV = new Vector4(newColor.W, newColor.W, newColor.W, 1); - switch (InterpolationMode) - { - case ColorInterpolationMode.PremultipliedRGB: - - var premultOV = new Vector4(oldColor.W, oldColor.W, oldColor.W, 1); - var premultNV = new Vector4(newColor.W, newColor.W, newColor.W, 1); - - oldColor *= premultOV; - newColor *= premultNV; - - return progress - .Select(p => - { - var time = (float)Easing.Ease(p); - var interpolatedColor = (deltaColor * time) + oldColor; - return new SolidColorBrush(Color.FromVector4(interpolatedColor, true)); - }); - - case ColorInterpolationMode.RGB: - return progress - .Select(p => - { - var time = (float)Easing.Ease(p); - var interpolatedColor = (deltaColor * time) + oldColor; - return new SolidColorBrush(Color.FromVector4(interpolatedColor, true)); - }); - default: - throw new NotImplementedException($"'{Enum.GetName(typeof(ColorInterpolationMode), InterpolationMode)}' color interpolation is not supported."); - } - + oldColor *= premultOV; + newColor *= premultNV; + return progress + .Select(p => + { + var time = (float)Easing.Ease(p); + var interpolatedColor = (deltaColor * time) + oldColor; + return new SolidColorBrush(Color.FromVector4(interpolatedColor, true)); + }); } - } }