Fastest way to calculate colors for a gradient?

前端 未结 2 1534
迷失自我
迷失自我 2021-01-02 14:23

I\'m making a small collection of types/functions related to gradients for future use. I would like to make sure there\'s at least two procedures: ColorBetween and ColorsBet

2条回答
  •  感情败类
    2021-01-02 15:15

    I don't know if this is the fastest way, but it works:

    function ColorBetween(const ColorA, ColorB: TColor; const Percent: Integer): TColor;
    var
      R1, G1, B1: Byte;
      R2, G2, B2: Byte;
    begin
      R1:= GetRValue(ColorA);
      G1:= GetGValue(ColorA);
      B1:= GetBValue(ColorA);
      R2:= GetRValue(ColorB);
      G2:= GetGValue(ColorB);
      B2:= GetBValue(ColorB);
    
      Result:= RGB(
        Percent * (R2-R1) div 100 + R1,
        Percent * (G2-G1) div 100 + G1,
        Percent * (B2-B1) div 100 + B1
      );
    end;
    
    function ColorsBetween(const ColorA, ColorB: TColor; const Count: Integer): TColorArray;
    var
      X : integer;
    begin
      SetLength(Result, Count);
      for X := 0 to Count - 1 do
        Result[X] := ColorBetween(ColorA, ColorB, Round((X / (Count-1)) * 100));  //Note: Divide by count-1
    end;
    

提交回复
热议问题