Advertisement
Guest User

Untitled

a guest
Jun 24th, 2015
986
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public static class UtilsColor
  5. {
  6.     public static void RGBToHSV(Color rgbColor, out float H, out float S, out float V)
  7.     {
  8.         if (rgbColor.b > rgbColor.g && rgbColor.b > rgbColor.r)
  9.         {
  10.             UtilsColor.RGBToHSVHelper(4f, rgbColor.b, rgbColor.r, rgbColor.g, out H, out S, out V);
  11.         }
  12.         else if (rgbColor.g > rgbColor.r)
  13.         {
  14.             UtilsColor.RGBToHSVHelper(2f, rgbColor.g, rgbColor.b, rgbColor.r, out H, out S, out V);
  15.         }
  16.         else
  17.         {
  18.             UtilsColor.RGBToHSVHelper(0f, rgbColor.r, rgbColor.g, rgbColor.b, out H, out S, out V);
  19.         }
  20.     }
  21.  
  22.     private static void RGBToHSVHelper(float offset, float dominantcolor, float colorone, float colortwo, out float H, out float S, out float V)
  23.     {
  24.         V = dominantcolor;
  25.         if (V != 0f)
  26.         {
  27.             float num;
  28.             if (colorone > colortwo)
  29.             {
  30.                 num = colortwo;
  31.             }
  32.             else
  33.             {
  34.                 num = colorone;
  35.             }
  36.             float num2 = V - num;
  37.             if (num2 != 0f)
  38.             {
  39.                 S = num2 / V;
  40.                 H = offset + (colorone - colortwo) / num2;
  41.             }
  42.             else
  43.             {
  44.                 S = 0f;
  45.                 H = offset + (colorone - colortwo);
  46.             }
  47.             H /= 6f;
  48.             if (H < 0f)
  49.             {
  50.                 H += 1f;
  51.             }
  52.         }
  53.         else
  54.         {
  55.             S = 0f;
  56.             H = 0f;
  57.         }
  58.     }
  59.  
  60.     public static Color HSVToRGB(float H, float S, float V)
  61.     {
  62.         Color white = Color.white;
  63.         if (S == 0f)
  64.         {
  65.             white.r = V;
  66.             white.g = V;
  67.             white.b = V;
  68.         }
  69.         else if (V == 0f)
  70.         {
  71.             white.r = 0f;
  72.             white.g = 0f;
  73.             white.b = 0f;
  74.         }
  75.         else
  76.         {
  77.             white.r = 0f;
  78.             white.g = 0f;
  79.             white.b = 0f;
  80.             float num = H * 6f;
  81.             int num2 = (int)Mathf.Floor(num);
  82.             float num3 = num - (float)num2;
  83.             float num4 = V * (1f - S);
  84.             float num5 = V * (1f - S * num3);
  85.             float num6 = V * (1f - S * (1f - num3));
  86.             int num7 = num2;
  87.             switch (num7 + 1)
  88.             {
  89.                 case 0:
  90.                     white.r = V;
  91.                     white.g = num4;
  92.                     white.b = num5;
  93.                     break;
  94.                 case 1:
  95.                     white.r = V;
  96.                     white.g = num6;
  97.                     white.b = num4;
  98.                     break;
  99.                 case 2:
  100.                     white.r = num5;
  101.                     white.g = V;
  102.                     white.b = num4;
  103.                     break;
  104.                 case 3:
  105.                     white.r = num4;
  106.                     white.g = V;
  107.                     white.b = num6;
  108.                     break;
  109.                 case 4:
  110.                     white.r = num4;
  111.                     white.g = num5;
  112.                     white.b = V;
  113.                     break;
  114.                 case 5:
  115.                     white.r = num6;
  116.                     white.g = num4;
  117.                     white.b = V;
  118.                     break;
  119.                 case 6:
  120.                     white.r = V;
  121.                     white.g = num4;
  122.                     white.b = num5;
  123.                     break;
  124.                 case 7:
  125.                     white.r = V;
  126.                     white.g = num6;
  127.                     white.b = num4;
  128.                     break;
  129.             }
  130.             white.r = Mathf.Clamp(white.r, 0f, 1f);
  131.             white.g = Mathf.Clamp(white.g, 0f, 1f);
  132.             white.b = Mathf.Clamp(white.b, 0f, 1f);
  133.         }
  134.         return white;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement