delphi RGBToHSB


本文整理自网络,侵删。

 RGBToHSB
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
uses
Math;

type
TRGBColor = record
Red,
Green,
Blue: Byte;
end;

THSBColor = record
Hue,
Saturnation,
Brightness: Double;
end;

function RGBToHSB(rgb: TRGBColor): THSBColor;
var
minRGB, maxRGB, delta: Double;
h, s, b: Double;
begin
H := 0.0;
minRGB := Min(Min(rgb.Red, rgb.Green), rgb.Blue);
maxRGB := Max(Max(rgb.Red, rgb.Green), rgb.Blue);
delta:= ( maxRGB - minRGB );
b:= maxRGB;
if (maxRGB <> 0.0) then s := 255.0 * Delta / maxRGB
else s := 0.0;
if (s <> 0.0) then
begin
if rgb.Red = maxRGB then h := (rgb.Green - rgb.Blue) / Delta
else
if rgb.Green = maxRGB then h := 2.0 + (rgb.Blue - rgb.Red) / Delta
else
if rgb.Blue = maxRGB then h := 4.0 + (rgb.Red - rgb.Green) / Delta
end
else h := -1.0;
h := h * 60;
if h < 0.0 then h := h + 360.0;
with result do
begin
Hue := h;
Saturnation := s * 100 / 255;
Brightness := b * 100 / 255;
end;
end;

//测试:
procedure TForm1.Button1Click(Sender: TObject);
var
rgb: TRGBColor;
hsb: THSBColor;
begin
rgb.Red := 255;
rgb.Green := 0;
rgb.Blue := 0;

hsb := RGBToHSB(rgb);

ShowMessage(FloatToStr(hsb.Hue)); //0
ShowMessage(FloatToStr(hsb.Saturnation)); //100
ShowMessage(FloatToStr(hsb.Brightness)); //100
end;

end.

相关阅读 >>

Delphi将图片缩放成指定大小

Delphi 通过进程名获得文件全路径的函数

Delphi 2010 复制整个文件夹(当然包括嵌套文件夹)

Delphi xe的firemonkey获取当前文件所在路径的方法

Delphi 获取安卓机器码

Delphi dbgrid刷新数据

Delphi 程序退出时删除自身

Delphi访问mysql乱码问题

Delphi中tapplicationevents控件的用途与使用方法

Delphi xe5中移动平台的字符串要注意的事项

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...