Matthew Hipkin

Determine contrasting TColor

This is a simple function for Delphi/FreePascal that returns black or white depending on the specified TColor.

function ColourContrast(incol: TColor): TColor;
const
  gamma = 2.2;
var
  r,g,b,L: Double;
begin
  r := Red(incol) / 255;
  g := Green(incol) / 255;
  b := Blue(incol) / 255;
  L := 0.2126 * power(R, gamma) + 0.7152 * power(G, gamma) + 0.0722 * power(B, gamma);
  if L > power(0.5, gamma) then Result := clBlack
  else Result := clWhite;
end;

This is a conversion of C code found here.

blog comments powered by Disqus