有没有一种简单的方法来混合两个 System.Drawing.Color 值?

发布于 2024-09-19 11:47:17 字数 100 浏览 5 评论 0 原文

有没有一种简单的方法来混合两个 System.Drawing.Color 值?或者我是否必须编写自己的方法来获取两种颜色并将它们组合起来?

如果我这样做,人们会怎么做呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瞎闹 2024-09-26 11:47:18

我正是为此目的编写了一个实用方法。 :)

/// <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);
}

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);
}
淡看悲欢离合 2024-09-26 11:47:18

如果您想以人眼看起来更自然的方式混合颜色,您应该考虑在与 RGB 不同的颜色空间中工作,例如 L*a*b*、HSL、HSB。

有一篇很棒的关于色彩空间的代码项目文章以及 C# 中的示例。

您可能喜欢使用 L*a*b*,因为它旨在线性化颜色差异的感知,因此应该产生优雅的渐变。

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.

泅渡 2024-09-26 11:47:18

我认为混合一系列颜色更容易,以防万一你想混合超过 2 种颜色
这是我的函数:

private Color colorBlend(List<Color> clrArr)
{   
    int r = 0;
    int g = 0;
    int b = 0;
    foreach(Color color in clrArr)
    {
        r += color.R;
        g += color.G;
        b += color.B;
    }
    r = r / clrArr.Count;
    g = g / clrArr.Count;
    b = b / clrArr.Count;
    return Color.FromArgb(r, g, b);
}

数组必须是颜色列表
使用 :

List<Color> colorList = new List<Color>();
colorList.Add(Color.Red);
colorList.Add(Color.Blue);
picturebox.backColor = colorBlend(colorList);

I think its easier to blend an array of colors, in case you want to blend more then 2
Here is my function:

private Color colorBlend(List<Color> clrArr)
{   
    int r = 0;
    int g = 0;
    int b = 0;
    foreach(Color color in clrArr)
    {
        r += color.R;
        g += color.G;
        b += color.B;
    }
    r = r / clrArr.Count;
    g = g / clrArr.Count;
    b = b / clrArr.Count;
    return Color.FromArgb(r, g, b);
}

The array must be as a List of color
Use :

List<Color> colorList = new List<Color>();
colorList.Add(Color.Red);
colorList.Add(Color.Blue);
picturebox.backColor = colorBlend(colorList);
他不在意 2024-09-26 11:47:18

要通过使用 alpha 混合来组合两种颜色,可以采用以下方法:

private const float Divisor = byte.MaxValue;

public static Color AlphaBlend(this Color backColor, Color foreColor)
{
    var fa = foreColor.A / Divisor;
    var fr = foreColor.R / Divisor;
    var fg = foreColor.G / Divisor;
    var fb = foreColor.B / Divisor;

    var ba = backColor.A / Divisor;
    var br = backColor.R / Divisor;
    var bg = backColor.G / Divisor;
    var bb = backColor.B / Divisor;

    var a = fa + ba - fa * ba;

    if (a <= 0)
        return Color.Transparent;

    var r = (fa * (1 - ba) * fr + fa * ba * fa + (1 - fa) * ba * br) / a;
    var g = (fa * (1 - ba) * fg + fa * ba * fa + (1 - fa) * ba * bg) / a;
    var b = (fa * (1 - ba) * fb + fa * ba * fa + (1 - fa) * ba * bb) / a;

    return Color.FromArgb(
        (int)(a * byte.MaxValue),
        (int)(r * byte.MaxValue),
        (int)(g * byte.MaxValue),
        (int)(b * byte.MaxValue));
}

To combine two colors by using alpha blending the following method can be taken:

private const float Divisor = byte.MaxValue;

public static Color AlphaBlend(this Color backColor, Color foreColor)
{
    var fa = foreColor.A / Divisor;
    var fr = foreColor.R / Divisor;
    var fg = foreColor.G / Divisor;
    var fb = foreColor.B / Divisor;

    var ba = backColor.A / Divisor;
    var br = backColor.R / Divisor;
    var bg = backColor.G / Divisor;
    var bb = backColor.B / Divisor;

    var a = fa + ba - fa * ba;

    if (a <= 0)
        return Color.Transparent;

    var r = (fa * (1 - ba) * fr + fa * ba * fa + (1 - fa) * ba * br) / a;
    var g = (fa * (1 - ba) * fg + fa * ba * fa + (1 - fa) * ba * bg) / a;
    var b = (fa * (1 - ba) * fb + fa * ba * fa + (1 - fa) * ba * bb) / a;

    return Color.FromArgb(
        (int)(a * byte.MaxValue),
        (int)(r * byte.MaxValue),
        (int)(g * byte.MaxValue),
        (int)(b * byte.MaxValue));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文