Search code examples
c#colorsrgbhsv

How to Convert RGB Color to HSV?


How can I convert a RGB Color to HSV using C#?
I've searched for a fast method without using any external library.


Solution

  • 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.