Jump to content
Sign in to follow this  
Phaser

Timing pixel colour changes

Recommended Posts

Phaser

Hi Guys

I have an app that at some point a pixel changes colour, well not just one pixel a small area but I can target just one pixel from that area. It needs to start when it first changes, its just a flicker, then stop timing it when it flickers again, heres what I am using but I don't think its very good, I would like to know the gap from the first flicker to the second

$check = PixelGetColor( 80 , 92 ,$hWnd)
 
If Not (PixelGetColor( 80 , 92 ,$hWnd) = $check) Then
  $starttime = _Timer_Init()
  $checking = "on"
  Sleep(500)
  If $checking = "on" Then
   If Not (PixelGetColor( 230 , 70 ,$hWnd) = $check) Then
    $result = _Timer_Diff($starttime)
    $checking = "off"
   EndIf
   MsgBox(0,"The Timer", "Change " & $result ,1)
  
  EndIf
 
EndIf

It doesn't seem to wait for the second flicker, the msgbox keeps showing when the first flicker has just happened, the gap is about 2 seconds but I would like milliseconds, currently it displays something like 0.061728346439 and I don't know why.

Thanks for looking

Edited by Phaser

Share this post


Link to post
Share on other sites
kaotkbliss

I assume your program is doing something like this:

Finds the pixel, it matches

Pixel changes color, does not match, start timer, turn on

if on (which is "yes") check color (which still does not match)

turn check off

show message box

so you would need a 2nd check to wait until the color matches again. Unless, of course, I'm envisioning the flicker wrong.


010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites
Phaser

Hi

Basically yes, it gets the colour of the pixel, when the colour changes it starts timer, then pixel goes back to original colour, when it changes again it should stop the timer and tell me how long it took, is that possible? what's wrong with my code?

Thank for your help

Share this post


Link to post
Share on other sites
Melba23

Phaser,

I would do it like this: :graduated:

#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)

$hButton_1 = GUICtrlCreateButton("Change", 10, 10, 80, 30)
$hButton_2 = GUICtrlCreateButton("Reset", 10, 50, 80, 30)

GUISetState()

; Set initial values
$aPos = WinGetPos($hGUI)
$iColour_Start = PixelGetColor($aPos[0] + 400, $aPos[1] + 400)
$iBegin = 0

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton_1
            GUISetBkColor(0xFF0000)
        Case $hButton_2
            GUISetBkColor($iColour_Start)
    EndSwitch

    ; Read the current colour
    $iColour = PixelGetColor($aPos[0] + 400, $aPos[1] + 400)

    ; When the colour changes
    If $iColour <> $iColour_Start And $iBegin = 0 Then
        $iBegin = TimerInit()
        ConsoleWrite("Initial colour changed" & @CRLF)
    EndIf

    ; When the colour changes back
    If $iColour = $iColour_Start And $iBegin <> 0 Then
        ConsoleWrite("Other colour present for: " & Round(TimerDiff($iBegin) / 1000, 1) & " secs" & @CRLF)
        $iBegin = 0
    EndIf

WEnd

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Phaser

It keeps giving me 0 as though its timing how long the pixel was changed for, thats not what I am after, I need to know the gaps between change 1 and change 2, for example

original colour is red

start checking......

it changed to green, then back to red

start timer

its still red

its still red

it changed to green, then back to red

stop timer

what was the gap between the 2 changes, hope that explains it properly

Share this post


Link to post
Share on other sites
Melba23

Phaser,

It would have helped to have had such a clear explanation in the first post! ;)

Try this: :graduated:

#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)
GUISetBkColor(0xFF0000)

$hButton_1 = GUICtrlCreateButton("Change", 10, 10, 80, 30)
$hButton_2 = GUICtrlCreateButton("Reset", 10, 50, 80, 30)

GUISetState()

; Set initial values
$aPos = WinGetPos($hGUI)
$iColour_Start = PixelGetColor($aPos[0] + 400, $aPos[1] + 400)
$iBegin = 0
$iChange = 0

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton_1
            GUISetBkColor(0x00FF00)
        Case $hButton_2
            GUISetBkColor($iColour_Start)
    EndSwitch

    ; Read the current colour
    $iColour = PixelGetColor($aPos[0] + 400, $aPos[1] + 400)

    ; When the colour changes to green
    If $iColour <> $iColour_Start Then
        Switch $iChange
            Case 0
                ConsoleWrite("First change to GREEN" & @CRLF)
                $iChange = 1
            Case 2
                ConsoleWrite("Second change to GREEN" & @CRLF)
                $iChange = 3
        EndSwitch
    EndIf

    ; When the colour changes back to red
    If $iColour = $iColour_Start Then
        Switch $iChange
            Case 3
                ConsoleWrite("Second change back to RED - Stop timer" & @CRLF & "Time: " & Round(TimerDiff($iBegin) / 1000, 1) & " secs" & @CRLF)
                $iChange = 0
            Case 1
                ConsoleWrite("First change back to RED - Start timer" & @CRLF)
                $iBegin = TimerInit()
                $iChange = 2
        EndSwitch
    EndIf

WEnd

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Phaser

It would have helped to have had such a clear explanation in the first post!

Sorry about that, thanks again will try this.

Share this post


Link to post
Share on other sites
Phaser

Hi Melba

Is there anyway I can put reply #7 into a function, I want to be able to click a button 'Go' then it calls the function, returns the time of said action, I use the time in the rest of my script then when I want to click Go again it repeats the process. I have tried but can't get out of the while loop, so in short

click Go

it checks the start pixel colour

pixel changes colour

pixel changes back to start colour

start timer

pixel changes colour

pixel changes back to start colour

stop timer

give duration between the two changes so I can use it, then do nothing until I click Go again and repeat the whole process so would like to put it into a function, possible?

Share this post


Link to post
Share on other sites
Melba23

Phaser,

Save this script as "Colouriser.au3":

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
 
$hGUI = GUICreate("Colouriser", 200, 200, 100, 100)
GUISetBkColor(0xFF0000)
GUISetState()
 
$iBegin = TimerInit()
$iDuration = Random(1, 5, 1) * 1000
$fColour = False
 
While 1
 
    If GUIGetMsg() = $GUI_EVENT_CLOSE Then
        Exit
    EndIf
 
    If TimerDiff($iBegin) > $iDuration Then
        $fColour = Not $fColour
        Switch $fColour
            Case True
                GUISetBkColor(0x00FF00)
            Case False
                GUISetBkColor(0xFF0000)
        EndSwitch
        $iBegin = TimerInit()
        $iDuration = Random(2, 10, 1) * 1000
    EndIf
 
WEnd

It produces a GUI which changes colour at random intervals.

Then save this script under what ever name you want as long as it is in the same folder: ;)

#include <GUIConstantsEx.au3>
 
; Start the Colouriser GUI
Run("AutoIt3.exe Colouriser.au3")
; And wait until it is active
WinWaitActive("Colouriser")
 
; Now create our GUI
$hGUI = GUICreate("Test", 200, 200)
$hButton = GUICtrlCreateButton("Go", 10, 10, 80, 30)
GUISetState()
 
While 1
 
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton
            ; Start the colour checker
            _Colour_Timer()
    EndSwitch
 
WEnd
 
Func _Colour_Timer()
 
    ; Set initial values - adjust these 2 lines to match the pixel in GUI you are interrogating
    $aPos = WinGetPos("Colouriser")
    $iColour_Start = PixelGetColor($aPos[0] + 100, $aPos[1] + 100)
    ; What did we start with?
    Switch $iColour_Start
        Case 0xFF0000
            ConsoleWrite("Initial Colour Red at  : " & @SEC & @CRLF)
        Case 0x00FF00
            ConsoleWrite("Initial Colour Green at: " & @SEC & @CRLF)
    EndSwitch
    $iBegin = 0
    $iChange = 0
 
    ; Now look for changes
    While 1
 
        ; Read the current colour - adjust these 2 lines to match the pixel in the GUI you are interrogating
        $aPos = WinGetPos("Colouriser")
        $iColour = PixelGetColor($aPos[0] + 100, $aPos[1] + 100)
 
        ; When the colour changes
        If $iColour <> $iColour_Start Then
            Switch $iChange
                Case 0
                    $iChange = 1
                    ConsoleWrite("1st change at: " & @SEC & @CRLF)
                Case 2
                    $iChange = 3
                    ConsoleWrite("3rd change at: " & @SEC & @CRLF)
            EndSwitch
        EndIf
 
        ; When the colour changes back
        If $iColour = $iColour_Start Then
            Switch $iChange
                Case 3
                    ConsoleWrite("4th change at: " & @SEC & " <<<<< Stop timer" & @CRLF)
                    ConsoleWrite("Final time: " & Round(TimerDiff($iBegin) / 1000, 1) & " secs" & @CRLF)
                    ExitLoop
                Case 1
                    $iBegin = TimerInit()
                    $iChange = 2
                    ConsoleWrite("2nd change at: " & @SEC & " <<<<< Start timer" & @CRLF)
            EndSwitch
        EndIf
 
    WEnd
 
EndFunc

Run this script and it will automatically run the first. When you press the "Go" button, you start the timer regardless of the current colour of the Colouriser GUI.

All clear? :graduated:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Phaser

Thanks Melba, I'll see what i can do with it.

Share this post


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
Sign in to follow this  

×