CheckColor / WaitColor - Color names of HEX color Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

CheckColor / WaitColor - Color names of HEX color

Post by Albireo » 26 Mar 2024, 09:10

Hi!
I want two functions() to handle colors.
Check Color() - Reads a color on one dot.
and
WaitColor() - Waits until a certain color occurs at the specified coordinate.

At the same time, I want to be able to name certain colors (estimated 10-20 pcs) in the call of the function().
eg. #FF0000 = Red and #800000 = DarkRed.
Another request could be that several HEX colors can be given the same name and so on.
Know there are many details I have skipped in this example.

the function Color := CheckColor(X, Y, WinName)
e.g.

Code: Select all

Color := CheckColor("254", "628", "ahk_exe Test.exe)"
The result can be Red or #7F0000 if the color is not specified in the function().
_____________________________________________________________________________

The other function ColorWait(PosX, PosY, WinName, Color, MaxTime) works in a similar way.

Code: Select all

ColorWait(157, 227, "ahk_pid " progPID, "LightGreen", 5)
_____________________________________________________________________________

My questions .:
Do these functions() already exist?

Code: Select all

ColorWait(posX, PosY, WinName, Color, MaxTime)
WaitColor := "WrongColor"
If Color := "White"
	WaitColor := "0xFFFFFF"
if Color := "Black"
	WaitColor := "0x000000"
....
If WaitColor = "WrongColor"
	MsgBox "the color " Color "is not defined"
....
Wait
	PixelGetColor ....
ColorCheck() is quite similar to ColorWait()
Can the comparisons be made on another way?

User avatar
boiler
Posts: 16978
Joined: 21 Dec 2014, 02:44

Re: CheckColor / WaitColor - Color names of HEX color

Post by boiler » 26 Mar 2024, 14:51

Instead of a bunch of if statements, you can use a Map:

Code: Select all

Color := 'Red' ; also test with 'WrongColor' or other color names
ColorValues := Map('White', 0xFFFFFF, 'Black', 0x000000, 'Red', 0xFF0000, 'DarkRed', 0x800000)
Try
	WaitColor := ColorValues[Color]
Catch {
	MsgBox 'The color "' Color '" is not defined'
	return
}
MsgBox 'The color in hex format is ' Format('{:#x}', WaitColor)
;PixelGetColor ...

Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Re: CheckColor / WaitColor - Color names of HEX color

Post by Albireo » 18 Apr 2024, 10:31

I try to understand...

If I add this instructions .:

Code: Select all

For key, value in ColorValues
      res .= key`t' = `t' value '`n'
MsgBox res
The HEX values have been converted to decimal numbers (which was a surprise)
Does it always happen?

Is it easy to know if a variable contains a HEX number?

User avatar
boiler
Posts: 16978
Joined: 21 Dec 2014, 02:44

Re: CheckColor / WaitColor - Color names of HEX color

Post by boiler » 18 Apr 2024, 10:42

Albireo wrote: The HEX values have been converted to decimal numbers (which was a surprise)
Does it always happen?

Is it easy to know if a variable contains a HEX number?
A variable doesn't contain a hex or decimal number. Those are just different number systems for displaying integers (i.e., it's the same number internally). The default number system that AHK and almost everything else uses is decimal (base 10). If you want to display that same number in hexadecimal (base 16), then you have to format it as such.

Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Re: CheckColor / WaitColor - Color names of HEX color

Post by Albireo » 18 Apr 2024, 11:23

OK!
Thank you!

Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Re: CheckColor / WaitColor - Color names of HEX color

Post by Albireo » 18 Apr 2024, 14:54

I find it difficult to know when a window has been opened.
I haven't found the right solution.

This doesn't work.
  • 1) Run 'notepad.exe',,, &tstPID
  • 2) if !WinWaitActive(WinName,, actMin)
  • 3) if PixelSearch(&Px, &Py, xPos1, yPos1, xPos2, yPos2, hexColor, cDiff )
This works!
  • 1) Run 'notepad.exe',,, &tstPID
  • 2) if !WinWaitActive(WinName,, actMin)
  • 3) Pause 1000
  • 4) if PixelSearch(&Px, &Py, xPos1, yPos1, xPos2, yPos2, hexColor, cDiff )
What to do?

A test example - adapted for Swedish conditions
( Sorry for Swedish comments )

User avatar
boiler
Posts: 16978
Joined: 21 Dec 2014, 02:44

Re: CheckColor / WaitColor - Color names of HEX color  Topic is solved

Post by boiler » 18 Apr 2024, 16:29

Code: Select all

Run 'notepad.exe',,, &tstPID
WinWaitActive 'ahk_pid ' tstPID
ColorCheck(40, 20, 'Ljusgrön', 'ahk_pid ' tstPID)
MsgBox "Done"

Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Re: CheckColor / WaitColor - Color names of HEX color

Post by Albireo » 18 Apr 2024, 17:52

This code work! (with Sleep 1000)

Code: Select all

#Requires Autohotkey 2.0
#SingleInstance Force
CoordMode 'Pixel', 'Window'

Run 'notepad.exe',,, &tstPID
WinWaitActive 'ahk_pid ' tstPID
Sleep 1000
Color := PixelGetColor( 40, 20 )
MsgBox  '(0x87C5D2) => ' Color

This code doesn't work (without Sleep)

Code: Select all

#Requires Autohotkey 2.0
#SingleInstance Force
CoordMode 'Pixel', 'Window'

Run 'notepad.exe',,, &tstPID
WinWaitActive 'ahk_pid ' tstPID
Color := PixelGetColor( 40, 20 )
MsgBox  '(0x87C5D2) => ' Color
Is there something I don't understand with "WinWait"?
Is there another way to know that the window is active?

User avatar
boiler
Posts: 16978
Joined: 21 Dec 2014, 02:44

Re: CheckColor / WaitColor - Color names of HEX color

Post by boiler » 18 Apr 2024, 18:32

It could mean that it is officially the active window in the eyes of the OS, but it has not finished drawing the window by the time you are checking the color, so a little delay is necessary. I would expect that a delay of much shorter than 1000 should be adequate. More like 100.

Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Re: CheckColor / WaitColor - Color names of HEX color

Post by Albireo » 19 Apr 2024, 16:24

OK the color is saved as an integer in the object.

The hidden script below give the folowing result
black = 0
darkred = 8388608
lightgreen = 8898002
red = 16711680
white = 16777215
Create an object with colornames and RGB-colors
1)
Is it complicated to convert from an integer to hex?
( ex. from 16777215 to 0xFFFFFF )


The instructions

Code: Select all

Try
	WaitColor := ColorValues[Color]
Catch {
	MsgBox 'The color "' Color '" is not defined'
	return
}
convert the colorname to an HEX-color (good)


2)
Is it complicated to do the other way from the same object?
eg.
the variable Color contains 0x87C5D2 (from the instuction Color := PixelGetColor( xPos, yPos ) )
And I want the result lightgreen
It would have been best if the same object could have been used for both desires
(multiple object with the same information need not be maintained)

User avatar
boiler
Posts: 16978
Joined: 21 Dec 2014, 02:44

Re: CheckColor / WaitColor - Color names of HEX color

Post by boiler » 19 Apr 2024, 16:50

No conversion is necessary because they are the same number. As I said, it's stored as an integer, and hex is just one way of representing it. You're seeing the decimal representation, but it equals the same as the hexadecimal representation. And it's neither hex nor decimal internally -- it's just the integer number. Maybe this script will help you believe it:

Code: Select all

if (16777215 = 0xFFFFFF)
	MsgBox 'Yes, they are equal.'
else
	MsgBox 'No, they are not equal.'

So to be clear:

16777215 is an integer represented in the decimal numbering system.
0xFFFFFF is also an integer (the same one) represented in the hexadecimal numbering system.
Internally, it is captured as a series of bits (111111111111111111111111) no matter which way you display it.

Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Re: CheckColor / WaitColor - Color names of HEX color

Post by Albireo » 19 Apr 2024, 19:01

Thank you!
0x is the key. (FFFFFF is not enough)
But I think it's easier to know/check what color a hex number is.
( eg. I find it easier to know that 0xFFFFFF is white, than 16777215 )

Is there any good way to compare e.g. LightGreen with lightgreen
?

This somehow works. (attempt to answer my second question)

Code: Select all

#Requires Autohotkey 2.0
#SingleInstance Force

cValue := Map( ''	,
	,	'white'			,	0xFFFFFF
	,	'black'			,	0x000000
	,	'lightgreen'	,	0x87C5D2
	,	'red'				,	0xFF0000
	,	'darkred'		,	0x800000
)
cName := Map()
For key, value in cValue
{	res .= key`t' = `t' value '`n' ; Create a view of cValue
	cName[value] := key
}
/*
; Check the result on cName
For key, value in cName
	res1 .= key`t' = `t' value '`n'	
; MsgBox res "`n`n" res1
*/

