Linear interpolation between two Color. - CSharp System.Drawing

CSharp examples for System.Drawing:Color

Description

Linear interpolation between two Color.

Demo Code


using System.Windows.Media;
using System.Globalization;
using System;/*from   w  w w. j ava2 s.c o  m*/

public class Main{
        /// <summary>
        /// Linear interpolation between two <see cref="Color"/>s.
        /// </summary>
        /// <param name="c0">
        /// </param>
        /// <param name="c1">
        /// </param>
        /// <param name="x">
        /// </param>
        /// <returns>
        /// </returns>
        public static Color Interpolate(Color c0, Color c1, double x)
        {
            double r = c0.R * (1 - x) + c1.R * x;
            double g = c0.G * (1 - x) + c1.G * x;
            double b = c0.B * (1 - x) + c1.B * x;
            double a = c0.A * (1 - x) + c1.A * x;
            return Color.FromArgb((byte)a, (byte)r, (byte)g, (byte)b);
        }
}

Related Tutorials