请教如何才能用程序生成一条象PHotoshop里的那样一条完整的颜色带,从红到紫的那条(100分)

  • 主题发起人 主题发起人 czzw
  • 开始时间 开始时间
rgb(r1,g1,b1) to rgb(r2,g2,b2) in 50 steps:
for (i = 0 ; i < 50 ; i++)
{
thisrgb = rgb(r1 + (r2 - r1) / 50 * i, g1 + (g2 - g1) / 50 * i, b1 + (b2 - b1) / 50 * i)
}

OK?
 
续mikedekings
……然后再画到Canvas上
for I := 1 to 10 do begin
for J := 1 to 10 do begin
Canvas.Pixels(I,J) := thisrgb;
end
end;

:))))
 
procedure TForm1.FormPaint(Sender: TObject);
var i:word;
dy,y:real;
begin
dy:=clientheight/256;
y:=0;
for i:=255 downto 0 do
begin
canvas.brush.color:=$00000000+i*$10000;
canvas.fillrect(rect(0,round(y),clientwidth,round(y+dy)));
y:=y+dy;
end;
 
以上代码均是从一种颜色递变到另外一种颜色。我想czzw的意思是产生一条完整色谱
即HSB模式的H 从0->360 ,我也一直想找关于HSB至RGB的互换公式。有谁知道?
 
你要求的实际是我做过的彩虹云纹,从红色到紫色的颜色渐变。我有任意形状区域的任意方向的
渐变,你有兴趣么?来信。
 
卷起千堆雪tyn:
能说说你的算法吗?
 
所谓渐变就是对不同的像素点赋不同的r,g,b值。
对任意区域里的点进行投影,每一类投影点赋一定的色彩,比如从前景色到背景色的渐变,需要考虑
这2种色彩的各自的r,g,b,从而得到具体的r,g,b.
 
同意楼上
 
这里有些不同的渐变效果,你看看,
unit rbgradutils;

interface

uses Windows, Classes, Graphics, forms, Controls, Dialogs;

type
TGradientFillType = (rgsHorizontal, rgsVertical, rgsElliptic, rgsRectangle,
rgsVerticalCenter,
rgsHorizontalCenter, rgsNWSE, rgsNWSW, rgsSENW, rgsSWNE, rgsSweet,
rgsStrange, rgsNeo);

procedure RbsGradientFill(Canvas: TCanvas; grdType: TGradientFillType; fromCol:
TColor;
toCol: TColor; ARect: TRect);

implementation

procedure RbsGradientFill(Canvas: TCanvas; grdType: TGradientFillType; fromCol:
TColor;
toCol: TColor; ARect: TRect);
var
FromR, FromG, FromB: Integer;
DiffR, DiffG, DiffB: Integer;

i: integer;
bm: TBitmap;
ColorRect: TRect;
R, G, B: Byte;

//for elliptical
Pw, Ph: Real;
x0, y0, x1, y1, x2, y2, x3, y3: Real;
points: array[0..3] of TPoint;
haf: Integer;

begin
//set bitmap
bm := TBitmap.Create;
bm.Width := ARect.Right;
bm.Height := ARect.Bottom;

//calc colors
FromR := fromcol and $000000FF; //Strip out separate RGB values
FromG := (fromcol shr 8) and $000000FF;
FromB := (fromcol shr 16) and $000000FF;
DiffR := (tocol and $000000FF) - FromR; //Find the difference
DiffG := ((tocol shr 8) and $000000FF) - FromG;
DiffB := ((tocol shr 16) and $000000FF) - FromB;

//draw gradient
case grdType of
rgsHorizontal:
begin
ColorRect.Top := 0; //Set rectangle top
ColorRect.Bottom := bm.Height;
for I := 0 to 255 do
begin //Make lines (rectangles) of color
ColorRect.Left := MulDiv(I, bm.Width, 256);
//Find left for this color
ColorRect.Right := MulDiv(I + 1, bm.Width, 256); //Find Right
R := fromR + MulDiv(I, diffr, 255); //Find the RGB values
G := fromG + MulDiv(I, diffg, 255);
B := fromB + MulDiv(I, diffb, 255);
bm.Canvas.Brush.Color := RGB(R, G, B); //Plug colors into brush
bm.Canvas.FillRect(ColorRect); //Draw on Bitmap
end;

