Advertisement
Pastemaster_Alpha

RGB and HSV conversions

Aug 21st, 2014
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.29 KB | None | 0 0
  1. //This function finds the max value of a range of floats.
  2. private float Max(params float[] numbers)
  3. {
  4.     float max = Single.MinValue;
  5.  
  6.     foreach (float num in numbers)
  7.     {
  8.         if (num > max)
  9.         {
  10.             max = num;
  11.         }
  12.     }
  13.    
  14.     return max;
  15. }
  16.  
  17. //This function finds the min value of a range of floats.
  18. private float Min(params float[] numbers)
  19. {
  20.     float min = Single.MaxValue;
  21.  
  22.     foreach (float num in numbers)
  23.     {
  24.         if (num < min)
  25.         {
  26.             min = num;
  27.         }
  28.     }
  29.    
  30.     return min;
  31. }
  32.  
  33. void RgbToHsv(float r, float g, float b, out float h, out float s, out float v)
  34. {
  35.     float min, max, delta;
  36.  
  37.     min = Min(r, g, b);
  38.     max = Max(r, g, b);
  39.     v = max;
  40.  
  41.     delta = max - min;
  42.  
  43.     if (max != 0)
  44.     {
  45.         s = delta / max;
  46.     }
  47.     else
  48.     {
  49.         s = 0;
  50.         h = 0;
  51.         return;
  52.     }
  53.  
  54.     if (r == max)
  55.     {
  56.         h = (g - b) / delta; // between yellow & magenta
  57.     }
  58.     else if (g == max)
  59.     {
  60.         h = 2 + (b - r) / delta; // between cyan & yellow
  61.     }
  62.     else
  63.     {
  64.         h = 4 + (r - g) / delta; // between magenta & cyan
  65.     }
  66.     h *= 60; // degrees
  67.     if (h < 0)
  68.     {
  69.         h += 360;
  70.     }
  71. }
  72.  
  73. void HsvToRgb(out float r, out float g, out float b, float h, float s, float v)
  74. {
  75.     int i;
  76.     float f, p, q, t;
  77.  
  78.     //if there is no saturation, return a shade of gray.
  79.     if (s == 0)
  80.     {        
  81.         r = v;
  82.         g = v;
  83.         b = v;
  84.         return;
  85.     }
  86.  
  87.     h /= 60; // sector 0 to 5
  88.     i = (int)Math.Floor(h);
  89.     f = h - i; // factorial part of h
  90.     p = v * (1 - s);
  91.     q = v * (1 - s * f);
  92.     t = v * (1 - s * (1 - f));
  93.  
  94.     switch (i)
  95.     {
  96.         case 0:
  97.             r = v;
  98.             g = t;
  99.             b = p;
  100.             break;
  101.         case 1:
  102.             r = q;
  103.             g = v;
  104.             b = p;
  105.             break;
  106.         case 2:
  107.             r = p;
  108.             g = v;
  109.             b = t;
  110.             break;
  111.         case 3:
  112.             r = p;
  113.             g = q;
  114.             b = v;
  115.             break;
  116.         case 4:
  117.             r = t;
  118.             g = p;
  119.             b = v;
  120.             break;
  121.         default:
  122.             r = v;
  123.             g = p;
  124.             b = q;
  125.             break;
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement