/// <summary> /// Interpolates the forecolor of a windows forms control (or any object with a ForeColor property). /// </summary> /// <param name="animationMessage">The control whose forecolor is meant to be interpolated.</param> /// <param name="destinationColor">The target colour to interpolate the backcolor to.</param> /// <param name="interpolator">The interpolator object used for calculating interpolations of colours as well as animation durations.</param> /// <param name="sourceColor">The colour to which start interpolating from.</param> public static void InterpolateForecolor(AnimMessage animationMessage, AnimInterpolator interpolator, Color sourceColor, Color destinationColor) { // Retrieve the BackColor Property via Reflection. var property = animationMessage.Control.GetType().GetProperty("ForeColor"); // Call internal overload. InterpolateColor(property, animationMessage, interpolator, sourceColor, destinationColor); }
/// <summary> /// Interpolates the backcolor of a windows forms control (or any object with a BackColor property). /// </summary> /// <param name="animationMessage">The control whose backcolor is meant to be interpolated.</param> /// <param name="destinationColor">The target colour to interpolate the backcolor to.</param> /// <param name="interpolator">The interpolator object used for calculating interpolations of colours as well as animation durations.</param> public static void InterpolateBackcolor(AnimMessage animationMessage, AnimInterpolator interpolator, Color destinationColor) { // Retrieve the BackColor Property via Reflection. var property = animationMessage.Control.GetType().GetProperty("BackColor"); // Calculate the original colour. Color originalColor = (Color)property?.GetValue(animationMessage.Control); // Call internal overload. InterpolateColor(property, animationMessage, interpolator, originalColor, destinationColor); }
private static async void InterpolateColor(PropertyInfo propertyInfo, AnimMessage animationMessage, AnimInterpolator interpolator, Color sourceColor, Color destinationColor) { // Safety procedure try { // Calculate all interpolated colours in between. Lch originalColorLch = ColorspaceConverter.ColorToLch(sourceColor); Lch newColorLch = ColorspaceConverter.ColorToLch(destinationColor); List <Lch> lchColours = interpolator.CalculateIntermediateColours(originalColorLch, newColorLch); // Converted interpolated colours to RGB. List <Color> interpolatedColours = ColorspaceConverter.LchListToColor(lchColours); // Check if object is a winform control. Control winFormControl = animationMessage.Control as Control; bool isWinFormControl = winFormControl != null; // Interpolate over the colours. foreach (Color newBackgroundColour in interpolatedColours) { // Check exit condition. if (animationMessage.PlayAnimation == false) { return; } // Set the BackColor if (isWinFormControl) { winFormControl.Invoke((ChangeColorDelegate)ChangeColor, propertyInfo, winFormControl, newBackgroundColour); } else { propertyInfo.SetValue(animationMessage.Control, newBackgroundColour, null); } // Wait await Task.Delay(interpolator.SleepTime); } } catch { } }