流体可视化rainbow颜色映射算法原理技术咨询
rainbow Color Mapping Function Let's walk through exactly how this function turns a scalar value (your fluid density rho, normalized to 0–1) into a rainbow-colored RGB triplet. I'll break it down step by step, no jargon overload:
1. Input Value Safety & Range Shifting
First, the function makes sure your input value stays within a valid 0–1 range—no weird out-of-bounds values messing things up:
if (value<0) value=0; if (value>1) value=1;
Then it adjusts this range to play nicely with the rest of the color math:
const float dx=0.8; value = (6-2*dx)*value+dx;
With dx=0.8, this converts your original 0–1 input to a new range of 0.8 to 5.2. This stretched range lets the red, green, and blue channels overlap smoothly to create that classic rainbow fade.
2. Red Channel Logic
The red channel uses two absolute value functions to create a smooth peak and falloff:
*R = max(0.0,(3-fabs(value-4)-fabs(value-5))/2);
- Think of
fabs(value-4)andfabs(value-5)as V-shaped curves centered at 4 and 5. Subtracting both from 3 gives a curve that hits full red (1.0) whenvalueis 4.5. - Red fades to 0 when
valuedrops below 3 or goes above 6 (but our adjusted max is 5.2, so it just fades out as we approach the highestrhovalues). max(0.0, ...)ensures we never get negative red values—RGB channels can't be negative!
3. Green Channel Logic
Green follows the same absolute value trick but is centered on the middle of our adjusted range:
*G = max(0.0,(4-fabs(value-2)-fabs(value-4))/2);
- This curve peaks at full green (1.0) when
valueis 3. It starts rising as soon asvalueenters our adjusted range (0.8) and fades out as we near 6. - This means green is present across most of your
rhorange, acting as the bridge between blue and red.
4. Blue Channel Logic
Blue is focused on the lower end of your input range:
*B = max(0.0,(3-fabs(value-1)-fabs(value-2))/2);
- It hits full blue (1.0) when
valueis 1.5, and fades completely to 0 oncevaluereaches 3. - So blue only shows up for your lowest
rhovalues, which helps distinguish low-density regions at a glance.
The Full Rainbow Transition
When you feed your normalized rho values through this function, you get a smooth, intuitive gradient:
- Lowest
rho(0): Starts at deep blue (full B, faint G, no R) - Mid-low
rho: Shifts to cyan (mix of B and G) then bright green - Mid
rho: Moves to yellow (mix of G and R) - Highest
rho(1): Ends at vivid red (full R, faint G, no B)
This is a tried-and-true rainbow colormap, perfect for fluid visualization because it makes subtle differences in density easy to spot.
内容的提问来源于stack exchange,提问作者K. Saudi

