Jump to content

pixelGetColor - wrong color?


 Share

Recommended Posts

I have checked the help file, as well as searched the forum, but can't seem to figure out why this isn't working. To test the function, I used the  Script (Created by Valuater) found on another post here. Here are a couple screenshots, I just used my desktop background as the initial test. The first one, my mouse is hovering over the black color, second one, the mouse was hovering over the homework folder, and the third was hovering inside the yellow circle (purple color). 

 

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

; BE SURE TO CHECK THESE TWO ***************************************************
Opt("MouseCoordMode", 0) ;1=absolute, 0=relative, 2=client
Opt("PixelCoordMode", 0) ;1=absolute, 0=relative, 2=client


Global $var, $Color_win, $pos, $my_color, $check

HotKeySet("{ESC}", "Terminate")
HotKeySet("{F9}", "Get_Color")
HotKeySet("{F8}", "Color_Box")
HotKeySet("{F7}", "Check_Color")

ToolTip('Get Color - is Running (F7 - Check Color)(F8 - Color Box)(F9 - Get Color)',0,0)
While 1
    Sleep(100)
WEnd
;;;;;;;;

Func Color_Box()
    $var = _ChooseColor (2)
    $pos = MouseGetPos()
    Show_Color()
EndFunc
Func Terminate()
    Exit 0
EndFunc
Func Get_Color()
    $pos = MouseGetPos()
    $Svar = PixelGetColor(  $pos[0] , $pos[1])
    $var = "0x" & Hex($Svar,6)
    Show_Color()
EndFunc
Func Show_Color()
    GUIDelete($Color_win)
    ClipPut($var)
    $my_color = $var
    $Color_win = GUICreate("RGB Color = " & $var, 290, 150, -1, -1)
    GUISetBkColor($var)
    GUISetFont(9, 400, -1, "MS Sans Serif")
    GUICtrlCreateLabel(" This Color has been Copied to the ClipBoard  " & @CRLF & @CRLF & "  Just Paste it wherever you would like"& @CRLF & @CRLF & @CRLF & " Mouse position X=" & $pos[0] & "  Y=" & $pos[1]   , 10, 10, 270, 100)
    GUICtrlSetFont(-1, 9, 650)
    If $var = 0x000000 Then GUICtrlSetColor( -1, 0xFFFFFF)
    $OK_Btn = GUICtrlCreateButton("&OK", 100, 110, 80, 30)
    GUISetState()
    While 2
        $msg1 = GUIGetMsg()
        If $msg1 = $OK_Btn Or $msg1 = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
    GUIDelete($Color_win)
EndFunc
Func Check_Color()
    $check = NOT $Check
    If $check Then ToolTip('Get Color - is Checking Color',0,0)
    While $check
        $var2 = PixelGetColor(  $pos[0] , $pos[1])
        If $my_color <> $var2 Then
            MsgBox(262208, "Color Check", " the Color has changed   ")
            ExitLoop
        EndIf
    WEnd
    ToolTip('Get Color - is Running',0,0)
EndFunc

 

black.jpg

folder.jpg

purple.jpg

Link to comment
Share on other sites

  • Moderators

@Pierson5 welcome to the forum. PixelGetColor is, as you are coming to find out, wholly unreliable. How about explaining your what you're trying to automate or do? There is certainly a better way to do it.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Why would you be using relative position to a window, and then look for pixels outside of the window?  make the options absolute, and they might return what you expect.

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

1 hour ago, JLogan3o13 said:

@Pierson5 welcome to the forum. PixelGetColor is, as you are coming to find out, wholly unreliable. How about explaining your what you're trying to automate or do? There is certainly a better way to do it.

I run speakers from my laptop to my garage. I use spotify. I figured color detection would be a good way to determine when an Add pops up, and I can have autoit turn down the volume, or click the add for 30minutes of add free music. Not sure if this is against the rules, but it would make it so I don't have to walk in the house to do it manually. I've been using autoit for similar small tasks and it's been a great learning experience, this is one where I got stuck. 

 

 

54 minutes ago, jdelaney said:

Why would you be using relative position to a window, and then look for pixels outside of the window?  make the options absolute, and they might return what you expect.

I tried changing it to absolute, similar results. I used the autoit tool that came with the program, and it gives the same results as well. So I don't think it's the code, that was just to verify what was going on. 

Link to comment
Share on other sites

1 minute ago, Earthshine said:

You know Bluetooth speakers aren’t that expensive and you could run your phone for the music

