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:
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?
(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.)
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.
VIBGYOR is an acronym for Violet, Indigo, Blue, Green, Yellow, Orange and Red while ROYGBIV is in the reverse order. These are the seven colors displayed in a rainbow and people use these acronyms for easily remembering the rainbow color codes.
Color gradients, or color transitions, are defined as a gradual blending from one color to another. This blending can occur between colors of the same tone (from light blue to navy blue), colors of two different tones (from blue to yellow), or even between more than two colors (from blue to purple to red to orange).
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.
Then you simply iterate over all of the possible values of the hue h
while keeping the saturation
s and luminosity l
constant.
If you want 100 colors of the rainbow spaced out equally:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With