text color visible lazarus / 21-General ---------------------------------------------------------------------- Paolo · 2026-05-13 10:25 Hello (win64/laz46/fpc 324RC1) I have a component of mine for graphical usage (inherited form TGraphicControl). I have in some cases some colored circle with inside a number (the circle ID). The number is written using Canvas.TextOut. Now the question: how to change the text color automatically to have max visibility of ID-text inside the colored circle ? if the circle is white black text (default) is fine, but if the circle is dark the number becames not visible. There is a pen/brush mode to do that ? code snippets are welcome, thanks. ---------------------------------------------------------------------- dsiders · 2026-05-13 20:06 Quote from: Paolo on May 13, 2026, 12:25:06 pmHello (win64/laz46/fpc 324RC1) I have a component of mine for graphical usage (inherited form TGraphicControl). I have in some cases some colored circle with inside a number (the circle ID). The number is written using Canvas.TextOut. Now the question: how to change the text color automatically to have max visibility of ID-text inside the colored circle ? if the circle is white black text (default) is fine, but if the circle is dark the number becames not visible. There is a pen/brush mode to do that ? code snippets are welcome, thanks. graphics.pp has a routine called InvertColor. But it's aesthetics lack a little. I generally want text to be either black (for light colors) or white (for dark colors). The attached has ColorIsDark() and ComplementColor() routines which I use. The example ises TPanel... but applies to TCanvas too. Hope that helps... ---------------------------------------------------------------------- speter · 2026-05-13 23:02 If you want a more "hands-on" approach look at the attached project. It creates 2 (almost identical) complementory colours for a given colour. The first converts the colour to HLS colour space then adds 128 to the "H" value: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   [...]  r := byte(colours[a]);  g := byte(colours[a] shr 8);  b := byte(colours[a] shr 16);   RGBtoHLS(r,g,b, ch,cl,cs);  k := ch;  ch := byte(k+128);  brush.color := HLStoColor(cH, cL, cS);  [...]and the second simply subtracts each RGB from 255 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   brush.color := 255 - r + (255 - g) shl 8 + (255 - b) shl 16; I generally use the second (simpler) technique. cheers S. ---------------------------------------------------------------------- H₂SO₄ · 2026-05-14 05:14 Quote from: Paolo on May 13, 2026, 12:25:06 pmThere is a pen/brush mode to do that ? TPenMode  has  pmXor,  which will invert any underlying colors when setting the pen to white (since  (B xor $FF) = (not B) = (255 - B)  when  B  is a byte, e.g. an RGB channel). I'm not aware of any similiar functionality for  TFont,  though. You could also draw the text white-on-black into some reserved area and call  TCanvas.CopyRect  with  TCanvas.CopyMode  set to  cmSrcInvert,  which has the same XOR effect. Personally, when I have to draw text over an arbitrarily colored background such as a photo, I prefer using black/white text with a white/black border (respectively), but that's not easy to accomplish when using  TCanvas. ---------------------------------------------------------------------- creaothceann · 2026-05-14 07:49 A "shadow" effect might already improve readability, for that you simply draw the text with a slanted offset and a darker color first, then without an offset with the actual color. For an outline there's this: https://forum.lazarus.freepascal.org/index.php?topic=39770.0 ---------------------------------------------------------------------- Paolo · 2026-05-14 10:10 Thak you all. I'll check what proposed. @Khrys I have already tried pmXor,  but nothing. I'll come back with my observation. Thanks again. ---------------------------------------------------------------------- Paolo · 2026-05-20 13:12 Applied what suggested by dsiders. It is enough since I know the background color. For more general case I'll see. ---------------------------------------------------------------------- wp · 2026-05-20 15:53 Quote from: dsiders on May 13, 2026, 10:06:18 pmI generally want text to be either black (for light colors) or white (for dark colors). The attached has ColorIsDark() and ComplementColor() routines which I use. Yes, I do that too, but my "color is dark or bright" routine of using the red/gree/blue mean value is more stupid: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function ContrastColor(AColor: TColor): TColor;var  sum: Integer;begin  sum := GetRValue(AColor) + GetGValue(AColor) + GetBValue(AColor);  if sum > 3*128 then    Result := clBlack  else    Result := clWhite;end; In most cases the result is visually correct, but there are a few colors, e.g. clLime, where obviously the wrong color is picked - your solution utilizing the visually correct grayscale conversion is better. Don, would you allow me to add your functions to the GraphUtil unit? I would take the freedom, though, to rename the "ComplementColor" to "ContrastColor" because the word "complement" indicates the opposite color on the color wheel to me while the function just returns black or white. ---------------------------------------------------------------------- dsiders · 2026-05-20 16:30 Quote from: wp on May 20, 2026, 05:53:48 pmWould you allow me to add your functions to the GraphUtil unit? Of course.  Quote from: wp on May 20, 2026, 05:53:48 pmI would take the freedom, though, to rename the "ComplementColor" to "ContrastColor" because it indicates the opposite color on the color wheel to me while it just returns black or white. That is a better / more accurate name for it. ---------------------------------------------------------------------- wp · 2026-05-20 17:02 Quote from: dsiders on May 20, 2026, 06:30:50 pmQuote from: wp on May 20, 2026, 05:53:48 pmWould you allow me to add your functions to the GraphUtil unit? Of course.  Quote from: wp on May 20, 2026, 05:53:48 pmI would take the freedom, though, to rename the "ComplementColor" to "ContrastColor" because it indicates the opposite color on the color wheel to me while it just returns black or white. That is a better / more accurate name for it. Done (https://gitlab.com/freepascal.org/lazarus/lazarus/- /commit/61dbd5c6967644682858572d85366476da2ee7e6). ---------------------------------------------------------------------- hedgehog · 2026-05-21 08:36 I use this function Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function GetContrastColor(C: TColor): TColor;var  R, G, B: Integer;  Y: double;begin  R := C and $ff;  G := (C shr 8) and $ff;  B := (C shr 16) and $ff;  Y := 0.299*R + 0.587*G + 0.114*B;  if Y>128 then    Result:= clBlack  else    Result:= clWhite;end; Edited: This function uses muted colors, which is useful for dark mode. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function GetContrastColor(C: TColor; MutedColor: boolean): TColor;const  ColorsArray: array [0..3] of byte = (255, 208, 0, 48);var  R, G, B: Integer;  Y: integer;  k: integer = 0;begin  R := C and $ff;  G := (C shr 8) and $ff;  B := (C shr 16) and $ff;  Y := trunc(0.299*R + 0.587*G + 0.114*B) - 128;  if Y>0 then k:= 2;  if MutedColor and (abs(Y)> 80) then inc(k);  B:= ColorsArray[k];  Result := ((B and $ff) shl 16) or ((B and $ff) shl 8) or (B and $ff);end;