end;
rgsVertical:
begin
ColorRect.Left := 0; //Set rectangle left&amp;right
ColorRect.Right := bm.Width;
for I := 0 to 255 do
begin //Make lines (rectangles) of color
ColorRect.Top := MulDiv(I, bm.Height, 256);
//Find top for this color
ColorRect.Bottom := MulDiv(I + 1, bm.Height, 256); //Find Bottom
R := fromr + MulDiv(I, diffr, 255); //Find the RGB values
G := fromg + MulDiv(I, diffg, 255);
B := fromb + MulDiv(I, diffb, 255);
bm.Canvas.Brush.Color := RGB(R, G, B); //Plug colors into brush
bm.Canvas.FillRect(ColorRect); //Draw on Bitmap
end;

end;
rgsElliptic:
begin
bm.Canvas.Pen.Style := psClear;
bm.Canvas.Pen.Mode := pmCopy;
x1 := 0 - (bm.Width / 4);
x2 := bm.Width + (bm.Width / 4) + 4;
y1 := 0 - (bm.Height / 4);
y2 := bm.Height + (bm.Height / 4) + 4;
Pw := ((bm.Width / 4) + (bm.Width / 2)) / 155;
Ph := ((bm.Height / 4) + (bm.Height / 2)) / 155;
for I := 0 to 155 do
begin //Make ellipses of color
x1 := x1 + Pw;
x2 := X2 - Pw;
y1 := y1 + Ph;
y2 := y2 - Ph;
R := fromr + MulDiv(I, diffr, 155); //Find the RGB values
G := fromg + MulDiv(I, diffg, 155);
B := fromb + MulDiv(I, diffb, 155);
bm.Canvas.Brush.Color := R or (G shl 8) or (b shl 16);
//Plug colors into brush
bm.Canvas.Ellipse(Trunc(x1), Trunc(y1), Trunc(x2), Trunc(y2));
end;
end;

rgsRectangle:
begin
bm.Canvas.Pen.Style := psClear;
bm.Canvas.Pen.Mode := pmCopy;
x1 := 0;
x2 := bm.Width + 2;
y1 := 0;
y2 := bm.Height + 2;
Pw := (bm.Width / 2) / 255;
Ph := (bm.Height / 2) / 255;
for I := 0 to 255 do
begin //Make rectangles of color
x1 := x1 + Pw;
x2 := X2 - Pw;
y1 := y1 + Ph;
y2 := y2 - Ph;
R := fromr + MulDiv(I, diffr, 255); //Find the RGB values
G := fromg + MulDiv(I, diffg, 255);
B := fromb + MulDiv(I, diffb, 255);
bm.Canvas.Brush.Color := RGB(R, G, B); //Plug colors into brush
bm.Canvas.FillRect(Rect(Trunc(x1), Trunc(y1), Trunc(x2),
Trunc(y2)));
end;
end;

rgsVerticalCenter:
begin
Haf := bm.Height div 2;
ColorRect.Left := 0;
ColorRect.Right := bm.Width;
for I := 0 to Haf do
begin
ColorRect.Top := MulDiv(I, Haf, Haf);
ColorRect.Bottom := MulDiv(I + 1, Haf, Haf);
R := fromr + MulDiv(I, diffr, Haf);
G := fromg + MulDiv(I, diffg, Haf);
B := fromb + MulDiv(I, diffb, Haf);
bm.Canvas.Brush.Color := RGB(R, G, B);
bm.Canvas.FillRect(ColorRect);
ColorRect.Top := bm.Height - (MulDiv(I, Haf, Haf));
ColorRect.Bottom := bm.Height - (MulDiv(I + 1, Haf, Haf));
bm.Canvas.FillRect(ColorRect);
end;

end;
rgsHorizontalCenter:
begin
Haf := bm.Width div 2;
ColorRect.Top := 0;
ColorRect.Bottom := bm.Height;
for I := 0 to Haf do
begin
ColorRect.Left := MulDiv(I, Haf, Haf);
ColorRect.Right := MulDiv(I + 1, Haf, Haf);
R := fromr + MulDiv(I, diffr, Haf);
G := fromg + MulDiv(I, diffg, Haf);
B := fromb + MulDiv(I, diffb, Haf);
bm.Canvas.Brush.Color := RGB(R, G, B);
bm.Canvas.FillRect(ColorRect);
ColorRect.Left := bm.Width - (MulDiv(I, Haf, Haf));
ColorRect.Right := bm.Width - (MulDiv(I + 1, Haf, Haf));
bm.Canvas.FillRect(ColorRect);
end;
end;
rgsNWSE:
begin
bm.canvas.Pen.Style := psclear;
bm.canvas.Pen.Mode := pmCopy;
Pw := (bm.Width + bm.height) / 255;
for I := 0 to 254 do
begin //Make trapeziums of color
x0 := i * Pw;
if (x0 < bm.width) then
y0 := 0
else
begin
y0 := x0 - bm.width;
x0 := bm.width - 1;
end;
x1 := (i + 1) * pw;
if (x1 < bm.width) then
begin
y1 := 0;
end
else
begin
y1 := x1 - bm.width;
x1 := bm.width - 1;
end;
y2 := i * pw;
if (y2 < bm.height) then
x2 := 0
else
begin
x2 := y2 - bm.height;
y2 := bm.height - 1;
end;
y3 := (i + 1) * pw;
if (y3 < bm.height) then
x3 := 0
else
begin
x3 := y3 - bm.height;
y3 := bm.height - 1;
end;
R := fromr + MulDiv(I, diffr, 255); //Find the RGB values
G := fromg + MulDiv(I, diffg, 255);
B := fromb + MulDiv(I, diffb, 255);
bm.canvas.Brush.Color := RGB(R, G, B); //Plug colors into brush
points[0] := point(Trunc(x0), Trunc(y0));
points[1] := point(Trunc(x1), Trunc(y1));
points[3] := point(Trunc(x2), Trunc(y2));
points[2] := point(Trunc(x3), Trunc(y3));
bm.canvas.polygon(points);
end;
end;

rgsNWSW:
begin
bm.canvas.Pen.Style := psclear;
bm.canvas.Pen.Mode := pmCopy;
Pw := (bm.width + bm.height) / 255;
for I := 0 to 254 do
begin //Make trapeziums of color
y0 := i * Pw;
if (y0 < bm.height) then
x0 := bm.width - 1
else
begin
x0 := bm.width - 1 - (y0 - bm.height);
y0 := bm.height - 1;
end;
y1 := (i + 1) * pw;
if (y1 < bm.height) then
x1 := bm.width - 1
else
begin
x1 := bm.width - 1;
end;
x2 := bm.width - 1 - (i * pw);
if (x2 > 0) then
y2 := 0
else
begin
y2 := -x2;
x2 := 0;
end;
x3 := bm.width - 1 - ((i + 1) * pw);
if (x3 > 0) then
y3 := 0
else
begin
y3 := -x3;
x3 := 0;
end;
R := fromr + MulDiv(I, diffr, 255); //Find the RGB values
G := fromg + MulDiv(I, diffg, 255);
B := fromb + MulDiv(I, diffb, 255);
bm.canvas.Brush.Color := RGB(R, G, B); //Plug colors into brush
points[0] := point(Trunc(x0), Trunc(y0));
points[1] := point(Trunc(x1), Trunc(y1));
points[3] := point(Trunc(x2), Trunc(y2));
points[2] := point(Trunc(x3), Trunc(y3));
bm.canvas.polygon(points);
end;
end;

rgsSENW:
begin
bm.canvas.Pen.Style := psclear;
bm.canvas.Pen.Mode := pmCopy;
Pw := (bm.width + bm.height) / 255;
for I := 0 to 254 do
begin //Make trapeziums of color
y0 := bm.height - 1 - (i * Pw);
if (y0 > 0) then
x0 := bm.width - 1
else
begin
x0 := bm.width - 1 + y0;
y0 := 0;
end;
y1 := bm.height - 1 - ((i + 1) * pw);
if (y1 > 0) then
x1 := bm.width - 1
else
begin
x1 := bm.width - 1 + y1;
y1 := 0;
end;
x2 := bm.width - 1 - (i * pw);
if (x2 > 0) then
y2 := bm.height - 1
else
begin
y2 := bm.height - 1 + x2;
x2 := 0;
end;
x3 := bm.width - 1 - ((i + 1) * pw);
if (x3 > 0) then
y3 := bm.height - 1
else
begin
y3 := bm.height - 1 + x3;
x3 := 0;
end;
R := fromr + MulDiv(I, diffr, 255); //Find the RGB values
G := fromg + MulDiv(I, diffg, 255);
B := fromb + MulDiv(I, diffb, 255);
bm.canvas.Brush.Color := RGB(R, G, B); //Plug colors into brush
points[0] := point(Trunc(x0), Trunc(y0));
points[1] := point(Trunc(x1), Trunc(y1));
points[3] := point(Trunc(x2), Trunc(y2));
points[2] := point(Trunc(x3), Trunc(y3));
bm.canvas.polygon(points);
end;
end;

rgsSWNE:
begin
bm.canvas.Pen.Style := psclear;
bm.canvas.Pen.Mode := pmCopy;
Pw := (bm.width + bm.height) / 255;
for I := 0 to 254 do
begin //Make trapeziums of color
y0 := bm.height - 1 - (i * Pw);
if (y0 > 0) then
x0 := 0
else
begin
x0 := -y0;
y0 := 0;
end;
y1 := bm.height - 1 - ((i + 1) * pw);
if (y1 > 0) then
x1 := 0
else
begin
x1 := -y1;
y1 := 0;
end;
x2 := (i * pw);
if (x2 < bm.width) then
y2 := bm.height - 1
else
begin
y2 := bm.height - 1 - (x2 - bm.width);
x2 := bm.width - 1;
end;
x3 := (i + 1) * pw;
if (x3 < bm.width) then
y3 := bm.height - 1
else
begin
y3 := bm.height - 1 - (x3 - bm.width);
x3 := bm.width - 1;
end;
R := fromr + MulDiv(I, diffr, 255); //Find the RGB values
G := fromg + MulDiv(I, diffg, 255);
B := fromb + MulDiv(I, diffb, 255);
bm.canvas.Brush.Color := RGB(R, G, B); //Plug colors into brush
points[0] := point(Trunc(x0), Trunc(y0));
points[1] := point(Trunc(x1), Trunc(y1));
points[3] := point(Trunc(x2), Trunc(y2));
points[2] := point(Trunc(x3), Trunc(y3));
bm.canvas.polygon(points);
end;
end;

rgssweet:
begin
bm.canvas.Pen.Style := psclear;
bm.canvas.Pen.Mode := pmCopy;
for i := 0 to 255 do
begin
x1 := muldiv(i, bm.Width, 255);
x2 := muldiv(i + 1, bm.Width, 255);
y1 := muldiv(i, bm.Height, 255);
y2 := muldiv(i + 1, bm.Height, 255);

R := fromr + MulDiv(I, diffr, 255); //Find the RGB values
G := fromg + MulDiv(I, diffg, 255);
B := fromb + MulDiv(I, diffb, 255);

bm.Canvas.Brush.Color := RGB(R, G, B);

points[0] := point(bm.Width div 2, bm.Height div 2);
points[1] := point(0, trunc(y1));
points[2] := point(0, trunc(y2));
points[3] := points[2];
bm.canvas.polygon(points);

points[0] := point(bm.Width div 2, bm.Height div 2);
points[1] := point(bm.Width, bm.Height - trunc(y1));
points[2] := point(bm.Width, bm.Height - trunc(y2));
points[3] := points[2];
bm.canvas.polygon(points);

points[0] := point(bm.Width div 2, bm.Height div 2);
points[1] := point(trunc(x1), 0);
points[2] := point(trunc(x2), 0);
points[3] := points[2];
bm.canvas.polygon(points);

points[0] := point(bm.Width div 2, bm.Height div 2);
points[1] := point(bm.Width - trunc(x1), bm.Height);
points[2] := point(bm.Width - trunc(x2), bm.Height);
points[3] := points[2];
bm.canvas.polygon(points);
end;
end;

rgsStrange:
begin
bm.canvas.Pen.Style := psclear;
bm.canvas.Pen.Mode := pmCopy;
for i := 0 to 255 do
begin
x1 := muldiv(i, bm.Width, 255);
y1 := muldiv(i, bm.Height, 255);

R := fromr + MulDiv(I, diffr, 255); //Find the RGB values
G := fromg + MulDiv(I, diffg, 255);
B := fromb + MulDiv(I, diffb, 255);

bm.Canvas.Brush.Color := RGB(R, G, B);

points[0] := point(trunc(x1), trunc(y1));
points[1] := point(0, bm.Height - trunc(y1));
points[2] := point(bm.Width, bm.Height);
points[3] := point(bm.width, 0);
bm.canvas.polygon(points);
end;
end;

rgsNeo:
begin
bm.canvas.Pen.Style := psclear;
bm.canvas.Pen.Mode := pmCopy;
for i := 0 to 255 do
begin
x1 := muldiv(i, bm.Width div 2, 255);
y1 := muldiv(i, bm.Height div 2, 255);

R := fromr + MulDiv(I, diffr, 255); //Find the RGB values
G := fromg + MulDiv(I, diffg, 255);
B := fromb + MulDiv(I, diffb, 255);

bm.Canvas.Brush.Color := RGB(R, G, B);

points[0] := point(trunc(x1), trunc(y1));
points[1] := point(0, bm.Height);
points[2] := point(bm.Width - trunc(x1), bm.Height - trunc(y1));
points[3] := point(bm.width, 0);
bm.canvas.polygon(points);
end;

end;
end;
BitBlt(Canvas.Handle, 0, 0, bm.Width, bm.Height, bm.Canvas.Handle, 0, 0,
SRCCOPY);
//Canvas.CopyRect(arect,bm.Canvas,arect);
bm.Free;
end;

end.
 
后退
顶部