They are nice speakers ;). I could also just pay for Spotify premium. It was more for the practice and to "see if I can." If pixelGetColor has some issues and doesn't work right, that's one thing. If it's because I'm doing it wrong, that's another. I'm a CS major in my second year in college. I'm learning different languages for the practice, something to add to my toolbox if I need it later down the road. 

Link to comment
Share on other sites

I like the premium because you get the premium quality of the music too. I  use iTunes Match Because they let me stream all the albums I own and they let me upload all the albums that They don’t have to the cloud so I can get my music from anywhere in high-quality

25$ yr 

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

This any good to you?

$nX = 10
$nY = 10

;Get a color and pass it to function
$Color = PixelGetColor($nX, $nY)

;Wait for color to change or time out after 5 seconds.
$NewColor = _ColorWaitChanged($nX, $nY, $Color, 5000)
If @error Then
    MsgBox(0, "Error: " & @error, "Color did not change")
    Exit
EndIf

MsgBox(0, "Success", "Color changed to 0x" & Hex($NewColor, 6))

;###########################################################################
;Waits for passed color at the given co-ordinates to to change or time out.
;###########################################################################
Func _ColorWaitChanged($x, $y, $col = 0, $timeout = 0, $sleep = 100)
    Local $Timer
    If $timeout Then
        $Timer = TimerInit()
    EndIf
    Do
        If $timeout Then
            If TimerDiff($Timer) >= $timeout Then
                Return SetError(1, 0, 0)
            EndIf
        EndIf
        Sleep($sleep)
    Until (PixelGetColor($x, $y) <> $col)
    Return PixelGetColor($x, $y)
EndFunc   ;==>_ColorWaitChanged

 

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

34 minutes ago, JohnOne said:

This any good to you?

$nX = 10
$nY = 10

;Get a color and pass it to function
$Color = PixelGetColor($nX, $nY)

;Wait for color to change or time out after 5 seconds.
$NewColor = _ColorWaitChanged($nX, $nY, $Color, 5000)
If @error Then
    MsgBox(0, "Error: " & @error, "Color did not change")
    Exit
EndIf

MsgBox(0, "Success", "Color changed to 0x" & Hex($NewColor, 6))

;###########################################################################
;Waits for passed color at the given co-ordinates to to change or time out.
;###########################################################################
Func _ColorWaitChanged($x, $y, $col = 0, $timeout = 0, $sleep = 100)
    Local $Timer
    If $timeout Then
        $Timer = TimerInit()
    EndIf
    Do
        If $timeout Then
            If TimerDiff($Timer) >= $timeout Then
                Return SetError(1, 0, 0)
            EndIf
        EndIf
        Sleep($sleep)
    Until (PixelGetColor($x, $y) <> $col)
    Return PixelGetColor($x, $y)
EndFunc   ;==>_ColorWaitChanged

 

Not quite. It will detect some changes, but not others. I think it has something to do with AutoIt's method of determining the colors. I'm not sure how the specifics of it works, but if I use the AutoIt Window Info, the color it gives me seems so random. 

Just on the forum for example, when I move the finder tool over the white area above the blue bar, it gives me FFFFFF (white), which is correct. But when I move the finder tool down over the blue bar and then the gray bar, the color code does not change. Do you guys get similar results? Or does it have something to do with my computer?

white.jpg

Link to comment
Share on other sites

I believe so? In case I was doing something stupid with the AutoIt Window tool, I also ran the previous code in the first post, clicked on this web page, and hit F9 to get the color code according to my cursor position. The top of this page (blue) seemed to work fine (the dark blue and gray underneath return the same color code, however). The white seems to work fine. Then the blue bar "# posts in this topic" comes up white. 

top.jpg

mid.jpg

bottom.jpg

Link to comment
Share on other sites

As I recall aero (win7) messes up colour, see if disabling it helps.

It messes mine up in win 10 with au3info. win 10 -> settings -> personalization -> colors -> transparency effects to off.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Thanks man, added this to the script from my first post:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_HiDpi=Y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

Script is working perfectly now. +Rep'd

 

Thankyou everyone for your help. I don't think I would have figured that one out. Love the forum.

Link to comment
Share on other sites

  • 5 years later...
On 2/10/2018 at 4:23 AM, InnI said:

@Pierson5 It looks like you use windows scaling

Thanks so much for this post.  Couldn't figure out why my colors were not being detected properly.

Can't believe there was so little posted about this and no one else has asked this issue in 6 years!

Edited by bengalih
Link to comment
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
 Share

  • Recently Browsing   0 members

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