Delphi 如何在转换时避免变量近似?

Delphi 如何在转换时避免变量近似?,delphi,Delphi,我有一个delphixe2项目。在我的项目中,我有MainForm,Label01,Label02,Label03,Label04,Label05,Label06,Edit01,Edit02,Edit03,Edit04,BitBtn01,,Timer01和Timer02 我正在努力实现以下目标: 单击按钮后Label01.Font.Color的亮度将持续增加或减少,就像“Microsoft在任务栏中完成Windows 7中的任何工作后完成此操作一样” 因此,我的逻辑是:在BitBtn01上。单击

我有一个
delphixe2
项目。在我的项目中,我有
MainForm
Label01
Label02
Label03
Label04
Label05
Label06
Edit01
Edit02
Edit03
Edit04
BitBtn01
Timer01
Timer02

我正在努力实现以下目标:

单击
按钮后
Label01.Font.Color的
亮度
将持续增加或减少,就像“Microsoft在任务栏中完成Windows 7中的任何工作后完成此操作一样”

因此,我的逻辑是:在
BitBtn01上。单击
Label01。Font.Color
将根据名为
RGBToHSV
的过程转换为
HSB颜色模型。如果
亮度
小于100%,则它将增加
Timer01
。在达到100%的
亮度后,它将减少
Timer02
至25%。每次
亮度
都将根据名为
HSVToRGV
的过程更新
Label01.Font.Color
,保持
色调
饱和度
恒定。这些程序根据
颜色转换算法
工作

根据我的要求,我编写了以下代码:

unit ApplicationWizard01;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.Buttons, Math;

type
  TMainForm = class(TForm)
    BitBtn01: TBitBtn;
    BitBtn02: TBitBtn;
    Edit01: TEdit;
    Edit02: TEdit;
    Edit03: TEdit;
    Edit04: TEdit;
    Label01: TLabel;
    Label02: TLabel;
    Label03: TLabel;
    Label04: TLabel;
    Label05: TLabel;
    Label06: TLabel;
    Timer01: TTimer;
    Timer02: TTimer;
    procedure BitBtn01Click(Sender: TObject);
    procedure Timer01Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BitBtn02Click(Sender: TObject);
    procedure Timer02Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

procedure HSVToRGB(Const H, S, V: Real;  {  'H' In '000' To '001', 'S' In '000' To '001', 'V' In '000' To '255'  }
                   Out R, G, B: Real);   {  'R' In '000' To '255', 'G' In '000' To '255', 'B' In '000' To '255'  }
const
  SectionSize = 60/360;
var
  F: real;
  P, Q, T: real;
  Section: real;
  SectionIndex: integer;
begin
  if H < 0 then
    begin
      R:= V;
      G:= R;
      B:= R;
    end
  else
    begin
      Section:= H/SectionSize;
      SectionIndex:= Floor(Section);
      F:= Section - SectionIndex;
      P:= V * ( 1 - S );
      Q:= V * ( 1 - S * F );
      T:= V * ( 1 - S * ( 1 - F ) );
      case SectionIndex of
        0:
          begin
            R:= V;
            G:= T;
            B:= P;
          end;
        1:
          begin
            R:= Q;
            G:= V;
            B:= P;
          end;
        2:
          begin
            R:= P;
            G:= V;
            B:= T;
          end;
        3:
          begin
            R:= P;
            G:= Q;
            B:= V;
          end;
        4:
          begin
            R:= T;
            G:= P;
            B:= V;
          end;
        else
          begin
            R:= V;
            G:= P;
            B:= Q;
          end;
      end;
    end;
end;

procedure RGBToHSV(Const R, G, B: Real;  {  'R' In '000' To '255', 'G' In '000' To '255', 'B' In '000' To '255'  }
                   Out H, S, V: Real);   {  'H' In '000' To '001', 'S' In '000' To '001', 'V' In '000' To '255'  }
var
  Range: real;
  RGB: array[0..2] of real;
  MinIndex, MaxIndex: integer;
begin
  RGB[0]:= R;
  RGB[1]:= G;
  RGB[2]:= B;

  MinIndex:= 0;
  if G < R then MinIndex:= 1;
  if B < RGB[MinIndex] then MinIndex:= 2;

  MaxIndex:= 0;
  if G > R then MaxIndex:= 1;
  if B > RGB[MaxIndex] then MaxIndex:= 2;

  Range:= RGB[MaxIndex] - RGB[MinIndex];

  if Range = 0 then
    begin
      H:= 0;
      S:= 0;
      V:= R;
    end
    else
      begin
        case MaxIndex of
          0:
            begin
              H:= (G-B)/Range;
            end;
          1:
            begin
              H:= 2 + (B-R)/Range;
            end;
          2:
            begin
              H:= 4 + (R-G)/Range;
            end;
        end;
        S:= Range/RGB[MaxIndex];
        V:= RGB[MaxIndex];
        H:= H * (1/6);
        if H < 0 then H:= 1 + H;
      end;
