如何在 C# 中获得彩虹色渐变?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2288498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:06:26  来源:igfitidea点击:

How do I get a rainbow color gradient in C#?

c#colorsgradient

提问by mafu

I'd like to have an enumeration of Colors based on the rainbow colors (red... yellow... green... blue...).

我想根据彩虹颜色(红色...黄色...绿色...蓝色...)枚举颜色。

I see basically two ways to do that:

我看到基本上有两种方法可以做到这一点:

  1. Create a lookup table containing some important reference colors and interpolate between these. I don't like this idea at all.

  2. Apply some more or less fancy math. Probably less, but I don't quite see how it works. Any ideas?

  1. 创建一个包含一些重要参考颜色的查找表并在这些颜色之间进行插值。我一点也不喜欢这个主意。

  2. 应用一些或多或少的花哨数学。可能更少,但我不太明白它是如何工作的。有任何想法吗?

(Oh, and while I did some SO research, I found no good results. If this question was already posted, please just point me to the location and I'll delete this.)

(哦,虽然我做了一些 SO 研究,但我没有发现好的结果。如果这个问题已经发布,请告诉我位置,我会删除它。)

Edit:I'd prefer to have this independent of the used technology to display the gradient. For instance, something like GetRainbowColor (float f) with f ranging from 0 (red) to 1 (violet) would work great.

编辑:我更喜欢独立于使用的技术来显示渐变。例如,像 GetRainbowColor (float f) 这样的 f 范围从 0(红色)到 1(紫色)会很好用。

采纳答案by Brian R. Bondy

This is easier than you think.

这比你想象的要容易。

First you need an hsv or hsl to rgb conversion function. Here is C# code to do that conversion.

首先你需要一个 hsv 或 hsl 到 rgb 的转换函数。 这是执行该转换的 C# 代码

Then you simply iterate over all of the possible values of the hue hwhile keeping the saturations and luminosity lconstant.

然后您只需迭代所有可能的色调值,h同时保持saturations 和亮度l不变。

If you want 100 colors of the rainbow spaced out equally:

如果你想要 100 种颜色的彩虹均匀分布:

for(double i = 0; i < 1; i+=0.01)
{
    ColorRGB c = HSL2RGB(i, 0.5, 0.5);
    //do something with the color
}

You could also easily create your desired function GetRainbowColorthis way by adding all of these colors to a List<ColorRGB>and returning the appropriate indexed color.

您还可以GetRainbowColor通过将所有这些颜色添加到 aList<ColorRGB>并返回适当的索引颜色,以这种方式轻松创建所需的函数。

回答by S.Lott

Start here: http://www.midnightkite.com/color.html

从这里开始:http: //www.midnightkite.com/color.html

You can interpret this: http://www.physics.sfasu.edu/astro/color/spectra.htmlit's FORTRAN, but it's pretty obvious what it does.

你可以解释这个:http: //www.physics.sfasu.edu/astro/color/spectra.html它是 FORTRAN,但它的作用很明显。

Also, you can read more in-depth here: http://en.wikipedia.org/wiki/CIE_1931_color_space

此外,您可以在这里更深入地阅读:http: //en.wikipedia.org/wiki/CIE_1931_color_space

Here's a version in Python: http://www.johnny-lin.com/py_code/wavelen2rgb.py

这是 Python 版本:http: //www.johnny-lin.com/py_code/wavelen2rgb.py

BTW, the first google hit for C# is this: http://miguelmoreno.net/sandbox/wavelengthtoRGB/default.aspx

顺便说一句,C# 的第一个谷歌命中是这样的:http: //miguelmoreno.net/sandbox/wavelengthtoRGB/default.aspx

回答by Mike Two

In winforms(or anything using GDI+) you could use System.Drawing.Drawing2D.LinearGradientBrush to do the interpolation for you.

在 winforms(或使用 GDI+ 的任何东西)中,您可以使用 System.Drawing.Drawing2D.LinearGradientBrush 为您进行插值。

WPF's System.Windows.Media.GradientBrush could work as well. It's abstract so you might end up with WPF's LinearGradientBrush. It's in a different namespace than the other.

WPF 的 System.Windows.Media.GradientBrush 也可以工作。它是抽象的,因此您最终可能会使用 WPF 的 LinearGradientBrush。它位于与另一个不同的命名空间中。

EDIT: since the question was edited to indicate that you want to be tech independent I don't think this answer applies. I'm going to leave it here for now in case someone is looking for Gradients in C#, but if someone finds that objectionable I'll remove the answer.

编辑:由于编辑了问题以表明您希望独立于技术,因此我认为此答案不适用。如果有人在 C# 中寻找 Gradients,我暂时将其留在这里,但如果有人认为这令人反感,我将删除答案。

I did a quick check to see if you could at least get at some of the functionality in a more independent way (such as getting an array of Point or something). Doesn't appear to be the case.

我做了一个快速检查,看看您是否至少可以以更独立的方式获得某些功能(例如获取 Point 数组或其他东西)。似乎并非如此。

回答by Bitterblue

I like to use this:

我喜欢用这个:

public static Color Rainbow(float progress)
{
    float div = (Math.Abs(progress % 1) * 6);
    int ascending = (int) ((div % 1) * 255);
    int descending = 255 - ascending;

    switch ((int) div)
    {
        case 0:
            return Color.FromArgb(255, 255, ascending, 0);
        case 1:
            return Color.FromArgb(255, descending, 255, 0);
        case 2:
            return Color.FromArgb(255, 0, 255, ascending);
        case 3:
            return Color.FromArgb(255, 0, descending, 255);
        case 4:
            return Color.FromArgb(255, ascending, 0, 255);
        default: // case 5:
            return Color.FromArgb(255, 255, 0, descending);
    }
}

回答by ProfNimrod

Here's one I like to use (the output is an HTML RGB color):

这是我喜欢使用的一个(输出是 HTML RGB 颜色):

public static String Rainbow(Int32 numOfSteps, Int32 step)
        {
            var r = 0.0;
            var g = 0.0;
            var b = 0.0;
            var h = (Double)step / numOfSteps;
            var i = (Int32)(h * 6);
            var f = h * 6.0 - i;
            var q = 1 - f;

            switch (i % 6)
            {
                case 0:
                    r = 1;
                    g = f;
                    b = 0;
                    break;
                case 1:
                    r = q;
                    g = 1;
                    b = 0;
                    break;
                case 2:
                    r = 0;
                    g = 1;
                    b = f;
                    break;
                case 3:
                    r = 0;
                    g = q;
                    b = 1;
                    break;
                case 4:
                    r = f;
                    g = 0;
                    b = 1;
                    break;
                case 5:
                    r = 1;
                    g = 0;
                    b = q;
                    break;
            }
            return "#" + ((Int32)(r * 255)).ToString("X2") + ((Int32)(g * 255)).ToString("X2") + ((Int32)(b * 255)).ToString("X2");
        }

回答by Ra1n

http://colorfulconsole.com/Allows pretty much what you're looking for, also can be installed as a NuGet package. It's not exactly a rainbow gradient. But it can write gradients to the console, also this confuses the IDE between the

http://colorfulconsole.com/几乎可以满足您的需求,也可以作为 NuGet 包安装。它不完全是彩虹渐变。但是它可以将渐变写入控制台,这也会混淆 IDE 之间的

Colorful.Console

and

System.Console

So make sure to define the right Console.

所以一定要定义正确的控制台。

回答by Peter Parker

Just use the Rainbow.dll. This is probably not the best library, but for a smooth Rainbow effect on, I think, every WinForm Control you want, this is it.

只需使用Rainbow.dll. 这可能不是最好的库,但对于平滑的彩虹效果,我认为,就是你想要的每个 WinForm 控件。

Link: https://marschalldev.com/2018/08/02/csharp-rainbow-effect-net-dll/

链接:https: //marschalldev.com/2018/08/02/csharp-rainbow-effect-net-dll/

How to use: Yourcontrol.background = Color.FromArgb(Class1.A, Class1.R, Class1.G);

如何使用: Yourcontrol.background = Color.FromArgb(Class1.A, Class1.R, Class1.G);