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

Share your advanced PureBasic knowledge/code with the community.
User avatar
jacdelad
Addict
Addict
Posts: 2010
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

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

Post 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
Post Reply