PDA

Ver la Versión Completa : Crear gradiente Tono / Saturacion


seoane
08-06-2006, 19:47:45
// B = Brillo (0..255)
procedure Gradiente(Canvas: TCanvas; R: TRect; b: integer);
var
h,s: integer; // Tono, Saturacion, Luminosidad
i,j: integer;
x,y,z: integer;
Bitmap: TBitmap;
begin
Bitmap:= TBitmap.Create;
try
Bitmap.Width:= 360;
Bitmap.Height:= 256;
with Bitmap.Canvas do
begin
Brush.Color:= clBlack;
FillRect(ClipRect);
for h:= 0 to 359 do
begin
i:= ((h mod 60) * b) div 60;
j:= b - i;
for s:= 0 to 255 do
begin
x:= i + ((b-i) * s) div 255;
y:= j + ((b-j) * s) div 255;
z:= (b * s) div 255;
case (h div 60) of
0: Pixels[h,s]:= RGB(b,x,z);
1: Pixels[h,s]:= RGB(y,b,z);
2: Pixels[h,s]:= RGB(z,b,x);
3: Pixels[h,s]:= RGB(z,y,b);
4: Pixels[h,s]:= RGB(x,z,b);
5: Pixels[h,s]:= RGB(b,z,y);
end;
end;
end;
end;
Canvas.StretchDraw(R,Bitmap);
finally
Bitmap.Free;
end;
end;


Ejemplos de uso:

procedure TForm1.Button1Click(Sender: TObject);
begin
// Dibujamos el gradiente en el fondo del formulario
Gradiente(Canvas,Rect(0,0,width,height),255);
end;



begin
// Creamos un gradiente y lo guardamos en un bmp
with TBitmap.Create do
try
Width:= 360;
Height:= 256;
Gradiente(Canvas,Canvas.ClipRect,255);
SaveToFile('d:\gradiente.bmp');
finally
Free;
end;
end;