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 ColorsBetween. I may want to just get an array of TColor between any 2 colors (ColorsBetween), and I may also just need to know one color value at a percentage between two colors (ColorBetween).
I already have it mostly done below. Except, I have two core questions:
- How do I calculate the in-between color of each RGB channel by a given percentage? (See below where I have
[???]
) - What's the fastest method to accomplish what I'm doing (while keeping the two distinct functions)?
Here's the Code:
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
StrUtils, StdCtrls, Math;
type
TColorArray = array of TColor;
implementation
function ColorsBetween(const ColorA, ColorB: TColor; const Count: Integer): TColorArray;
var
X: Integer; //Loop counter
begin
SetLength(Result, Count);
for X:= 0 to Count - 1 do
Result[X]:= ColorBetween(ColorA, ColorB, Round((X / Count) * 100)); //Correct?
end;
function ColorBetween(const ColorA, ColorB: TColor; const Percent: Single): 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(
EnsureRange(([???]), 0, 255),
EnsureRange(([???]), 0, 255),
EnsureRange(([???]), 0, 255)
);
end;
EDIT: Changed Percent: Integer
to Percent: Single
to get a smoother effect - not restricted to 100 possible values.