Get Color Saturation - CSharp System.Drawing

CSharp examples for System.Drawing:Color HSV

Description

Get Color Saturation

Demo Code


using System.Windows.Media;
using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*from w  ww  .  j a  va2s. c  o  m*/

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

         if (maxval == minval)
            return 0.0f;

         int sum = maxval + minval;
         if (sum > 255)
            sum = 510 - sum;

         return (float)(maxval - minval) / sum;
      }
}

Related Tutorials