Get Saturation value from Color - CSharp System.Drawing

CSharp examples for System.Drawing:Color HSV

Description

Get Saturation value from Color

Demo Code


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

public class Main{
        public static float GetSaturation(this Color color)
        {
            var r = color.R / 255f;
            var g = color.G / 255f;
            var b = color.B / 255f;

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

            return (max == 0f) ? 0f : 1f - (1f * min / max);
            //var saturation = (max + min) / 2f;
            //if (saturation <= 0.5)
            //    return ((max - min) / (max + min));

            //return ((max - min) / ((2f - max) - min));
        }
}

Related Tutorials