Trying to implement HSV color controls from scratch. Hit a wall and could use fresh eyes

Hi all,

As an exercise to improve my node wielding skills, I decided to try and implement a node-group dedicated to modifying hue, saturation, and lightness. I know there is a node already included with Blender that does this, but I’m not a huge fan of the algorithm and I just wanted a pet project to mess with.

The code I’m implementing is found here.

RGBtoHSV= function(color) {
        var r,g,b,h,s,v;
        r= color[0];
        g= color[1];
        b= color[2];
        min = Math.min( r, g, b );
        max = Math.max( r, g, b );


        v = max;
        delta = max - min;
        if( max != 0 )
            s = delta / max;        // s
        else {
            // r = g = b = 0        // s = 0, v is undefined
            s = 0;
            h = -1;
            return [h, s, undefined];
        }
        if( r === max )
            h = ( g - b ) / delta;      // between yellow & magenta
        else if( g === max )
            h = 2 + ( b - r ) / delta;  // between cyan & yellow
        else
            h = 4 + ( r - g ) / delta;  // between magenta & cyan
        h *= 60;                // degrees
        if( h < 0 )
            h += 360;
        if ( isNaN(h) )
            h = 0;
        return [h,s,v];
    };

HSVtoRGB= function(color) {
        var i;
        var h,s,v,r,g,b;
        h= color[0];
        s= color[1];
        v= color[2];
        if(s === 0 ) {
            // achromatic (grey)
            r = g = b = v;
            return [r,g,b];
        }
        h /= 60;            // sector 0 to 5
        i = Math.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;
        }
        return [r,g,b];
    }

Here are the groups:

HSV to RGB Conversion:

RGB to HSV Conversion:

Unfortunately after building out the required node groups, they don’t seem to be working. When I test both of them out I get an overwhelming red/pink tint. See the following images:

I’m kind of exhausted after working on this all day and a little put off that they aren’t working correctly, so I decided to throw everything up here to see if any of you know what might be causing the problems. Maybe some of you might find some of the node groups useful as well.

Here is the file:

https://www.dropbox.com/s/orm6okr5me6k00e/HSL%20Implementation%20Bugged.blend?dl=0

Happy blending!

Not all HSV algorithms give the same output. Test it by running two of your nodes, not by combining one of your nodes with one of Blender’s nodes. Unless you’re 100% confident that Blender is using the same algorithm you are.

Your linked page addresses a different question than how to separate or combine HSV, after all-- it’s answering how to edit by HSV, and to do that, it separates and combines. It does not have to separate into the same mapping of hue that Blender uses in order to allow editing by HSV.

If you still have problems after that, first, localize the problems. Is the problem in the Hue output only? Or are S and V also a problem? That’ll guide you on where to look.

Edit: In fact, look at what numbers get outputted for hue by your code: a range from 0 to 360. What values does Blender expect from hue? 0 to 1. And it doesn’t look like your nodes correct for that.

1 Like