How would you go about making a range of RGB colours evenly spaced over the spectral colour range? So as to look like a real rainbow.
相关问题
- DBGrid - How to set an individual background color
- How to display an image represented by three matri
- How to delete a certain part of an Image?
- Name for a method that has only side effects
- draw polylines with different colors on v2 maps
相关文章
- Emacs/xterm color annoyance on Linux
- Should client-server code be written in one “proje
- Algorithm for maximizing coverage of rectangular a
- matplotlib bwr-colormap, always centered on zero
- Is there an existing solution for these particular
- MeshLab: How to import XYZRGB file
- What is Scope Creep? [closed]
- How to add RGB values into setColor() in Java?
The other solutions require rather large amounts of code and conditional branching, which makes them unsuitable for GPUs. I recently arrived at the following magically simple formula in GLSL. It's essentially the same in OpenCL:
This will give you a rainbow color that corresponds to the given hue value in linear RGB. To use in an image, convert to sRGB and then multiply by 255.
Here is a C++ version:
The key here is to realize that the graph of each of the R, G, B coordinates in function of the hue value is a clamped value of a periodic triangle function, and that can be obtained as the absolute value of a sawtooth function,
x - floor(x)
.The simplest approach is to do a linear interpolation (in RGB) between each consecutive pair in this sequence:
#ff0000
red#ffff00
yellow#00ff00
green#00ffff
cyan#0000ff
blue#ff00ff
magenta#ff0000
back to redThis should get you pretty much the same result as sweeping through the hue values in HSV or HSL, but lets you work directly in RGB. Note that only one component changes for each interpolation, which simplifies things. Here's a Python implementation: