Hsv to Rgb - CSharp System.Drawing

CSharp examples for System.Drawing:Color RGB

Description

Hsv to Rgb

Demo Code


using System.Text.RegularExpressions;
using System.Globalization;
using System;// w w w.  j  a  va2  s. c o  m
using UnityEngine;

public class Main{
        // http://www.rapidtables.com/convert/color/hsv-to-rgb.htm
      public static Color Hsv2Rgb (float h, float s, float v, float a = 1) {
         h *= 360;
         float r, g, b;

         var c = v * s;
         var x = c * (1 - Mathf.Abs((h / 60) % 2 - 1));
         var m = v - c;

         switch ((int)(h / 60)) {
         case 0: // 0 <= h < 60
            r = c;
            g = x;
            b = 0;
            break;
         case 1: // 60 <= h < 120
            r = x;
            g = c;
            b = 0;
            break;
         case 2: // 120 <= h < 180
            r = 0;
            g = c;
            b = x;
            break;
         case 3: // 180 <= h < 240
            r = 0;
            g = x;
            b = c;
            break;
         case 4: // 240 <= h < 300
            r = x;
            g = 0;
            b = c;
            break;
         case 5: // 300 <= h < 360
            r = c;
            g = 0;
            b = x;
            break;
         default:
            throw new ArithmeticException(h + " is not in the range of 0 <= h < 360");
         }

         r += m;
         g += m;
         b += m;

         return new Color(Clump(r), Clump(g), Clump(b), a);
      }
    public static Color Hsv2Rgb (HsvColor color, float a = 1) {
         return Hsv2Rgb(color.h, color.s, color.v, a);
      }
}

Related Tutorials