Advertisement
Guest User

Unity HSV Colors

a guest
Nov 28th, 2012
4,556
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static Color ColorFromHSV(float h, float s, float v, float a = 1)
  2. {
  3.     // no saturation, we can return the value across the board (grayscale)
  4.         if (s == 0)
  5.             return new Color(v, v, v, a);
  6.  
  7.         // which chunk of the rainbow are we in?
  8.         float sector = h / 60;
  9.  
  10.         // split across the decimal (ie 3.87 into 3 and 0.87)
  11.         int i = (int)sector;
  12.         float f = sector - i;
  13.  
  14.         float p = v * (1 - s);
  15.         float q = v * (1 - s * f);
  16.         float t = v * (1 - s * (1 - f));
  17.  
  18.         // build our rgb color
  19.         Color color = new Color(0, 0, 0, a);
  20.  
  21.         switch(i)
  22.         {
  23.             case 0:
  24.                 color.r = v;
  25.                 color.g = t;
  26.                 color.b = p;
  27.                 break;
  28.  
  29.             case 1:
  30.                 color.r = q;
  31.                 color.g = v;
  32.                 color.b = p;
  33.                 break;
  34.  
  35.             case 2:
  36.                 color.r  = p;
  37.                 color.g  = v;
  38.                 color.b  = t;
  39.                 break;
  40.  
  41.             case 3:
  42.                 color.r  = p;
  43.                 color.g  = q;
  44.                 color.b  = v;
  45.                 break;
  46.  
  47.             case 4:
  48.                 color.r  = t;
  49.                 color.g  = p;
  50.                 color.b  = v;
  51.                 break;
  52.  
  53.             default:
  54.                 color.r  = v;
  55.                 color.g  = p;
  56.                 color.b  = q;
  57.                 break;
  58.         }
  59.  
  60.         return color;
  61. }
  62.    
  63. public static void ColorToHSV(Color color, out float h, out float s, out float v)
  64. {
  65.     float min = Mathf.Min(Mathf.Min(color.r, color.g), color.b);
  66.         float max = Mathf.Max(Mathf.Max(color.r, color.g), color.b);
  67.         float delta = max - min;
  68.  
  69.         // value is our max color
  70.         v = max;
  71.  
  72.         // saturation is percent of max
  73.         if (!Mathf.Approximately(max, 0))
  74.             s = delta / max;
  75.         else
  76.         {
  77.             // all colors are zero, no saturation and hue is undefined
  78.             s = 0;
  79.             h = -1;
  80.             return;
  81.         }
  82.  
  83.         // grayscale image if min and max are the same
  84.         if (Mathf.Approximately(min, max))
  85.         {
  86.             v = max;
  87.             s = 0;
  88.             h = -1;
  89.             return;
  90.         }
  91.  
  92.         // hue depends which color is max (this creates a rainbow effect)
  93.         if (color.r == max)
  94.             h = (color.g - color.b) / delta;            // between yellow & magenta
  95.         else if (color.g == max)
  96.             h = 2 + (color.b - color.r) / delta;        // between cyan & yellow
  97.         else
  98.             h = 4 + (color.r - color.g) / delta;        // between magenta & cyan
  99.  
  100.         // turn hue into 0-360 degrees
  101.         h *= 60;
  102.         if (h < 0 )
  103.             h += 360;
  104. }
Advertisement
RAW Paste Data Copied
Advertisement