Get Hue value from Color - CSharp System.Drawing

CSharp examples for System.Drawing:Color HSV

Description

Get Hue value from Color

Demo Code


using System.Windows.Media;
using System;/*w  w w  . j a  v a 2s.c o  m*/

public class Main{
        public static float GetHue(this Color color)
        {
            if (color.R == color.G && color.G == color.B)
                return 0;

            var r = color.R / 255f;
            var g = color.G / 255f;
            var b = color.B / 255f;

            float hue;

            var min = Min(r, g, b);
            var max = Max(r, g, b);

            var delta = max - min;

            if (r == max)
                hue = (g - b) / delta; // between yellow & magenta
            else if (g == max)
                hue = 2 + (b - r) / delta; // between cyan & yellow
            else
                hue = 4 + (r - g) / delta; // between magenta & cyan

            hue *= 60; // degrees

            if (hue < 0)
                hue += 360;

            return hue;
        }
}

Related Tutorials