Jump to content
Sign in to follow this  

Opposite color

Recommended Posts


Hi guys,

just a quick question...

Let's say I have the hex color 0xFFC90F which is something between yellow and orange.

I have a gui with that background color and a label on top of it.

The label needs to clearly readable. So you might suggest to make its font black.

Well, but the background color can be changed. The font will be invisible or hardly to read when the background is black or grey.

How can I let autoit calculate a good font color depending on the GUI background?


EDIT: Sorry for posting in the wrong section. Please move it, thanks!

Edited by RomanK

[font="Courier New"]http://RomanK.hondadesigns.com[/font]

Share this post

Link to post
Share on other sites

Can't remember where I found this piece of code, kudos to the author :)...

#Include <Color.au3>


Func _ColorInverse($nColor)
    Local $nRed = _ColorGetRed($nColor)
    Local $nGreen = _ColorGetGreen($nColor)
    Local $nBlue = _ColorGetBlue($nColor)
    Return "0x" & Hex(255 - $nRed, 2) & Hex(255 - $nGreen, 2) & Hex(255 - $nBlue, 2)
EndFunc   ;==>_ColorInverse

Share this post

Link to post
Share on other sites

This should get better readable colors:

Func _CalcContrastColor ($crBg)
    ; http://www.codeproject.com/KB/tips/JbColorContrast.aspx
    Local Const $TOLERANCE = 30

    if ( _
        abs(BitAND($crBg  , 0xFF) - 0x80) <= $TOLERANCE And _
        abs(BitAND(BitShift($crBg , 8) , 0xFF) - 0x80) <= $TOLERANCE And _
        abs(BitAND(BitShift($crBg , 16) & 0xFF) - 0x80) <= $TOLERANCE _
    ) Then return BitAND((0x7F7F7F + $crBg) , 0xFFFFFF);

    return BitXOR($crBg ,0xFFFFFF);

(opposite color is not a good choice for our eyes)

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post

Link to post
Share on other sites

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.