Get Color Hue - CSharp System.Drawing

CSharp examples for System.Drawing:Color HSV

Description

Get Color Hue

Demo Code


using System.Windows.Media;
using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*from w w w . j  a v  a  2 s. co  m*/

public class Main{
    public static float GetHue(this Color c)
      {
         var r = c.R;
         var g = c.G;
         var b = c.B;
         byte minval = (byte)Math.Min(r, Math.Min(g, b));
         byte maxval = (byte)Math.Max(r, Math.Max(g, b));

         if (maxval == minval)
            return 0.0f;

         float diff = (float)(maxval - minval);
         float rnorm = (maxval - r) / diff;
         float gnorm = (maxval - g) / diff;
         float bnorm = (maxval - b) / diff;

         float hue = 0.0f;
         if (r == maxval)
            hue = 60.0f * (6.0f + bnorm - gnorm);
         if (g == maxval)
            hue = 60.0f * (2.0f + rnorm - bnorm);
         if (b == maxval)
            hue = 60.0f * (4.0f + gnorm - rnorm);
         if (hue > 360.0f)
            hue = hue - 360.0f;

         return hue;

      }
}

Related Tutorials