Sign in to follow this  

HSV to RGB conversion

This topic is 3297 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, i need an explanation on the algorithm to convert colors in HSV model to RGB model. all the algorithms that i have seen assign the following values to RGB in different regions of the hexacone h /= 60; // sector 0 to 5 i = floor( h ); f = h - i; // factorial part of h p = v * ( 1 - s ); q = v * ( 1 - s * f ); t = v * ( 1 - s * ( 1 - f ) ); switch( i ) { case 0: *r = v; *g = t; *b = p; break; case 1: *r = q; *g = v; *b = p; break; case 2 *r = p; *g = v; *b = t; break; case 3: *r = p; *g = q; *b = v; break; case 4: *r = t; *g = p; *b = v; break; default: // case 5: *r = v; *g = p; *b = q; break; } i can't understand the mapping of p,q and t. an explanation is very much appreciated. thank you.

Share this post


Link to post
Share on other sites
The hexagon basically consists of 6 triangles (sectors). Each edge between adjacent sectors has a primary rgb color; thus, the v component (luminance) drives one of the rgb components in each sector directly.

The proportions of the remaining rgb components are establishing by linearly interpolating the components by hue across the current triangle (f) and then interpolating the result towards their average by using saturation (s). Finally, the components are multiplied by the brightness (v).

In essence, the proportions of the colors equals the barycentric coordinates of the current point on the triangle. These coordinates are what the p, q and t variables represent.

The algorithm is easier to understand if you consider only one triangle (sector) at a time. Once you understand the interactions of the values on a single triangle, it will all "click" together and it is trivial to derive the values for the other triangles.

Share this post


Link to post
Share on other sites
I just told you the theory :)

I haven't read this anywhere in particular, I just have innate knowledge of how/why it works.

If you want to "get" the interpolation across triangle part, read up on barycentric coordinates and linear interpolation. Then apply these concepts to rgb colors, represented as 3-component vectors.

Share this post


Link to post
Share on other sites

This topic is 3297 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this