Determine if string equals a GUI colour

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Determine if string equals a GUI colour

04 May 2016, 04:43

Does a 6 letter long string always translate to an AHK GUI color? How to determine if a string is a legitimate color or not? Thanks.
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

Re: Determine if string equals a GUI colour

04 May 2016, 09:44

Any 6 digit hexadecimal value is a valid color. You can use code similar to the follow to check if a string is a 6 digit hex value...

Code: Select all

String := "A1B2C3"
If (String is xdigit) AND (StrLen(String) = 6)
	MsgBox %String% is a valid color.
Else
	MsgBox %String% is NOT a valid color.
Note: It is sometimes possible to use common color names (such as "Red", "Blue", "Yellow", etcetera) instead of hex values, and the above code does not check for those.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Determine if string equals a GUI colour

04 May 2016, 10:03

Shadowpheonix

Code: Select all

String := "xxxxxx"
If (String is xdigit) AND (StrLen(String) = 6)
	MsgBox %String% is a valid color.
Else
	MsgBox %String% is NOT a valid color.
If (String is xdigit) Uses expression syntax. It's the same as doing If ("xxxxxx" "" "")
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

Re: Determine if string equals a GUI colour

04 May 2016, 10:24

Kon,

Thank you for pointing that out. I must have mis-typed something when I tested the original code with various values, as it appeared to work correctly. However, I just retested it now and it is definitely failing.

Here is the corrected code...

Code: Select all

String := "xxxxxx"
If String is xdigit
{
	If (StrLen(String) = 6)
		MsgBox %String% is a valid color.
}
Else
	MsgBox %String% is NOT a valid color.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Determine if string equals a GUI colour

04 May 2016, 10:32

Code: Select all

Gui, Color, 7f
Gui, Show, w400 h400
Still seems to work. ;) Looks like any non-negative number less than or equal to 0xffffff is valid.

And as you mentioned, the color names are also valid: Black,Green,Silver,Lime,Gray,Olive,White,Yellow,Maroon,Navy,Red,Blue,Purple,Teal,Fuchsia,Aqua
So maybe if var in matchlist could be used too.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder and 189 guests