如何实现HSB -> RGB?(如画出一条彩虹)(100分)

  • 主题发起人 主题发起人 fly.zhou.
  • 开始时间 开始时间
F

fly.zhou.

Unregistered / Unconfirmed
GUEST, unregistred user!
如何实现HSB -> RGB?
(如画出一条彩虹)
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=0368427
 
那个问题我早已看过, 我要的是HSB->RGB, 不是HSL->RGB
 
新闻组里找到的,不知道行不行。

//==========================================================================
====
procedure TForm1.RGBtoHSB(const r, g, b: Integer; var h, s, br : Double);
var
largestColor : Integer; // holds the largest color (rgb) at the start
lowestColor : Integer; // opposite of above
hue : Double; // it puts the "h" in "hsb"
saturation : Double;// "s"
brightness : Double; // and the "b"
redRatio : Double;
greenRatio : Double;
blueRatio : Double;
begin
// assign largestColor to the greater of the red or green
if (r <= g) then
largestColor := g
else
largestColor := r;

// now see if blue is bigger
if (b > largestColor) then
largestColor := b;

// set lowestColor = to the smallest value
if (g < r) then
lowestColor := g
else
lowestColor := r;

if (b < lowestColor) then
lowestColor := b;

// brightness is calculated like so.
brightness := largestColor / 255.0;

// if the largestColor isn't zero (so we don't divide by zero) set the
variable
// to the difference of the two as a percentage of the largest
if (largestColor <> 0) then
saturation := (largestColor - lowestColor) / largestColor
else
saturation := 0.0;

if (saturation = 0.0) then
hue := 0.0
else
begin
//some temporary variables to figure out the hue
redRatio := (largestColor - r) / (largestColor - lowestColor);
greenRatio := (largestColor - g) / (largestColor - lowestColor);
blueRatio := (largestColor - b) / (largestColor - lowestColor);

// depending on which of the 3 was the highest, we calculate our hue.
if (r = largestColor) then
hue := blueRatio - greenRatio
else if (g = largestColor) then
hue := (2.0 + redRatio) - blueRatio
else // blue is largest
hue := (4.0 + greenRatio) - redRatio;

// divide it by 6
hue := hue /6.0;

// I don't know if this prevents us from being negative (as in the
worst outcome from the above
// operations is -1, or if this does something different).
if (hue < 0.0) then
hue := hue + 1;
end;

//Pass Back Values
h := hue;
s := saturation;
br := brightness;
end;
//==========================================================================
====
procedure TForm1.HSBtoRGB(const hue, saturation, brightness: Double; var r,
g, b: Integer);
const
max = 255;
var
h : Double;
f : Double;
p : Double;
q : Double;
t : Double;
begin

if(saturation = 0) then
begin
r := trunc(brightness * 255);
g := trunc(brightness * 255);
b := trunc(brightness * 255);
end
else
begin
h := (hue - floor(hue)) * 6.0;
f := h - floor(h);
p := brightness * (1.0 - saturation);
q := brightness * (1.0 - saturation * f);
t := brightness * (1.0 - (saturation * (1.0 - f)));

case trunc(h) of
0: begin
r := trunc(brightness * 255);
g := trunc(t * max);
b := trunc(p * max);
end;
1: begin
r := trunc(q * max);
g := trunc(brightness * max);
b := trunc(p * max);
end;
2: begin
r := trunc(p * max);
g := trunc(brightness * max);
b := trunc(t * max);
end;
3: begin
r := trunc(p * max);
g := trunc(q * max);
b := trunc(brightness * max);
end;
4: begin
r := trunc(t * max);
g := trunc(p * max);
b := trunc(brightness * max);
end;
5: begin
r := trunc(brightness * max);
g := trunc(p * max);
b := trunc(q * max);
end;
end;
end;
end;
//==========================================================================
====
 
thank you, zw84611
 
后退
顶部