tags:

views:

75

answers:

2

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?

+4  A: 

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.FromArgb(r, g, b);
}
Timwi
This would be interesting as an operator overload so that you could write: `Color NewColor = Color1 + Color2;` An article that deals with this: http://msdn.microsoft.com/en-us/magazine/cc163737.aspx
JYelton
You could always add in a "this" just because extensions are fun.
Blam
@Blam: OK, done :)
Timwi
@JYelton: But then you can’t specify the *amount* parameter...
Timwi
Depending on the colors, linear blending can darken the resulting color.
MerickOWA
@Timwi: That's true -- an operator overload would have to use some default blending value.
JYelton
+1  A: 

I am not entirely sure what you're trying to do with blending, but you could look into alpha blending http://en.wikipedia.org/wiki/Alpha_compositing.

Aurojit Panda