Jump to content

Recommended Posts

Posted

Hi there,

I need a function to check if certain color is a shade of white or black (dark) color, i searched on the forum, but didn't find anything that might help with my task.

Basicaly i need to update a label (it's background) with some color, and then set color for the control, but if the background color is dark, and the label text color dark as well (set by user), the text is almoust unreadable.

Here is an example of what i am using for now:

#include <GuiConstants.au3>

$Bk_Color = 0x000000 ;0xFFFFFF
$Color = 0x000000

$GUI = GUICreate("Test Script", 300, 200)

GUICtrlCreateLabel("Some Text", 20, 40, 55, 14)
GUICtrlSetBkColor(-1, $Bk_Color)

If $Bk_Color = 0x000000 Then $Color = 0xFFFFFF
GUICtrlSetColor(-1, $Color)


GUISetState(@SW_SHOW, $GUI)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

So here i check if the back color is black, and if it does i just set the text color to white... but if the colors are little different, but still have black/white shade, text color not readable very well :).

Is there maybe a method to check color for «darknest/whitnest»? :)

Thanks.

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted (edited)

  Quote

You can use _ColorGetBlue/Red/Green and do a calculation with those values.

Big thanks! :)

Here is what i needed i think:

#include <GuiConstants.au3>
#include <Misc.au3>

Global $Bk_Color = 0xFFFFFF

$GUI = GUICreate("_ColorIsDarkShade Demo", 300, 200)

$Label = GUICtrlCreateLabel("Some Text", 20, 40, 55, 14)
GUICtrlSetBkColor(-1, $Bk_Color)

$ColorSet_Button = GUICtrlCreateButton("Set Color", 20, 80)

GUISetState(@SW_SHOW, $GUI)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ColorSet_Button
            $Bk_Color = _ChooseColor(2, $Bk_Color, 2, $GUI)
            
            GUICtrlSetBkColor($Label, $Bk_Color)
            GUICtrlSetColor($Label, 0x0)
            
            If _ColorIsDarkShade($Bk_Color) Then GUICtrlSetColor($Label, 0xFFFFFF)
    EndSwitch
WEnd

Func _ColorIsDarkShade($nColor)
    Local $i_Red = BitAND(BitShift($nColor, 16), 0xFF)
    Local $i_Green = BitAND(BitShift($nColor, 8), 0xFF)
    Local $i_Blue = BitAND($nColor, 0xFF)
    
    Local $iMidle_Color_Val = Int(765 / 2) ;765 is the total of 255 + 255 + 255
    
    Return ($i_Red + $i_Green + $i_Blue) < $iMidle_Color_Val
EndFunc

Edit: Changed the example.

Edited by MsCreatoR

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...