Get HSV Saturation from R G B value - CSharp System.Drawing

CSharp examples for System.Drawing:Color HSV

Description

Get HSV Saturation from R G B value

Demo Code


using System.Windows.Media;

public class Main{
        /// <summary>
      /// Get HSV Saturation
      /// </summary>
      /// <param name="red">RGB -> R (0..256)</param>
      /// <param name="green">RGB -> G (0..256)</param>
      /// <param name="blue">RGB -> B (0..256)</param>
      /// <returns>HSV -> S (0..360)</returns>
      public static double GetHue(int red, int green, int blue)
      {/*w ww. j  a va2 s  .  com*/
         int min = System.Math.Min(System.Math.Min(red, green), blue);
         int max = System.Math.Max(System.Math.Max(red, green), blue);

         if (min == max)
            return 0;

         double hue;
         if (max == red)
         {
            hue = (green - blue * 1.0) / (max - min);

         }
         else if (max == green)
         {
            hue = 2f + (blue - red * 1.0) / (max - min);

         }
         else
         {
            hue = 4f + (red - green * 1.0) / (max - min);
         }

         hue = hue * 60;
         if (hue < 0)
            hue = hue + 360;

         return hue;
      }
}

Related Tutorials