PC SOFT

FOROS PROFESIONALES
WINDEVWEBDEV y WINDEV Mobile

Inicio → WINDEV 2024 → WD20 - readable color for certain background
WD20 - readable color for certain background
Iniciado por guest, 12,oct. 2016 20:37 - 2 respuestas
Publicado el 12,octubre 2016 - 20:37
Hi,

I'm looking for a function to find a readable color for text on a given background color.
Like white text on black background or black text on a yellow surface.

I found one in one of the LST's and it use some calculation to change the HUE and SATURATION of a given color to determine the new readable one. Works OK but the effect is getting the same color lighter or darker.

I'm looking for something which returns let's say black for a all white, lightgray, yellow, lightred, etc backgrounds. Preferably not using some table of predefined colors, but for ANY given color.
Publicado el 13,octubre 2016 - 14:26
Hi Arie,

We used the following algorithm for many years with success (getting this old code back from the GDS; it should be good):

PROCEDURE CouleurFondVersCouleurTexte(nCouleurFond est une Couleur) : Couleur nLuminositéFond est un entier // White brightness = 255; Black brightness = 0. nLuminositéFond = ((nCouleurFond..Rouge * 299) + (nCouleurFond..Vert * 587) + (nCouleurFond..Bleu * 114)) / 1000 c est une Couleur SI ((255 - nLuminositéFond) > 125) ALORS c = iBlanc SINON c = iNoir FIN RENVOYER c
It proved to be bullet proof. We used that years before we started to use WinDev. It was found somewhere on the Web I guess.

When the LST went out we adapted the procedure, taking inspiration from the LST, to make it as follow:

PROCEDURE CouleurFondVersCouleurTexte(CouleurFond est une Couleur) : Couleur nLuminositéFond est un entier // White brightness = 255; Black brightness = 0. nLuminositéFond = 255 - (CouleurFond..Rouge * 299 + CouleurFond..Vert * 587 + CouleurFond..Bleu * 114) / 1000 c est une Couleur SI nLuminositéFond > 125 ALORS // c = iBlanc c = TSL(CouleurTeinte(CouleurFond),CouleurSaturation(CouleurFond),95) // 95 pour tirer vraiment sur le blanc, sinon avec fond rouge ce n'est pas bon SINON // c = iNoir c = TSL(CouleurTeinte(CouleurFond),CouleurSaturation(CouleurFond),10) // 10 pour voir la teinte FIN RENVOYER c
We hacked the values to make sure we have acceptable results in all the use case. We now use this last procedure since couple years (about since the LST went out), and I can't recall any reading problems with it. (After the tweaking, off course.)

Best regards,
Alexandre Leclerc
Publicado el 13,octubre 2016 - 15:14
Thanks Alexandre,

tried it right away and it is better indeed :spos: