Jump to content

PixelGetColor (called with wrong number of args)


Go to solution Solved by Guest,

Recommended Posts

Posted

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))

 

  • Developers
Posted
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.
  :)

Posted

Not sure what to look for exactly, I know that PixelGetColor should have comma separated value like: PixelGetColor(100, 100)

I tried

$color = PixelGetColor($position[1],$position[2])

The script starts, but the MsgBox doesn't show

  • Developers
Posted
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.
  :)

Posted

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

  • Solution
Posted (edited)
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
Posted (edited)

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
Posted
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.

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
×
×
  • Create New...