title: RGB to HSL, hue calculation is wrong
body: <p>I'm trying to convert a RGB32 value to HSL because I want to use the Hue component.</p>
  
  <p>I have used some examples that I found online to create this class:</p>
  
  <pre><code>    public class HSLColor
      {
          public Double Hue;
          public Double Saturation;
          public Double Luminosity;
  
          public HSLColor(Double H, Double S, Double L)
          {
              Hue = H;
              Saturation = S;
              Luminosity = L;
          }
  
          public static HSLColor FromRGB(Color Clr)
          {
              return FromRGB(Clr.R, Clr.G, Clr.B);
          }
  
          public static HSLColor FromRGB(Byte R, Byte G, Byte B)
          {
              Double _R = (R / 255d);
              Double _G = (G / 255d);
              Double _B = (B / 255d);
  
              Double _Min = Math.Min(Math.Min(_R, _G), _B);
              Double _Max = Math.Max(Math.Max(_R, _G), _B);
              Double _Delta = _Max - _Min;
  
              Double H = 0;
              Double S = 0;
              Double L = (float)((_Max + _Min) / 2.0f);
  
              if (_Delta != 0)
              {
                  if (L &lt; 0.5d)
                  {
                      S = (float)(_Delta / (_Max + _Min));
                  }
                  else
                  {
                      S = (float)(_Delta / (2.0f - _Max - _Min));
                  }
  
  
                  if (_R == _Max)
                  {
                      H = (_G - _B) / _Delta;
                  }
                  else if (_G == _Max)
                  {
                      H = 2f + (_B - _R) / _Delta;
                  }
                  else if (_B == _Max)
                  {
                      H = 4f + (_R - _G) / _Delta;
                  }
              }
  
              //Convert to degrees
              H = H * 60d;
              if (H &lt; 0) H += 360;
              //Convert to percent
              S *= 100d;
              L *= 100d;
  
              return new HSLColor(H, S, L);
          }
  
          private Double Hue_2_RGB(Double v1, Double v2, Double vH)
          {
              if (vH &lt; 0) vH += 1;
              if (vH &gt; 1) vH -= 1;
              if ((6.0d * vH) &lt; 1) return (v1 + (v2 - v1) * 6 * vH);
              if ((2.0d * vH) &lt; 1) return (v2);
              if ((3.0d * vH) &lt; 2) return (v1 + (v2 - v1) * ((2.0d / 3.0d) - vH) * 6.0d);
              return (v1);
          }
  
          public Color ToRGB()
          {
              Color Clr = new Color();
              Double var_1, var_2;
  
              if (Saturation == 0)
              {
                  Clr.R = (Byte)(Luminosity * 255);
                  Clr.G = (Byte)(Luminosity * 255);
                  Clr.B = (Byte)(Luminosity * 255);
              }
              else
              {
                  if (Luminosity &lt; 0.5) var_2 = Luminosity * (1 + Saturation);
                  else var_2 = (Luminosity + Saturation) - (Saturation * Luminosity);
  
                  var_1 = 2 * Luminosity - var_2;
  
                  Clr.R = (Byte)(255 * Hue_2_RGB(var_1, var_2, Hue + (1 / 3)));
                  Clr.G = (Byte)(255 * Hue_2_RGB(var_1, var_2, Hue));
                  Clr.B = (Byte)(255 * Hue_2_RGB(var_1, var_2, Hue - (1 / 3)));
              }
  
  
  
              return Clr;
          }
      }
  </code></pre>
  
  <p>However it doesn't seem to work correctly,</p>
  
  <p>If I use an input color of <code>(R 0, G 255, B 193)</code> for example:
  I get <code>Hue = 0</code>
  while in photoshop if I choose the exact same RGB values I get:
   <code>Hue = 165</code>
  which is the correct value.</p>
  
  <p>I want the Hue to be a value ranging from 0 to 360 or 0 to 240</p>
  
  <p>What is the problem?..</p>
  
  <p>Reference:
  <a href="http://www.easyrgb.com/index.php?X=MATH&amp;H=18#text18" rel="nofollow">EasyRGB RGB->HSL</a></p>