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?
有没有一种简单的方法来混合两个System.Drawing.Color值?或者我是否必须编写自己的方法来采用两种颜色并将它们组合起来?
If I do, how might one go about that?
如果我这样做,那怎么可能呢?
3 个解决方案
#1
30
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);
}
#2
5
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.
如果要以对人眼看起来更自然的方式混合颜色,则应考虑在RGB的不同颜色空间中工作,例如L * a * b *,HSL,HSB。
There a great code project article on colour spaces with examples in C#.
有关色彩空间的优秀代码项目文章以及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.
您可能喜欢使用L * a * b *,因为它旨在线性化色差的感知,因此应该产生优雅的渐变。
#3
3
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.
我不完全确定你在尝试使用混合做什么,但你可以看看alpha混合http://en.wikipedia.org/wiki/Alpha_compositing。
#1
30
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);
}
#2
5
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.
如果要以对人眼看起来更自然的方式混合颜色,则应考虑在RGB的不同颜色空间中工作,例如L * a * b *,HSL,HSB。
There a great code project article on colour spaces with examples in C#.
有关色彩空间的优秀代码项目文章以及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.
您可能喜欢使用L * a * b *,因为它旨在线性化色差的感知,因此应该产生优雅的渐变。
#3
3
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.
我不完全确定你在尝试使用混合做什么,但你可以看看alpha混合http://en.wikipedia.org/wiki/Alpha_compositing。