如何将HSB转换为RGB [英] How to convert HSB to RGB
问题描述
我有一个Delphi XE2项目使用 Timer04
更改 Label01字体颜色
。所以我写了以下代码:
I having one Delphi XE2 Project to change Label01 Font Color
using Timer04
. So I have written the following codes:
procedure TMainForm.FormCreate(Sender: TObject);
begin
Timer04.Enabled := true;
end;
..
..
..
..
..
procedure TMainForm.Timer04Timer(Sender: TObject);
var
StartColor, RedColor, GreenColor, BlueColor: integer;
begin
StartColor := ColorToRGB(Label01.Font.Color);
RedColor := GetRValue(StartColor);
GreenColor := GetGValue(StartColor);
BlueColor := GetBValue(StartColor);
if RedColor <= 251 then Inc(RedColor, 1) else RedColor := 1;
if GreenColor <= 252 then Inc(GreenColor, 2) else GreenColor := 2;
if BlueColor <= 253 then Inc(BlueColor, 3) else BlueColor := 3;
Label01.Font.Color := RGB(RedColor, GreenColor, BlueColor);
end;
这段代码工作完美。 Label01字体颜色
不同颜色之间的变化。
This codes work perfectly. Label01 Font Color
changes between different colors.
现在我试图实现 Label02颜色
将被修改(如绿色),亮点的值将从 0 增加到 100 。如果值达到 100 ,它将会降低到 0 ,并且循环将持续。
Now I am trying to implement that Label02 Color
will be fixed (say Green) and the value of brightnees will be increase from 0 to 100. If the value reaches to 100 it will be decreased to 0 and it will be continuous to a loop.
对于我的情况,我选择了 HUE = 135
, SATURATION = 85
和 BRIGHTNESS = 50
。 BRIGHTNESS
的值将从 50 增加到 100 ,然后将从 100 强>至 0 ,并将继续。
但是问题是没有这样的函数
HSB 转换为 RGB
,反之亦然, Delphi XE2
。我已经走了但是我发现任何函数
为 HSBToRGB
。只有一些 Delphi Unit
可用。我已经阅读了他们的重新发现,发现每个人都有一些错误。
For my case I have chosen HUE=135
, SATURATION=85
and BRIGHTNESS=50
. The value of BRIGHTNESS
will be increased from 50 to 100 and then will be decreased from 100 to 0 and it will be continued.
But the problem is that there is no such Function
available to convert HSB
to RGB
and vice versa in Delphi XE2
. I have Gooled it. But I have found any Function
as HSBToRGB
. Only some Delphi Unit
is availabe. I have read their revoews and found that every one is having some bugs.
推荐答案
这是一个Delphi翻译的C代码这里: http://www.cs.rit.edu/~ncs/color /t_convert.html
Here is a Delphi a translation of C code found here: http://www.cs.rit.edu/~ncs/color/t_convert.html
function RGBFP(R, G, B: Double): TColor;
const
RGBmax = 255;
begin
Result := RGB(Round(RGBmax * R), Round(RGBmax * G), Round(RGBmax * B));
end;
function HSVtoRGB(H, S, V: Double): TColor;
var
i: Integer;
f, p, q, t: Double;
begin
Assert(InRange(H, 0.0, 1.0));
Assert(InRange(S, 0.0, 1.0));
Assert(InRange(V, 0.0, 1.0));
if S = 0.0 then
begin
// achromatic (grey)
Result := RGBFP(V, V, V);
exit;
end;
H := H * 6.0; // sector 0 to 5
i := floor(H);
f := H - i; // fractional part of H
p := V * (1.0 - S);
q := V * (1.0 - S * f);
t := V * (1.0 - S * (1.0 - f));
case i of
0:
Result := RGBFP(V, t, p);
1:
Result := RGBFP(q, V, p);
2:
Result := RGBFP(p, V, t);
3:
Result := RGBFP(p, q, V);
4:
Result := RGBFP(t, p, V);
else
Result := RGBFP(V, p, q);
end;
end;
我给了这个最小的测试。请确保双重检查。
I've given this minimal testing. Please do feel free to double check it.
这篇关于如何将HSB转换为RGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!