简体   繁体   English

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

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

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? 有任何想法吗?

(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(紫色))会很有效。

This is easier than you think. 这比你想象的要容易。

First you need an hsv or hsl to rgb conversion function. 首先,您需要hsv或hsl到rgb转换功能。 Here is C# code to do that conversion . 这是进行转换的C#代码

Then you simply iterate over all of the possible values of the hue h while keeping the saturation s and luminosity l constant. 然后,您只需迭代色调h所有可能值,同时保持saturation和亮度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 GetRainbowColor this way by adding all of these colors to a List<ColorRGB> and returning the appropriate indexed color. 您还可以通过将所有这些颜色添加到List<ColorRGB>并返回适当的索引颜色,轻松地GetRainbowColor这种方式创建所需的函数GetRainbowColor

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

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. WPF的System.Windows.Media.GradientBrush也可以正常工作。 It's abstract so you might end up with WPF's LinearGradientBrush. 它是抽象的,所以你最终可能会得到WPF的LinearGradientBrush。 It's in a different namespace than the other. 它与另一个名称空间不同。

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). 我做了一个快速检查,看看你是否能够以更独立的方式获得某些功能(例如获取Point数组或其他东西)。 Doesn't appear to be the case. 似乎并非如此。

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

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.html it'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 BTW,C#的第一个谷歌热门是: http//miguelmoreno.net/sandbox/wavelengthtoRGB/default.aspx

http://colorfulconsole.com/ Allows pretty much what you're looking for, also can be installed as a NuGet package. http://colorfulconsole.com/允许你正在寻找的东西,也可以作为NuGet包安装。 It's not exactly a rainbow gradient. 它不完全是彩虹渐变。 But it can write gradients to the console, also this confuses the IDE between the 但它可以将渐变写入控制台,这也会使IDE混淆

Colorful.Console

and

System.Console

So make sure to define the right Console. 因此,请确保定义正确的控制台。

Just use the Rainbow.dll . 只需使用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效果,我想,你想要的每个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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM