我需要生成一个渐变位图,以显示用户选择的两种颜色之间的彩虹渐变。产生彩虹很容易。下面的代码是我从Wiki获得的,并对其进行了一些改动。它具有快速简便的优点。

 function TColor_Dialog.GiveRainbowColor (fraction: double): TAlphaColor;
  var
    m: Double;
    r, g, b, mt: Byte;
  begin
    if fraction <= 0 then m := 0 else
    if fraction >= 1 then m := 6
                     else m := fraction * 6;
    mt := (round (frac (m) * $FF));
    case Trunc (m) of
    0: begin
        R := $FF;
        G := mt;
        B := 0;
      end;
    1: begin
        R := $FF - mt;
        G := $FF;
        B := 0;
      end;
    2: begin
        R := 0;
        G := $FF;
        B := mt;
      end;
    3: begin
        R := 0;
        G := $FF - mt;
        B := $FF;
      end;
    4: begin
        R := mt;
        G := 0;
        B := $FF;
      end;
    5: begin
        R := $FF;
        G := 0;
        B := $FF - mt;
      end;
    end; // case

    Result := ColorToQuad (r, g, b);
  end; // GiveRainbowColor //

该算法的麻烦之处在于它无法在两种颜色之间显示部分彩虹。好吧,当然可以,但是比起每种颜色的分数,我不喜欢这种解决方案。我尝试将颜色分解成其r,g,b channel ,但这没有用。事后看来,原因很明显。假设您需要一个介于FF0000和0000FF之间的渐变。从FF-> 00转换为红色,从00-> FF转换为蓝色。但是,没有绿色(00FF00)明显存在于彩虹渐变中。

我需要的是一个渐变函数,我可以给出两种颜色和一个分数,然后生成一种颜色。谁能指出我的文章,算法甚至代码?

更新

NGLN的答案是此问题的正确答案。他和沃伦都想知道当颜色不是鲜艳颜色(包含0,$ FF和值的颜色)时该怎么办。我尝试了几种角度:上/下缩放和HSL插值。我终于选择了最后一个,因为它是最简单的。

基本上,您有两种颜色:fromto。使用RGBtoHSL从每种颜色中提取HSL参数:RGBtoHSL (col_from, hf, sf, lf)。接下来,计算两种颜色之间的色相,饱和度和亮度,并重建新的颜色。这就是NGLN在其有关色相的更新中提到的内容,但是如果您概括这一原理,则任何颜色之间都有彩虹。
function TColor_Dialog.interpolate_hsl (col_from, col_to: TAlphaColor; fraction: double): TAlphaColor;
  var af, at, ad: uInt8;
      hf, ht, hd: single;
      sf, st, sd: single;
      lf, lt, ld: single;
  begin
  // Get each rgb color channel
     af := GetAValue (col_from);
     at := GetAValue (col_to);
     RGBtoHSL (col_from, hf, sf, lf);
     RGBtoHSL (col_to,   ht, st, lt);

  // Compute differences
     ad := af + Round (fraction * (at - af));
     hd := hf + fraction * (ht - hf);
     sd := sf + fraction * (st - sf);
     ld := lf + fraction * (lt - lf);

     Result := MakeColor (HSLtoRGB (hd, sd, ld), ad);
  end; // interpolate_hsl //

这为所有可能的颜色提供了彩虹。我对不透明度应用了相同的插值,因此使用MakeColor将插值的Alpha channel “弄乱”到颜色中。

最佳答案

然后,您需要计算颜色在Rainbow中的位置; GiveRainbowColor的反函数:

function RainbowIndex(BrightColor: TColor): Double;
var
  R: Byte;
  G: Byte;
  B: Byte;
