Delphi实现RGB色环的代码绘制

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Delphi实现RGB色环的代码绘制相关的知识,希望对你有一定的参考价值。

function TColorFrm.CreateColorCircle(const size: integer): TBitmap;
var
i,j,x,y: Integer;
radius: integer;
perimeter,arc,degree,step: double;
R,G,B: byte;
color: TColor;
begin
radius := round(size / 2);
RESULT := TBitmap.Create;
R:=255;
G:=0;
B:=0;
with RESULT do
begin
width := size;
height:= size;
pixelFormat := pf24bit;
Canvas.Brush.Color := RGB(R,G,B);
x := size + 1;
y := round(radius) + 1;
Canvas.FillRect(Rect(size,round(radius),x,y));
for j := 0 to size do
begin
perimeter := (size - j) * PI + 1;
arc := perimeter / 6;
step := ( 255 * 6 ) / perimeter ; //颜色渐变步长
for i := 0 to round(perimeter) - 1 do
begin
degree := 360 / perimeter * i;
x := round(cos(degree * PI / 180) * (size - j + 1) / 2) + radius;//数学公式,最后加上的是圆心点
y := round(sin(degree * PI / 180) * (size - j + 1) / 2) + radius;

if (degree > 0) and (degree <= 60) then
begin
R := 255;
G := 0;
B := round(step * i);
end;
if (degree > 60) and (degree <= 120) then
begin
if perimeter / 3 / 120 * (degree - 60) > 1.0 then
R := 255 - round(step * (i - arc))
else
R := 255 - round(step * ABS(i - arc));
G := 0;
B := 255;
end;
if (degree > 120) and (degree <= 180) then
begin
R := 0;
if perimeter / 3 / 120 * (degree - 120) > 1.0 then
G := round(step * (i - 2 * arc))
else
G := round(step * ABS(i - 2 * arc));
B := 255;
end;
if (degree > 180) and (degree <= 240) then
begin
R := 0;
G := 255;
if perimeter / 3 / 120 * (degree - 120) > 1.0 then
B := 255 - round(step * (i - perimeter / 2))
else
B := 255 - round(step * ABS(i - perimeter / 2));
end;
if (degree > 240) and (degree <= 300) then
begin
if perimeter / 3 / 120 * (degree - 240) > 1.0 then
R := round(step * (i - 4 * arc))
else
R := round(step * ABS(i - 4 * arc)) ;
G := 255;
B := 0;
end;
if (degree > 300) and (degree <= 360) then
begin
R := 255;
if perimeter / 3 / 120 * (degree - 300) > 1.0 then
G := 255 - round(step * (i - 5 * arc))
else
G := 255 - round(step * ABS(i - 5 * arc));
B := 0;
end;
color := RGB( ROUND(R + (255 - R)/size * j),ROUND(G + (255 - G) / size * j),ROUND(B + (255 - B) / size * j));
Canvas.Brush.Color := color;
//为了绘制出来的圆好看,分成四个部分进行绘制
if (degree >= 0) and (degree <= 45) then
Canvas.FillRect(Rect(x,y,x-2,y-1));
if (degree > 45) and (degree <= 135) then
Canvas.FillRect(Rect(x,y,x-1,y-2));
if (degree > 135) and (degree <= 225) then
Canvas.FillRect(Rect(x,y,x+2,y+1));
if (degree > 215) and (degree <= 315) then
Canvas.FillRect(Rect(x,y,x+1,y+2));
if (degree > 315) and (degree <= 360) then
Canvas.FillRect(Rect(x,y,x-2,y-1));
end;
end;
end;
end;

以上是关于Delphi实现RGB色环的代码绘制的主要内容,如果未能解决你的问题,请参考以下文章

delphi RGB与TColor的转换

Delphi颜色的表示(一共5种表示法)

delphi RGB 转 HSV

delphi 怎样绘制半透明窗体,只让窗体背景半透明,而窗体里面的控件 不透明

色环电阻的阻值识别

Delphi如何访问父窗体