Rgb Color to Hsv Color - CSharp System.Drawing

CSharp examples for System.Drawing:Color RGB

Description

Rgb Color to Hsv Color

Demo Code


using System.Text.RegularExpressions;
using System.Globalization;
using System;/* ww w  .  j a  va2  s  .co m*/
using UnityEngine;

public class Main{
        // http://www.rapidtables.com/convert/color/rgb-to-hsv.htm
      public static HsvColor Rgb2Hsv (float r, float g, float b) {
         float h = 0, s, cmin, cmax, delta;

         cmin = Mathf.Min(Mathf.Min(r, b), g);
         cmax = Mathf.Max(Mathf.Max(r, b), g);
         delta = cmax - cmin;

         if (delta == 0)
            h = 6;
         else if (cmax == r)
            h = (b - g) / delta;
         else if (cmax == b)
            h = (g - r) / delta + 2;
         else if (cmax == g)
            h = (r - b) / delta + 4;
         h /= 6;

         if (h <= 0) h += 1f;
         h = 1f - h;

         if (cmax == 0) s = 0;
         else s = delta / cmax;

         return new HsvColor(h, s, cmax);
      }
        public static HsvColor Rgb2Hsv (Color color) {
         return Rgb2Hsv(color.r, color.g, color.b);
      }
}

Related Tutorials