begin
  R := GetRValue(ColorToRGB(BrightColor));
  G := GetGValue(ColorToRGB(BrightColor));
  B := GetBValue(ColorToRGB(BrightColor));
  if (R * G * B <> 0) or ((R <> 255) and (G <> 255) and (B <> 255)) then
    Result := -1
  else if B = 0 then
    if R = 255 then
      Result := 0 + G / 255
    else
      Result := 1 + (255 - R) / 255
  else if R = 0 then
    if G = 255 then
      Result := 2 + B / 255
    else
      Result := 3 + (255 - G) / 255
  else { G = 0 }
    if B = 255 then
      Result := 4 + R / 255
    else
      Result := 5 + (255 - B) / 255;
  Result := Result / 6;
end;

(但是,这对于同时没有0和255部分的颜色会出现问题。换句话说:您还需要从阴影,有色或灰色的颜色中计算出明亮的颜色。请参见下面的更新。)

将彩虹切片从clRed转换为clBlue的示例用法:
procedure TForm1.FormPaint(Sender: TObject);
var
  Start: Double;
  Finish: Double;
  X: Integer;
begin
  Start := RainbowIndex(clRed);
  Finish := RainbowIndex(clBlue);
  for X := 0 to ClientWidth - 1 do
  begin
    Canvas.Brush.Color := GiveRainbowColor(0, ClientWidth - 1, X);
    Canvas.FillRect(Bounds(X, 0, 1, 50));
    Canvas.Brush.Color :=
      GiveRainbowColor(0, ClientWidth - 1, Round(Start + (Finish - Start) * X));
    Canvas.FillRect(Bounds(X, 50, 1, 50));
  end;
end;

更新:

上面的RainbowIndex例程实际上不执行任何操作,然后计算颜色的hue属性。 GraphUtil单元为HSL color model提供了转换例程,这使该RainbowIndex例程变得过时了,并具有能够提供任何TColor值的优点:
uses
  GraphUtil;

const
  HLSMAX = 240;

function Hue(AColor: TColor): Double;
var
  Hue: Word;
  Luminance: Word;
  Saturation: Word;
begin
  ColorRGBToHLS(ColorToRGB(AColor), Hue, Luminance, Saturation);
  Result := Hue / HLSMAX;
end;

将彩虹切片从clMoneyGreen转换为clPurple的示例用法:
function RainbowColor(Hue: Double): TColor; overload;
begin
  Hue := EnsureRange(Hue, 0, 1) * 6;
  case Trunc(Hue) of
    0: Result := RGB(255, Round(Frac(Hue) * 255), 0);
    1: Result := RGB(255 - Round(Frac(Hue) * 255), 255, 0);
    2: Result := RGB(0, 255, Round(Frac(Hue) * 255));
    3: Result := RGB(0, 255 - Round(Frac(Hue) * 255), 255);
    4: Result := RGB(Round(Frac(Hue) * 255), 0, 255);
  else
    Result := RGB(255, 0, 255 - Round(Frac(Hue) * 255));
  end;
end;

function RainbowColor(MinHue, MaxHue, Hue: Integer): TColor; overload;
begin
  Result := RainbowColor((Hue - MinHue) / (MaxHue - MinHue + 1));
end;

procedure TForm1.FormPaint(Sender: TObject);
var
  X: Integer;
  Start: Double;
  Finish: Double;
begin
  Start := Hue(clMoneyGreen);
  Finish := Hue(clPurple);
  for X := 0 to ClientWidth - 1 do
  begin
    Canvas.Brush.Color := RainbowColor(0, ClientWidth - 1, X);
    Canvas.FillRect(Bounds(X, 0, 1, 50));
    Canvas.Brush.Color :=
      RainbowColor(Start + (Finish - Start) * X / ClientWidth);
    Canvas.FillRect(Bounds(X, 50, 1, 50));
  end;
end;

此外,RainbowColor例程可以简化为:
function RainbowColor(Hue: Double): TColor; overload;
begin
  Result := ColorHLStoRGB(Round(Hue * HLSMAX), HLSMAX div 2, HLSMAX);
end;

关于delphi - 如何创建彩虹渐变的一部分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19715600/

10-08 22:41