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

后端 未结 3 933
执念已碎
执念已碎 2021-01-01 13:20

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

相关标签:
3条回答
  • 2021-01-01 13:58

    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.

    0 讨论(0)
  • 2021-01-01 14:05

    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);
    }
    
    0 讨论(0)
  • 2021-01-01 14:20

    If you want to blend colours in a way that looks more natural to the human eye, you should consider working in a different colour space to RGB, such as L*a*b*, HSL, HSB.

    There a great code project article on colour spaces with examples in C#.

    You may like to work with L*a*b*, as it was designed to linearise the perception of color differences and should therefore produce elegant gradients.

    0 讨论(0)
提交回复
热议问题