HSV to RGB approximation

Thinking about iq’s color palettes I realized that it was possible to use that same trick to approximate HSV. While there are already good branchless methods to map HSV to RGB, I thought it might be fun to see if it was possible.

A nice example of the method can be seen in the following picture, which is taken from http://dev.thi.ng/gradients/, a great tool to experiment with!


The code is:

vec3 hsv2rgb_approx(vec3 c)
      float h = c.x;
      float s = c.y * c.z;
      float s_n = c.z - s * .5;

      return palette(h, vec3(s_n), vec3(s), vec3(1.0, 0.667, .334));

You can check out a demo here

The comparison can be seen here, and you can see that there’s a difference with more classical approaches.


Essentially, by scaling and offseting the cosine wave it is possible to fake it. Note however that it is not perfect, because the real mapping uses linear functions, as can be seen in this figure from Wikipedia:



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s