tangxu
2024-10-24 c7a67ab24f476b0f8593fc61362d3bb8b262aa3e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#region Imports
 
using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Animate.Poison
{
    #region ColorBlendAnimationAnimate
 
    public sealed class ColorBlendAnimation : Animate
    {
        private double percent = 1;
 
        public void Start(Control control, string property, Color targetColor, int duration)
        {
            if (duration == 0)
            {
                duration = 1;
            }
 
            Start(control, transitionType, 2 * duration,
                delegate
                {
                    Color controlColor = GetPropertyValue(property, control);
                    Color newColor = DoColorBlend(controlColor, targetColor, 0.1 * (percent / 2));
 
                    PropertyInfo prop = control.GetType().GetProperty(property);
                    MethodInfo method = prop.GetSetMethod(true);
                    method.Invoke(control, new object[] { newColor });
                },
                delegate
                {
                    Color controlColor = GetPropertyValue(property, control);
 
                    if (controlColor.A.Equals(targetColor.A) &&
                        controlColor.R.Equals(targetColor.R) &&
                        controlColor.G.Equals(targetColor.G) &&
                        controlColor.B.Equals(targetColor.B))
                    {
                        return true;
                    }
 
                    return false;
                });
        }
 
        private Color DoColorBlend(Color startColor, Color targetColor, double ratio)
        {
            percent += 0.2;
 
            int a = (int)Math.Round((startColor.A * (1 - ratio)) + (targetColor.A * ratio));
            int r = (int)Math.Round((startColor.R * (1 - ratio)) + (targetColor.R * ratio));
            int g = (int)Math.Round((startColor.G * (1 - ratio)) + (targetColor.G * ratio));
            int b = (int)Math.Round((startColor.B * (1 - ratio)) + (targetColor.B * ratio));
            return Color.FromArgb(a, r, g, b);
        }
 
        private static Color GetPropertyValue(string pName, Control control)
        {
            Type type = control.GetType();
            string propertyName = pName;
 
            BindingFlags flags = BindingFlags.GetProperty;
            Binder binder = null;
            object[] args = null;
            object value = type.InvokeMember(propertyName, flags, binder, control, args);
 
            return (Color)value;
        }
    }
 
    #endregion
}