

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

How to change RGB color to HSV? In C# language. I search for very fast method without any external library.

share|improve this question
Not an exact duplicate. HSL != HSV. – Adam Rosenfield Dec 11 '08 at 15:01
What to do to adapt code from HSL post to use in HSV solution? – tomaszs Dec 11 '08 at 15:03
Wikipedia: Both are mathematically cylindrical, but HSV can be thought of conceptually as an inverted cone of colors, HSL conceptually represents a double-cone or sphere. While “hue” in HSL and HSV refers to the same attribute, their definitions of “saturation” differ dramatically. – BlaM Dec 11 '08 at 15:05
@Adam: I wish the Paint.NET people could figure that out! :) – leppie Jul 27 '09 at 10:56
RE-iterating: HSV is very different from HSL. HSV is sometimes known as HSB (especially in Photoshop, and in .NET) – Ian Boyd Dec 20 '09 at 15:45
up vote 19 down vote accepted

Have you considered simply using System.Drawing namespace? For example:

System.Drawing.Color color = System.Drawing.Color.FromArgb(red, green, blue);
float hue = color.GetHue();
float saturation = color.GetSaturation();
float lightness = color.GetBrightness();

Note that it's not exactly what you've asked for (see differences between HSL and HSV and the Color class does not have a conversion back from HSL/HSV but the latter is reasonably easy to add.

share|improve this answer
As you point out, this doesn't actually answer the question because these methods provide a RGB to HSL conversion, not RGB to HSV. – Greg Oct 26 '09 at 17:38
@greg: I agree, this will lead to terrible results, as I have experienced myself. – leppie Nov 5 '09 at 12:01
-1: As this is not the answer to the question. HSV is very different from HSL. Note: HSV is sometimes known as HSB (especially in Photoshop, and in .NET) – Ian Boyd Dec 20 '09 at 15:44
-1: Same as above and it is pretty slow. – Dänu May 27 '12 at 21:17
I'm not sure what the difference is between lightness, brightness and value (always thought they were synonyms), but looking at the System.Drawing.Color implementation I see it is not based on the NTSC weights for Red, Green and Blue, but treat them all the same, yielding suboptimal results. – Itai Bar-Haim May 21 '13 at 15:54

Note that Color.GetSaturation() and Color.GetBrightness() return HSL values, not HSV.
The following code demonstrates the difference.

Color original = Color.FromArgb(50, 120, 200);
// original = {Name=ff3278c8, ARGB=(255, 50, 120, 200)}

double hue;
double saturation;
double value;
ColorToHSV(original, out hue, out saturation, out value);
// hue        = 212.0
// saturation = 0.75
// value      = 0.78431372549019607

Color copy = ColorFromHSV(hue, saturation, value);
// copy = {Name=ff3278c8, ARGB=(255, 50, 120, 200)}

// Compare that to the HSL values that the .NET framework provides: 
original.GetHue();        // 212.0
original.GetSaturation(); // 0.6
original.GetBrightness(); // 0.490196079

The following C# code is what you want. It converts between RGB and HSV using the algorithms described on Wikipedia. The ranges are 0 - 360 for hue, and 0 - 1 for saturation or value.

public static void ColorToHSV(Color color, out double hue, out double saturation, out double value)

    hue = color.GetHue();
    saturation = (max == 0) ? 0 : 1d - (1d * min / max);
    value = max / 255d;

public static Color ColorFromHSV(double hue, double saturation, double value)
    int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
    double f = hue / 60 - Math.Floor(hue / 60);

    value = value * 255;
    int v = Convert.ToInt32(value);
    int p = Convert.ToInt32(value * (1 - saturation));
    int q = Convert.ToInt32(value * (1 - f * saturation));
    int t = Convert.ToInt32(value * (1 - (1 - f) * saturation));

    if (hi == 0)
        return Color.FromArgb(255, v, t, p);
    else if (hi == 1)
        return Color.FromArgb(255, q, v, p);
    else if (hi == 2)
        return Color.FromArgb(255, p, v, t);
    else if (hi == 3)
        return Color.FromArgb(255, p, q, v);
    else if (hi == 4)
        return Color.FromArgb(255, t, p, v);
        return Color.FromArgb(255, v, p, q);
share|improve this answer
i think you transposed the sample values of Saturation and Value in your answer. When i convert rgb(50,120,200) to Hue-Saturation-Value (HSV) i get hsv(212, 75%, 78.4%). Looking at wikipedia formulas for HSV: V=Max(r,g,b). In this case max(50,120,200)=200. 200/255 = 0.7843 – Ian Boyd Dec 20 '09 at 16:13
i'll just edit the answer, transposing Saturation and Value numbers. Saturation should be 0.75, Value should be 0.7843... – Ian Boyd Dec 20 '09 at 16:16
Any idea how to convert saturation from 0-1 to the saturation value that MS Paint uses (ie 0-240 scale)? – Jake Drew Jan 1 at 2:26

See Wikipedia. It should be relatively straightforward to convert those formulas into code. Just be careful about the ranges of your inputs and outputs: are they floats between 0 and 1, integers between 0 and 255, or something else?

share|improve this answer

There's a C implementation here:

Should be very straightforward to convert to C#, as almost no functions are called - just calculations.

found via Google

share|improve this answer
This implementation is not correct. It seems to be based on this example code but it's missing a section (the normalization part). Had me tripped up for a while! – Dan Messing Aug 8 '12 at 21:25

This is the version which works fine for me ported from the C code in BlaM's post.

There's a C implementation here:

Should be very straightforward to convert to C#, as almost no functions are called - just > calculations.

Public Sub HSVtoRGB(ByRef r As Double, ByRef g As Double, ByRef b As Double, ByVal h As Double, ByVal s As Double, ByVal v As Double)
    Dim i As Integer
    Dim f, p, q, t As Double

    If (s = 0) Then
        ' achromatic (grey)
        r = v
        g = v
        b = v
        Exit Sub
    End If

    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))

    Select Case (i)
        Case 0
            r = v
            g = t
            b = p
            Exit Select
        Case 1
            r = q
            g = v
            b = p
            Exit Select
        Case 2
            r = p
            g = v
            b = t
            Exit Select
        Case 3
            r = p
            g = q
            b = v
            Exit Select
        Case 4
            r = t
            g = p
            b = v
            Exit Select
        Case Else   'case 5:
            r = v
            g = p
            b = q
            Exit Select
    End Select
End Sub
share|improve this answer

The EasyRGB has many color space conversions. Here is the code for the RGB->HSV conversion.

share|improve this answer

Your Answer