Color := 0x87C5D2
Try
	WaitColor := cName[Color]
Catch {
	MsgBox 'The color "' Color '" is not defined'
	return
}
MsgBox 'The color name is .: ' WaitColor
Haven't checked what restrictions the code has.
for example. what happens if two different colors get the same Hex-RGB?

User avatar
boiler
Posts: 16978
Joined: 21 Dec 2014, 02:44

Re: CheckColor / WaitColor - Color names of HEX color

Post by boiler » 19 Apr 2024, 22:12

Albireo wrote: Is there any good way to compare e.g. LightGreen with lightgreen?
I’m not sure what you mean. If you had both entries in your map and the were both mapped to the same color value? It would result in the last one as it enumerates the map unless you were to break after thr first one, then it would return that one. By first/last, I mean by the order AHK uses to enumerate them, not the order they appear in your assignment.

Albireo wrote: what happens if two different colors get the same Hex-RGB?
If you mean you assigned the same value to two different keys (color names), then the same answer as above. These things are easily tested.

Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Re: CheckColor / WaitColor - Color names of HEX color

Post by Albireo » 22 Apr 2024, 06:36

You have right! (thank you)
this works!

Code: Select all

#Requires Autohotkey 2.0
inpVar := "TestLeTTeR"
If inpVar = "testletter"
	MsgBox "Yes! " inpVar "`nis found."
else
	MsgBox "No! " inpVar "`nis nor found."

Post Reply

Return to “Ask for Help (v2)”