title: Algorithm to Switch Between RGB and HSB Color Values
body: <p>I read the article <a href="http://www.devx.com/tips/Tip/41581" rel="noreferrer">Algorithm to Switch Between RGB and HSB Color Values</a></p>
  
  <pre><code>Type RGBColor
       Red As Byte
       Green As Byte
       Blue As Byte
  End Type
  
  Type HSBColor
       Hue As Double
       Saturation As Double
       Brightness As Double
  End Type
  
  Function RGBToHSB(rgb As RGBColor) As HSBColor
       Dim minRGB, maxRGB, Delta As Double
       Dim h, s, b As Double
       h = 0
       minRGB = Min(Min(rgb.Red, rgb.Green), rgb.Blue)
       maxRGB = Max(Max(rgb.Red, rgb.Green), rgb.Blue)
       Delta = (maxRGB - minRGB)
       b = maxRGB
       If (maxRGB &lt;&gt; 0) Then
            s = 255 * Delta / maxRGB
       Else
            s = 0
       End If
       If (s &lt;&gt; 0) Then
            If rgb.Red = maxRGB Then
                 h = (CDbl(rgb.Green) - CDbl(rgb.Blue)) / Delta
            Else
                 If rgb.Green = maxRGB Then
                      h = 2 + (CDbl(rgb.Blue) - CDbl(rgb.Red)) / Delta
                 Else
                      If rgb.Blue = maxRGB Then
                           h = 4 + (CDbl(rgb.Red) - CDbl(rgb.Green)) / Delta
                      End If
                 End If
            End If
       Else
            h = -1
       End If
       h = h * 60
       If h &lt; 0 Then h = h + 360
       RGBToHSB.Hue = h
       RGBToHSB.Saturation = s * 100 / 255
       RGBToHSB.Brightness = b * 100 / 255
  End Function
  
  Function HSBToRGB(hsb As HSBColor) As RGBColor
       Dim maxRGB, Delta As Double
       Dim h, s, b As Double
       h = hsb.Hue / 60
       s = hsb.Saturation * 255 / 100
       b = hsb.Brightness * 255 / 100
       maxRGB = b
       If s = 0 Then
            HSBToRGB.Red = 0
            HSBToRGB.Green = 0
            HSBToRGB.Blue = 0
       Else
            Delta = s * maxRGB / 255
            If h &gt; 3 Then
                 HSBToRGB.Blue = CByte(Round(maxRGB))
                 If h &gt; 4 Then
                      HSBToRGB.Green = CByte(Round(maxRGB - Delta))
                      HSBToRGB.Red = CByte(Round((h - 4) * Delta)) + HSBToRGB.Green
                 Else
                      HSBToRGB.Red = CByte(Round(maxRGB - Delta))
                      HSBToRGB.Green = CByte(HSBToRGB.Red - Round((h - 4) * Delta))
                 End If
            Else
                 If h &gt; 1 Then
                      HSBToRGB.Green = CByte(Round(maxRGB))
                      If h &gt; 2 Then
                           HSBToRGB.Red = CByte(Round(maxRGB - Delta))
                           HSBToRGB.Blue = CByte(Round((h - 2) * Delta)) + HSBToRGB.Red
                      Else
                           HSBToRGB.Blue = CByte(Round(maxRGB - Delta))
                           HSBToRGB.Red = CByte(HSBToRGB.Blue - Round((h - 2) * Delta))
                      End If
                 Else
                      If h &gt; -1 Then
                           HSBToRGB.Red = CByte(Round(maxRGB))
                           If h &gt; 0 Then
                                HSBToRGB.Blue = CByte(Round(maxRGB - Delta))
                                HSBToRGB.Green = CByte(Round(h * Delta)) + HSBToRGB.Blue
                           Else
                                HSBToRGB.Green = CByte(Round(maxRGB - Delta))
                                HSBToRGB.Blue = CByte(HSBToRGB.Green - Round(h * Delta))
                           End If
                      End If
                 End If
            End If
       End If
  End Function
  </code></pre>
  
  <p>Then there was someone who posted that there was a mistake but didn't elaborate much</p>
  
  <blockquote>
    <p>But I think it need to manage when h is more than 5, for example for the color R:130 G:65 B:111</p>
  </blockquote>
  
  <pre><code>If h &gt; 5 Then
      HSBToRGB.Red = CByte(Round(maxRGB))
  If h &gt; 6 Then
      HSBToRGB.Blue= CByte(Round(maxRGB - Delta))
      HSBToRGB.Green= CByte(Round((h - 6) * Delta)) HSBToRGB.Blue
  Else
      HSBToRGB.Green= CByte(Round(maxRGB - Delta))
      HSBToRGB.Blue = CByte(HSBToRGB.Green- Round((h - 6) * Delta))
  End If
  </code></pre>
  
  <p>Do I need to add in that piece of code? And I presume it should go into HSB to RGB (in my C# conversion)</p>
  
  <pre><code>...
  if (s != 0) {
      delta = s * maxRGB / 255;
      if (h &gt; 5)
          rgb.Red = Convert.ToByte(Math.Round(maxRGB));
      if (h &gt; 6)
      {
          rgb.Green = Convert.ToByte(Math.Round(maxRGB - delta));
          rgb.Blue = Convert.ToByte(rgb.Green - Math.Round((h - 6) * delta));
      }
      if (h &gt; 3)
      {
          ...
  </code></pre>
  
  <p>also, should it be like above, or</p>
  
  <pre><code>if (h &gt; 6) { } 
  else if (h &gt; 3)  { }
  </code></pre>