Convertire il valore di un colore da RGB in HSV

Da Gambas-it.org - Wikipedia.

Per convertire il valore di un colore da formato RGB in formato HSV, è possibile utilizzare il seguente codice:

Public Sub Main()
 
 Dim rgb As Integer[]
 Dim h, s, v As Integer
 
  rgb = [0, 255, 255]
  
  Da_rgb_a_hsv(rgb, VarPtr(h), VarPtr(s), VarPtr(v))
  
  Print "hue =        "; h
  Print "saturation = "; s
  Print "value =      "; v

End


Private Function Da_rgb_a_hsv(colore As Integer[], hp As Pointer, sp As Pointer, vp As Pointer)
 
 Dim k, x, f, i, j As Integer
 Dim stH, stS, stV As Stream
 
  x = colore[0]
  
  If colore[1] < x Then x = colore[1]
  If colore[2] < x Then x = colore[2]
  
  k = colore[0]
  If colore[1] > k Then k = colore[1]
  If colore[2] > k Then k = colore[2]
  
  stH = Memory hp For Write
  stS = Memory sp For Write
  stV = Memory vp For Write
  If k = x Then
    Write #stH, 0 As Integer
    Write #stS, 0 As Integer
    Write #stV, k As Integer
  Else
    f = If(colore[0] = x, colore[1] - colore[2], If(colore[1] = x, colore[2] - colore[0], colore[0] - colore[1]))
    i = If(colore[0] = x, 3, If(colore[1] = x, 5, 1))
    j = CInt((i - CFloat(f) / (k - x)) * 60)
    If j = 360 Then
      Write #stH, 0 As Integer
    Else
      Write #stH, j As Integer
    Endif
    Write #stS, ((k - x) * 255) / k As Integer
    Write #stV, k As Integer
  Endif
  
  stH.Close
  stS.Close
  stV.Close
  
End