Advertisement
JimMcKeeth

HighContrastText

Apr 18th, 2025 (edited)
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 0.61 KB | Source Code | 0 0
  1. function HighContrastText(ABackground: TColor): TColor;
  2. var
  3.   R, G, B, Luminance: Single;
  4. begin
  5.   // Convert system color to RGB if needed
  6.   ABackground := ColorToRGB(ABackground);
  7.  
  8.   // Calculate relative luminance using standard coefficients
  9.   // Using ITU-R BT.709 coefficients
  10.   R := (ABackground and $FF) / 255;
  11.   G := ((ABackground shr 8) and $FF) / 255;
  12.   B := ((ABackground shr 16) and $FF) / 255;
  13.   Luminance := 0.2126 * R + 0.7152 * G + 0.0722 * B;
  14.  
  15.   // If background is dark, return white; if light, return black
  16.   if Luminance < 0.5 then
  17.     Result := clWhite
  18.   else
  19.     Result := clBlack;
  20. end;
Tags: delphi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement