This code performs linear interpolation between two colors, could be probably optimized to smaller function (any volunteers? ):

Code:
type
  TColor = array[0..3] of byte;

function ClampB(n: integer): byte;
begin
  if n > 255 then
    Result := 255
  else
  if n < 0 then
    Result &#58;= 0
  else
    Result &#58;= n;
end;

function fraction&#40;maxnew, maxold, val&#58; single&#41;&#58; single;
begin
  if maxnew = 0 then
    Result &#58;= 0
  else
    Result &#58;= &#40;val * maxold&#41; / maxnew;
end;

function ColorLERP&#40;src, dest&#58; TColor; min, max, val&#58; single&#41;&#58; TColor; // Linear interpolation between two colors
var
  interp&#58; integer;
begin

  interp &#58;= trunc&#40;fraction&#40;max, 255, val&#41;&#41;;

  Result&#91;0&#93; &#58;= ClampB&#40;ClampB&#40;src&#91;0&#93; - interp&#41; + ClampB&#40;dest&#91;0&#93; - 255 + interp&#41;&#41;;
  Result&#91;1&#93; &#58;= ClampB&#40;ClampB&#40;src&#91;1&#93; - interp&#41; + ClampB&#40;dest&#91;1&#93; - 255 + interp&#41;&#41;;
  Result&#91;2&#93; &#58;= ClampB&#40;ClampB&#40;src&#91;2&#93; - interp&#41; + ClampB&#40;dest&#91;2&#93; - 255 + interp&#41;&#41;;
  Result&#91;3&#93; &#58;= ClampB&#40;ClampB&#40;src&#91;3&#93; - interp&#41; + ClampB&#40;dest&#91;3&#93; - 255 + interp&#41;&#41;;
end;