Jump to content

PixelGetColor (called with wrong number of args)


Go to solution Solved by Musashi,

Recommended Posts

I'm trying to create a simple script for getting the hex color value of a position on the screen from a variable given by an input box.

The script is not starting because of that error, I don't really understand why it's not working this way, would really appreciate if anyone can explain this to me.

#include <MsgBoxConstants.au3>
$position = InputBox("", "position:", "", "", 200, 150,  @DesktopWidth / 2-125, @DesktopHeight / 2-100, 0)
$color = PixelGetColor($position)
MsgBox($MB_SYSTEMMODAL,"","Hex Color: " & Hex($color, 6))

 

Link to post
Share on other sites
  • Developers
2 minutes ago, ManualIT said:

I don't really understand why it's not working this way,

Did you try opening the helpfile and actually check what the syntax is? ;) 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites
  • Developers

So you did read the help page and looked at the example? https://www.autoitscript.com/autoit3/docs/functions/PixelGetColor.htm

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites
  • Developers
1 hour ago, ManualIT said:

But why in this way it doesn't?

because  PixelGetColor(1,10) is NOT the same as PixelGetColor("1,10"). I would like to refer you to some sort of basic programing course when that's not clear to you yet. ;) 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites

OK let me just say that I've always thought that whatever value is assigned to a variable that is between quotes, the quotes will be ignored when the variable is called

like for example:

$sleep = "1000"
Sleep($sleep)
exit

 

I honestly never knew that I could use Sleep("1000") I always thought it has to be Sleep(1000) without quotes, so using it that way always gave me the idea that quotes will be ignored.

And I have always used the Sleep function as the example above in my scripts, so this whole thing got me really confused

Link to post
Share on other sites
  • Solution
1 hour ago, ManualIT said:

OK let me just say that I've always thought that whatever value is assigned to a variable that is between quotes, the quotes will be ignored when the variable is called

(simplified description)
AutoIt works internally with the data type Variant.
For example, if you pass a string "500" to the Sleep function, AutoIt tries to get the numeric part of it, starting with the first character.

Try for demonstration purposes :

#include <Timers.au3>

Local $hStarttime = _Timer_Init()
Sleep("500,2500") ; -> 500
ConsoleWrite("Time elapsed (ms) = " & _Timer_Diff($hStarttime) & @CRLF)

$hStarttime = _Timer_Init()
Sleep("2500,500") ; -> 2500
ConsoleWrite("Time elapsed (ms) = " & _Timer_Diff($hStarttime) & @CRLF)

$hStarttime = _Timer_Init()
Sleep("1500A0") ; -> 1500
ConsoleWrite("Time elapsed (ms) = " & _Timer_Diff($hStarttime) & @CRLF)

PixelGetColor expects at least 2 parameters (x-coordinate , y-coordinate).

If you pass a string like "10, 100", then only the value 10 is evaluated as a parameter. 

@ManualIT : Additional Info :

#include <MsgBoxConstants.au3>

Local $sInput = "10, 100"
Local $aPosition = StringSplit($sInput, ",", 2) ; 2=$STR_NOCOUNT
Local $iColor = PixelGetColor($aPosition[0], $aPosition[1])
MsgBox($MB_SYSTEMMODAL,"","Hex Color: " & Hex($iColor, 6))

However, I would not make this "AutoIt feature" a habit, instead I would pass the data types that the respective function expects ;).

Edited by Musashi
additional info

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to post
Share on other sites

Thank you for explaining!

 

From that, now I understand that I have to give 2 different values to PixelGetColor

Something like PixelGetColor($value1,$value2)

But how can this be done with one input box?

Edited by ManualIT
Link to post
Share on other sites
4 minutes ago, ManualIT said:

But how can this be done with one input box?

Check out the small example I added to my post above. You can separate e.g. "10,100" with StringSplit, and then use the two elements of the resulting array.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

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

    No registered users viewing this page.

×
×
  • Create New...