From: Paul D B on
Hi all,

anyone who has got a couple of VO-functions in his drawer to convert
from RGB color to HSL and back?
Would help me.

TIA

--
Paul

From: Norbert Kolb on
Try this link

http://www.easyrgb.com/math.php?MATH=M18#text18

Norbert


From: johan.nel on
Hi Paul,

I hacked this from somewhere I cannot remember so not sure it work but
you can try, sorry for no indentation:
FUNCTION RGB2HSV(nR, nG, nB) AS ARRAY
LOCAL nDelta, nMin, nV, nS, nH AS REAL8
LOCAL aHSV AS ARRAY
nMin := Min(nR, Min(nG, nB))
nV := Max(nR, Max(nG, nB))
nDelta := nV - nMin
BEGIN SEQUENCE
IF nV = 0.0
nS := 0.0
ELSE
nS := nDelta / nV
ENDIF
IF nS = 0.0
aHSV := {NIL, nS, nV}
BREAK // nH undefined
ELSEIF nR = nV
nH := 60.0 * (nG - nB) / nDelta
ELSEIF nG = nV
nH := 120.0 + 60.0 * (nB - nR) / nDelta
ELSEIF nB = nV
nH := 240.0 + 60.0 * (nR - nG) / nDelta
ENDIF
IF nH < 0.0
nH := nH + 360.0
ENDIF
aHSV := {nH, nS, nV}
END SEQUENCE
RETURN aHSV

FUNCTION HSV2RGB(nH, nS, nV) AS ARRAY
LOCAL nF, nHTemp, nP, nQ, nT, nR, nG, nB AS REAL8
LOCAL aRGB AS ARRAY
LOCAL nI AS INT
BEGIN SEQUENCE
IF nS = 0.0
IF nH = NIL
nR := nV
nG := nV
nB := nV
ELSE
aRGB := {NIL, NIL, NIL}
BREAK
ENDIF
ELSE
IF nH = 360.0
nHTemp := 0.0
ELSE
nHTemp := nH
ENDIF
nHTemp := nHTemp / 60
nI := Floor(nHTemp)
nF := nHTemp - nI
nP := nV * (1.0 - nS)
nQ := nV * (1.0 - (nS * nF))
nT := nV * (1.0 - (nS * (1.0 - nF)))
DO CASE
CASE nI = 0
nR := nV; nG := nT; nB := nP
CASE nI = 1
nR := nQ; nG := nV; nB := nP
CASE nI = 2
nR := nP; nG := nV; nB := nT
CASE nI = 3
nR := nP; nG := nQ; nB := nV
CASE nI = 4
nR := nT; nG := nP; nB := nV
CASE nI = 5
nR := nV; nG := nP; nB := nQ
ENDCASE
ENDIF
aRGB := {nR, nG, nB}
END SEQUENCE
RETURN aRGB

HTH,

Johan Nel
Pretoria, South Africa.
From: Paul D B on
Norbert Kolb wrote:
> Try this link
>
> http://www.easyrgb.com/math.php?MATH=M18#text18
>
> Norbert

Thanks, but I knew this resource, and I had already programmed it in VO.
But it didn't work as it should.

--
Paul

From: Paul D B on
johan.nel(a)xsinet.co.za wrote:
> Hi Paul,
>
> I hacked this from somewhere I cannot remember so not sure it work but
> you can try, sorry for no indentation:
> FUNCTION RGB2HSV(nR, nG, nB) AS ARRAY
> LOCAL nDelta, nMin, nV, nS, nH AS REAL8
> LOCAL aHSV AS ARRAY
> nMin := Min(nR, Min(nG, nB))
> nV := Max(nR, Max(nG, nB))
> nDelta := nV - nMin
> BEGIN SEQUENCE
> IF nV = 0.0
> nS := 0.0
> ELSE
> nS := nDelta / nV
> ENDIF
> IF nS = 0.0
> aHSV := {NIL, nS, nV}
> BREAK // nH undefined
> ELSEIF nR = nV
> nH := 60.0 * (nG - nB) / nDelta
> ELSEIF nG = nV
> nH := 120.0 + 60.0 * (nB - nR) / nDelta
> ELSEIF nB = nV
> nH := 240.0 + 60.0 * (nR - nG) / nDelta
> ENDIF
> IF nH < 0.0
> nH := nH + 360.0
> ENDIF
> aHSV := {nH, nS, nV}
> END SEQUENCE
> RETURN aHSV
>
> FUNCTION HSV2RGB(nH, nS, nV) AS ARRAY
> LOCAL nF, nHTemp, nP, nQ, nT, nR, nG, nB AS REAL8
> LOCAL aRGB AS ARRAY
> LOCAL nI AS INT
> BEGIN SEQUENCE
> IF nS = 0.0
> IF nH = NIL
> nR := nV
> nG := nV
> nB := nV
> ELSE
> aRGB := {NIL, NIL, NIL}
> BREAK
> ENDIF
> ELSE
> IF nH = 360.0
> nHTemp := 0.0
> ELSE
> nHTemp := nH
> ENDIF
> nHTemp := nHTemp / 60
> nI := Floor(nHTemp)
> nF := nHTemp - nI
> nP := nV * (1.0 - nS)
> nQ := nV * (1.0 - (nS * nF))
> nT := nV * (1.0 - (nS * (1.0 - nF)))
> DO CASE
> CASE nI = 0
> nR := nV; nG := nT; nB := nP
> CASE nI = 1
> nR := nQ; nG := nV; nB := nP
> CASE nI = 2
> nR := nP; nG := nV; nB := nT
> CASE nI = 3
> nR := nP; nG := nQ; nB := nV
> CASE nI = 4
> nR := nT; nG := nP; nB := nV
> CASE nI = 5
> nR := nV; nG := nP; nB := nQ
> ENDCASE
> ENDIF
> aRGB := {nR, nG, nB}
> END SEQUENCE
> RETURN aRGB
>
> HTH,
>
> Johan Nel
> Pretoria, South Africa.

Thanks Johan. But it doesn't work. When I convert a RGB color to HSV, do
nothing with it and just convert it back to HSV, I get 0,0,0 (black. ).
I'll play with it this afternoon to see what goes wrong.




--
Paul