Percentual para Cor

Top  Previous  Next

// 0 - 100 = TColor verde (0) para vermelho (100)
function GetColorFromPercentual(const Perc: Double): TColor;
var
  R, G, B: Byte;
  Calc   : Double;
begin
  B := 0;
  R := 255;
  G := 165;
 
  if Perc < 0 then
  begin
    Exit(RGB(0, 165, 0));
  end;
 
  if Perc > 100 then
  begin
    Exit(RGB(255, 0, 0));
  end;
 
  if Perc < 50 then
  begin
    Calc := (Perc * 255) / 50;
    R    := Trunc(Calc);
  end
  else
  begin
    R    := 255;
    Calc := ((100 - Perc) * 165) / 50;
    G    := Trunc(Calc);
  end;
 
  Result := RGB(R, G, B);
end;