end;

procedure TMainForm.BitBtn01Click(Sender: TObject);
begin
  Timer01.Enabled := true;
end;

procedure TMainForm.BitBtn02Click(Sender: TObject);
begin
  Timer01.Enabled := false;
  Timer02.Enabled := false;
end;

procedure TMainForm.FormCreate(Sender: TObject);
const
  HueStandardisationFactor = 360;
  SaturationStandardisationFactor = 100;
  BrightnessStandardisationFactor = 100/255;
var
  H, S, V, R, G, B: Real;
begin
  R := 98;
  G := 128;
  B := 33;

  Label01.Font.Color := RGB(Round(R), Round(G), Round(B));

  Edit01.Text := FloatToStr(Round(R)) + '        ' + FloatToStr(Round(G)) + '        ' + FloatToStr(Round(B));

  RGBToHSV(R, G, B, H, S, V);

  Edit02.Text := FloatToStr(Round(H*HueStandardisationFactor)) + '        ' + FloatToStr(Round(S*SaturationStandardisationFactor)) + '        ' + FloatToStr(Round(V*BrightnessStandardisationFactor));

  Label02.Caption := 'Starting RGB Value :    ' + '(' + FloatToStr(R) + '  ' + FloatToStr(G) + '  ' + FloatToStr(V) + ')';
  Label03.Caption := 'Starting HSV Value :    ' + '(' + FloatToStr(Round(H*HueStandardisationFactor)) + '  ' + FloatToStr(Round(S*SaturationStandardisationFactor)) + '  ' + FloatToStr(Round(V*BrightnessStandardisationFactor)) + ')';
end;

procedure TMainForm.Timer01Timer(Sender: TObject);
const
  HueStandardisationFactor = 360;
  SaturationStandardisationFactor = 100;
  BrightnessStandardisationFactor = 100/255;
var
  Brightness : Integer;
  H1, S1, V1, R1, G1, B1: Real;
  H2, S2, V2, R2, G2, B2: Real;
begin
  R1 := GetRValue(Label01.Font.Color);
  G1 := GetGValue(Label01.Font.Color);
  B1 := GetBValue(Label01.Font.Color);

  RGBToHSV(R1, G1, B1, H1, S1, V1);

  Brightness := Round(V1);
  Brightness := Brightness + 1;
  if Brightness >= 255 then
    begin
      Timer01.Enabled := false;
      Timer02.Enabled := true;
    end;

  H2 := H1;
  S2 := S1;
  V2 := Brightness;

  Edit03.Text := FloatToStr(Round(H2*HueStandardisationFactor)) + '        ' + FloatToStr(Round(S2*SaturationStandardisationFactor)) + '        ' + FloatToStr(Round(V2*BrightnessStandardisationFactor));

  HSVToRGB(H2, S2, V2, R2, G2, B2);

  Label01.Font.Color := RGB(Round(R2), Round(G2), Round(B2));

  Edit04.Font.Color := RGB(95, 25, 255);
  Edit04.Text := FloatToStr(Round(R2)) + '        ' + FloatToStr(Round(G2)) + '        ' + FloatToStr(Round(B2));
end;

procedure TMainForm.Timer02Timer(Sender: TObject);
const
  HueStandardisationFactor = 360;
  SaturationStandardisationFactor = 100;
  BrightnessStandardisationFactor = 100/255;
var
  Brightness : Integer;
  H1, S1, V1, R1, G1, B1: Real;
  H2, S2, V2, R2, G2, B2: Real;
begin

  R1 := GetRValue(Label01.Font.Color);
  G1 := GetGValue(Label01.Font.Color);
  B1 := GetBValue(Label01.Font.Color);

  RGBToHSV(R1, G1, B1, H1, S1, V1);

  Brightness := Round(V1);
  Brightness := Brightness - 1;
  if Brightness <= 25 then
    begin
      Timer01.Enabled := true;
      Timer02.Enabled := false;
    end;

  H2 := H1;
  S2 := S1;
  V2 := Brightness;

  Edit03.Text := FloatToStr(Round(H2*HueStandardisationFactor)) + '        ' + FloatToStr(Round(S2*SaturationStandardisationFactor)) + '        ' + FloatToStr(Round(V2*BrightnessStandardisationFactor));

  HSVToRGB(H2, S2, V2, R2, G2, B2);

  Label01.Font.Color := RGB(Round(R2), Round(G2), Round(B2));

  Edit04.Font.Color := RGB(15, 135, 255);
  Edit04.Text := FloatToStr(Round(R2)) + '        ' + FloatToStr(Round(G2)) + '        ' + FloatToStr(Round(B2));
end;

