Page 1 of 1

Determine whether to use dark or light font/drawing...

Posted: Tue Mar 26, 2024 4:05 am
by jacdelad
Hi,
I don't know if this was posted here before, but I couldn't find something similar.

I needed a simple test whether to draw light or dark text on a canvas with a not predefined color. There are some algorithms and explanations out there which are more or lesse complex. Some even used several stages to determine the best contrast, some adjust by human perception and so on.
However, the simplest one I found is already suiting my needs (and also uses a simple calculation regarding perception) and maybe someone else can use this too (looking at ObjectTheme and auto creation of themes or so):

Code: Select all

Macro IsDark(Color)
  Bool(0.299*Red(Color)+0.587*Green(Color)+0.114*Blue(Color)<=186)
EndMacro
...returns #True if the color is perceived as dark.

Re: Determine whether to use dark or light font/drawing...

Posted: Tue Mar 26, 2024 5:12 am
by BarryG
Thanks for sharing. :)

You inspired me to share my ListIcon gridlines color-matching tip -> https://www.purebasic.fr/english/viewtopic.php?t=83901