# Improving the Rainbow – Part 2

In the previous part of this tutorial, Improving the Rainbow – Part 1, we have seen different techniques to reproduce the colours of the rainbow procedurally. Solving this problem efficiently will allow us to simulate physically based reflections with a much higher fidelity.

The purpose of this post is to introduce a novel approach that yields better results than any of the previous solutions, without using any branching.

You can find the complete series here:

A link to download the Unity project used in this series is also provided at the end of the page.

#### Introduction

In the previous post, we have analysed four different techniques to convert wavelengths in the visible range of the electromagnetic spectrum (400-700 nanometers) to their respective colours.

Three of those solutions (JET, Bruton and Spektre) heavily relied on if statements. While that is a standard practice in C#, branching in a shader is notoriously bad. The approach discussed in the GPU Gems book is the only one that did not use any branching. Despite that, it did not provide the best approximation for the colours in the visible spectrum.

The post will show an optimised version of the colour scheme firstly described in the GPU Gems book.

#### The “Bump” Colour Scheme

The original colour scheme introduced in the GPU Gems book used three parabolas (called “bumps” by the author) to replicate the distribution of R, G and B colours in the rainbow.

Each bump is described by the following equation:

Each wavelength in the range [400, 700] is associated with a normalised value in the range [0,1]. Then, the R, G and B components of the visible spectrum are given by:

All the numerical values have been been chosen by the author experimentally. You can see, however, how poorly they map to the actual distribution of colours.

#### Optimising for Quality

The first solution I personally came up with uses exactly the same equations of the GPU Gems colour scheme. However, I have optimised all the numerical values so that the final range of colours matches as closely as possible with the actual colours from the visible spectrum.

The result converges to the following solution:

And yields a much more realistic result:

 Name Gradient GPU Gems Zucconi Visible

Like the original solution, this new approach is branchless. Hence, it is perfect for shaders. This is the code:

To solve this optimisation algorithm I used the Python library scikit.

Those are the parameters necessary to replicate my results:

• Algorithm: L-BFGS-B
• Tolerance:
• Iterations:
• Weighted MSE:
• Fitting
• Initial solution:
• Solution:

#### Improving the Rainbow

If we look closer at the distribution of colours in the visible spectrum, we can notice that parabolas cannot really capture the R, G and B colour curves. A slightly better approach is to use six parabolas, instead of just three. Fitting two bumps for each primary component, we can get a much better approximation. The difference is really visible in the violet part of the spectrum.

The difference is really visible in the violet and orange parts of the spectrum:

Here is the code:

There is no doubt that spectral_zucconi6 provides the best colour approximation, without introducing any branching. If performance is an issue, you can rely upon its simplified version  spectral_zucconi.

#### Conclusion

This post provides an overview of some of the most common techniques to generate rainbow-like patterns in a shader. Moreover, a novel approach has been introduced.

 Name Gradient JET Bruton GPU Gems Spektre Zucconi Zucconi6 Visible

You can find a WebGL port of those colour schemes on this Shadertoy page.

You can find the complete series here:

The Python project used to find the optimal parameters for the spectral_zucconi and spectral_zucconi6 functions is available on Patreon as well.

##### Support this blog! ♥

For the past three years I've been dedicating more and more of my time to the creation of quality tutorials, mainly about game development and machine learning. If you think these posts have either helped or inspired you, please consider supporting this blog.

##### Don't miss the next tutorial!

There's a new post every Wednesday: leave your email to be notified!

1. Job van der Zwan

> Like the original solution, this new approach is branchless. Hence, it is perfect for a shader for shaders.

I am going to pretend that this isn’t a typo, but the code equivalent of saying “that person is an artist’s artist”, that is: normal people don’t appreciate it, but those in the know see the beauty.

• Ooops! 🙂
Sorry, I’m correcting it right now.
I hope this isn’t disappointing you! 🙂

### Webmentions

• Understanding Diffraction Grating - Alan Zucconi August 8, 2017

Ooops! 🙂
Sorry, I’m correcting it right now.
I hope this isn’t disappointing you! 🙂

• Tutorial Series - Alan Zucconi August 8, 2017

Ooops! 🙂
Sorry, I’m correcting it right now.
I hope this isn’t disappointing you! 🙂

• Improving the Rainbow - Part 1 - Alan Zucconi August 8, 2017

Ooops! 🙂
Sorry, I’m correcting it right now.
I hope this isn’t disappointing you! 🙂

• The Nature of Light - Alan Zucconi August 8, 2017

Ooops! 🙂
Sorry, I’m correcting it right now.
I hope this isn’t disappointing you! 🙂