end.
单元应用向导01;
接口
使用
Winapi.Windows、Winapi.Messages、System.SysUtils、System.Variants、System.Classes、Vcl.Graphics、,
控件、窗体、对话框、ExtCtrls、StdCtrls、按钮、数学;
类型
TMainForm=类(TForm)
BitBtn01:TBitBtn;
BitBtn02:TBitBtn;
Edit01:TEdit;
Edit02:TEdit;
Edit03:TEdit;
Edit04:TEdit;
Label01:TLabel;
Label02:TLabel;
Label03:TLabel;
Label04:TLabel;
Label05:TLabel;
Label06:TLabel;
Timer01:TTimer;
Timer02:TTimer;
程序BitBtn01Click(发送方:TObject);
程序定时器01定时器(发送方:TObject);
过程表单创建(发送方:ToObject);
程序BitBtn02Click(发送方:TObject);
程序定时器02定时器(发送方:TObject);
私有的
{私有声明}
公众的
{公开声明}
结束;
变量
主要形式:TMA通知;
实施
{$R*.dfm}
程序HSVToRGB(常数H,S,V:Real;{'000'中的'H'到'001','000'中的'S'到'001','000'中的'V'到'255'}
Out R,G,B:真实值);{'000'中的'R'到'255','000'中的'G'到'255','000'中的'B'到'255'}
常数
截面尺寸=60/360;
变量
F:真实的;
P、 Q,T:真实;
第三节:房地产;
分段索引:整数;
开始
如果H<0,则
开始
R:=V;
G:=R;
B:=R;
结束
其他的
开始
截面:=H/截面尺寸;
截面索引:=楼层(截面);
F:=截面-截面索引;
P:=V*(1-S);
Q:=V*(1-S*F);
T:=V*(1-S*(1-F));
案例索引
0:
开始
R:=V;
G:=T;
B:=P;
结束;
1:
开始
R:=Q;
G:=V;
B:=P;
结束;
2:
开始
R:=P;
G:=V;
B:=T;
结束;
三:
开始
R:=P;
G:=Q;
B:=V;
结束;
4:
开始
R:=T;
G:=P;
B:=V;
结束;
其他的
开始
R:=V;
G:=P;
B:=Q;
结束;
结束;
结束;
结束;
过程RGBToHSV(常量R,G,B:Real;{'000'中的'R'到'255','000'中的'G'到'255','000'中的'B'到'255'}
输出H、S、V:真实值);{'000'中的'H'到'001','000'中的'S'到'001','000'中的'V'到'255'}
变量
范围:真实;
RGB:实数的数组[0..2];
MinIndex,MaxIndex:整数;
开始
RGB[0]:=R;
RGB[1]:=G;
RGB[2]:=B;
最小指数:=0;
如果GR,则MaxIndex:=1;
如果B>RGB[MaxIndex],则MaxIndex:=2;
范围:=RGB[MaxIndex]-RGB[MinIndex];
如果范围=0,则
开始
H:=0;
S:=0;
V:=R;
结束
其他的
开始
案例MaxIndex
0:
开始
H:=(G-B)/范围;
结束;
1:
开始
H:=2+(B-R)/量程;
结束;
2:
开始
H:=4+(R-G)/量程;
结束;
结束;
S:=范围/RGB[MaxIndex];
V:=RGB[MaxIndex];
H:=H*(1/6);
如果H<0,则H:=1+H;
结束;
结束;
程序TMainForm.BitBtn01Click(发送方:TObject);
开始
Timer01.Enabled:=真;
结束;
程序TMainForm.BitBtn02Click(发送方:ToObject);
开始
Timer01.Enabled:=false;
Timer02.Enabled:=false;
结束;
程序TMAInformCreate(发送方:ToObject);
常数
Huestandriationfactor=360;
饱和标准化系数=100;
亮度标准化系数=100/255;
变量
H、 S,V,R,G,B:真实;
开始
R:=98;
G:=128;
B:=33;
Label01.Font.Color:=RGB(圆形(R)、圆形(G)、圆形(B));
Edit01.Text:=FloatToStr(第(R)轮)+''+FloatToStr(第(G)轮)+''+FloatToStr(第(B)轮);
RGBToHSV(R、G、B、H、S、V);
Edit02.Text:=FloatToStr(圆形(H*HuestandardiationFactor))+“”+FloatToStr(圆形(S*饱和标准化因子))+“”+FloatToStr(圆形(V*亮度标准化因子));
Label02.标题:='起始RGB值:'+'('+FloatToStr(R)+'+FloatToStr(G)+'+FloatToStr(V)+');
Label03.标题:='起始HSV值:'+'('+FloatToStr(圆形(H*HuestandardiationFactor))+''+FloatToStr(圆形(S*SaturationStandardisationFactor))+''+FloatToStr(圆形(V*Bright