HSVtoRGB and RGBtoHSV in C#

I found myself in need of HSV to RGB and RBB to HSV conversions in C#. There are a lot around, but most of them work with the 0-255 color space and I needed 0.0-1.0 color space, so I made my own implementations from (buggy) code I found on the web. Should you need color space conversions in the real, here you have it.

using System;

public static class ColorSpace
{
    // Expects and returns values in the range 0 to 1
    public static void HSVtoRGB(double h, double s, double v, out double r, out double g, out double b)
    {
        if (s == 0)
        {
            r = v;
            g = v;
            b = v;
        }
        else
        {
            double varH = h * 6;
            double varI = Math.Floor(varH);
            double var1 = v * (1 - s);
            double var2 = v * (1 - (s * (varH - varI)));
            double var3 = v * (1 - (s * (1 - (varH - varI))));

            if (varI == 0)
            {
                r = v;
                g = var3;
                b = var1;
            }
            else if (varI == 1)
            {
                r = var2;
                g = v;
                b = var1;
            }
            else if (varI == 2)
            {
                r = var1;
                g = v;
                b = var3;
            }
            else if (varI == 3)
            {
                r = var1;
                g = var2;
                b = v;
            }
            else if (varI == 4)
            {
                r = var3;
                g = var1;
                b = v;
            }
            else
            {
                r = v;
                g = var1;
                b = var2;
            }
        }
    }

    // Expects and returns values in the range 0 to 1
    public static void RGBtoHSV(double r, double g, double b, out double h, out double s, out double v)
    {
        double varMin = Math.Min(r, Math.Min(g, b));
        double varMax = Math.Max(r, Math.Max(g, b));
        double delMax = varMax - varMin;

        v = varMax;

        if (delMax == 0)
        {
            h = 0;
            s = 0;
        }
        else
        {
            double delR = (((varMax - r) / 6) + (delMax / 2)) / delMax;
            double delG = (((varMax - g) / 6) + (delMax / 2)) / delMax;
            double delB = (((varMax - b) / 6) + (delMax / 2)) / delMax;

            s = delMax / varMax;

            if (r == varMax)
            {
                h = delB - delG;
            }
            else if (g == varMax)
            {
                h = (1.0 / 3) + delR - delB;
            }
            else //// if (b == varMax) 
            {
                h = (2.0 / 3) + delG - delR;
            }

            if (h < 0)
            {
                h += 1;
            }

            if (h > 1)
            {
                h -= 1;
            }
        }
    }
}

About mfagerlund
Writes code in my sleep - and sometimes it even compiles!

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: