如何在C#中获得彩虹颜色渐变? [英] How do I get a rainbow color gradient in C#?

查看:2313
本文介绍了如何在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. 创建一个包含一些重要参考颜色的查找表,我根本不喜欢这个想法。

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

应用一些或多或少的花哨数学。可能更少,但我不太清楚它是如何工作的。任何想法?

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

(哦,我做了一些SO研究,已经发布,请指向我的位置,我会删除这个。)

(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.)

编辑:独立于使用的技术来显示渐变。例如,类似GetRainbowColor(float f)的f(范围从0(红色)到1(紫色))会工作得很好。

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.

推荐答案

p>这比你想象的要容易。

This is easier than you think.

首先,您需要一个hsv或hsl to rgb转换函数。 这里是C#代码做这种转换

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

然后,您只需遍历hue h 的所有可能值,同时保留饱和度 s和光度 l 常数。

Then you simply iterate over all of the possible values of the hue h while keeping the saturation s and luminosity l constant.

彩虹间隔均匀:

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

通过将所有这些颜色添加到 List< ColorRGB> 并返回适当的索引颜色,可以获得所需的函数 GetRainbowColor

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.

这篇关于如何在C#中获得彩虹颜色渐变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