Registriert seit: 24. Jul 2003
Ort: Kassel
138 Beiträge
Delphi 6 Professional
|
Re: RGB nach HSV und zurück
27. Jul 2005, 03:42
Hi,
eine funktion die ich mir dafür mal gemacht hab:
Delphi-Quellcode:
TColorRGB = Record
public
R, G, B: Extended;
end;
TColorHSV = Record
H, S, V: Extended;
end;
function RGBtoHSV(R, G, B: Extended): TColorHSV;
function Min(V1, V2, V3: Extended): Extended;
begin
Result:=V1;
if V2 < Result then Result:=V2;
if V3 < Result then Result:=V3;
end;
function Max(V1, V2, V3: Extended): Extended;
begin
Result:=V1;
if V2 > Result then Result:=V2;
if V3 > Result then Result:=V3;
end;
var
deltaValue, minValue, maxValue: Extended;
deltaR, deltaG, deltaB: Extended;
begin
minValue:=Min(R, G, B);
maxValue:=Max(R, G, B);
deltaValue:=maxValue - minValue;
Result.V:=maxValue;
if deltaValue = 0 then begin
Result.H:=0;
Result.S:=0;
end else begin
Result.S:=deltaValue / maxValue;
deltaR:=(((maxValue - R) / 6) + (deltaValue / 2 )) / deltaValue;
deltaG:=(((maxValue - G) / 6) + (deltaValue / 2 )) / deltaValue;
deltaB:=(((maxValue - B) / 6) + (deltaValue / 2 )) / deltaValue;
if R = maxValue then
Result.H:=deltaB - deltaG
else if G = maxValue then
Result.H:=(1 / 3) + deltaR - deltaB
else if B = maxValue then
Result.H:=(2 / 3) + deltaG - deltaR;
if Result.H < 0 then
Result.H:=Result.H + 1;
if Result.H > 1 then
Result.H:=Result.H - 1;
end;
end;
function HSVtoRGB(H, S, V: Extended): TColorRGB;
var
var_h, var_i, var_1, var_2, var_3: Extended;
begin
if S = 0 then begin
Result.R:=V * 255;
Result.G:=V * 255;
Result.B:=V * 255;
end else begin
var_h:=H * 6;
var_i:=floor(var_h);
var_1:=V * (1 - S);
var_2:=V * (1 - S * (var_h - var_i));
var_3:=V * (1 - S * (1 - (var_h - var_i)));
if var_i = 0 then begin
Result.R:=V;
Result.G:=var_3;
Result.B:=var_1;
end else if var_i = 1 then begin
Result.R:=var_2;
Result.G:=V;
Result.B:=var_1;
end else if var_i = 2 then begin
Result.R:=var_1;
Result.G:=V;
Result.B:=var_3;
end else if var_i = 3 then begin
Result.R:=var_1;
Result.G:=var_2;
Result.B:=V;
end else if var_i = 4 then begin
Result.R:=var_3;
Result.G:=var_1;
Result.B:=V;
end else begin
Result.R:=V;
Result.G:=var_1;
Result.B:=var_2;
end;
Result.R:=Result.R * 255;
Result.G:=Result.G * 255;
Result.B:=Result.B * 255;
end;
end;
Au'revoir,
Aya~
I aim for my endless dreams and I know they will come true!
S: String = #86^)^3^)^4#58#32^(^4^4^0#58#47#47^7^7^7#46^+^/^3^(^)^'^!^9^!#46^$^%;
|
|
Zitat
|