RGB <=> HSV - Umrechner
Verfasst: 20.01.2015 23:14
Code gelöscht weil der nich mir gehörte.
Code: Alles auswählen
Procedure.d GetMax(a.d, b.d, c.d)
Protected Result.d
Result = a
If b > Result
Result = b
EndIf
If c > Result
Result = c
EndIf
ProcedureReturn Result
EndProcedure
Procedure.d GetMin(a.d, b.d, c.d)
Protected Result.d
Result = a
If b < Result
Result = b
EndIf
If c < Result
Result = c
EndIf
ProcedureReturn Result
EndProcedure
Procedure RGB2HSV(Red, Green, Blue, *H.INTEGER, *S.DOUBLE, *V.DOUBLE)
Protected R.d, G.d, B.d, Max.d, Min.d
Protected H, S.d, V.d
R = Red / 255
G = Green / 255
B = Blue / 255
Max = GetMax(R, G, B)
Min = GetMin(R, G, B)
If Max = Min
*H\i = 0
ElseIf Max = R
*H\i = 60 * ((G - B) / (Max - Min))
ElseIf Max = G
*H\i = 60 * (2 + ((B - R) / (Max - Min)))
Else
*H\i = 60 * (4 + ((R - G) / (Max - Min)))
EndIf
If *H\i < 0
*H\i + 360
EndIf
*S\d = 0.0
If Max > 0.0
*S\d = (Max - Min) / Max
EndIf
*V\d = Max
EndProcedure
Procedure HSV2RGB(H, S.d, V.d, *R.INTEGER, *G.INTEGER, *B.INTEGER)
Protected hi, f.d, p.d, q.d, t.d, R.d, G.d, B.d
hi = H / 60
f = (H / 60) - hi
p = V * (1 - S)
q = V * (1 - S * f)
t = V * (1 - S * (1 - f))
Select hi
Case 0, 6
R = V : G = t : B = p
Case 1
R = q : G = V : B = p
Case 2
R = p : G = V : B = t
Case 3
R = p : G = q : B = V
Case 4
R = t : G = p : B = V
Case 5
R = V : G = p : B = q
EndSelect
*R\i = R * 255
*G\i = G * 255
*B\i = B * 255
EndProcedure
;example
CompilerIf #PB_Compiler_IsMainFile
Procedure main()
Protected Red, Green, Blue, H, S.d, V.d
RGB2HSV(255, 128, 64, @H, @S, @V)
Debug "RGB 255, 128, 64 => H:" + Str(H) + "°, S = " + StrD(S * 100, 1) + "%, V = " + StrD(V * 100, 1) + "%"
HSV2RGB(H, S, V, @Red, @Green, @Blue)
Debug "HSV " + Str(H) + "°, " + StrD(S * 100, 1) + "%, " + StrD(V * 100, 1) + "% => R:" + Str(Red) + ", G:" + Str(Green) + ", B:" + Str(Blue)
RGB2HSV(71, 58, 171, @H, @S, @V)
Debug "RGB 255, 128, 64 => H:" + Str(H) + "°, S = " + StrD(S * 100, 1) + "%, V = " + StrD(V * 100, 1) + "%"
HSV2RGB(H, S, V, @Red, @Green, @Blue)
Debug "HSV " + Str(H) + "°, " + StrD(S * 100, 1) + "%, " + StrD(V * 100, 1) + "% => R:" + Str(Red) + ", G:" + Str(Green) + ", B:" + Str(Blue)
EndProcedure
main()
CompilerEndIf