Search code examples
c#.netcolorsgdi+

Is there an easy way to blend two System.Drawing.Color values?


Is there an easy way to blend two System.Drawing.Color values? Or do I have to write my own method to take in two colors and combine them?

If I do, how might one go about that?


Solution

  • I wrote a utility method for exactly this purpose. :)

    /// <summary>Blends the specified colors together.</summary>
    /// <param name="color">Color to blend onto the background color.</param>
    /// <param name="backColor">Color to blend the other color onto.</param>
    /// <param name="amount">How much of <paramref name="color"/> to keep,
    /// “on top of” <paramref name="backColor"/>.</param>
    /// <returns>The blended colors.</returns>
    public static Color Blend(this Color color, Color backColor, double amount)
    {
        byte r = (byte) (color.R * amount + backColor.R * (1 - amount));
        byte g = (byte) (color.G * amount + backColor.G * (1 - amount));
        byte b = (byte) (color.B * amount + backColor.B * (1 - amount));
        return Color.FromRgb(r, g, b);
    }