Storing distinct colors during a click and drag

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Off Topic
Posts: 43
Joined: 07 Oct 2017, 20:57

Storing distinct colors during a click and drag

23 Apr 2018, 21:27

Hey guys, I'd like to be able to easily pick up colors from vector palettes like this one by dragging over them while holding a mouse button:
ScanTest.png
ScanTest.png (543 Bytes) Viewed 860 times
I thought this would be easy but I'm having trouble with it, and keep getting the same color spammed despite a few different ways of trying. My current code:

Code: Select all

#NoEnv
#SingleInstance, Force
SetWorkingDir %A_ScriptDir%

SetTimer, mouseTrack, 20

mouseTrack:
MouseGetPos, vCurX, vCurY
Return

!#RButton::
	colorScan := "Start"
	errCount = 0
	newColor1 := "", newColor2 := "", newColor3 := ""
	originX := vCurX, originY := vCurY
	SetTimer, WhileHolding, 20
	PixelGetColor, originColor, originX, originY, RGB
Return

WhileHolding:
PixelGetColor, newColor, vCurX, vCurY, RGB
While (newColor != originColor) {
	If (colorScan = "Done")
		Break
	++errCount
	If (errCount > 5) {
		errCount = 0
		If ((newColor2 != "") && (newColor3 != newColor2)) {
			newColor3 := newColor
			colorScan := "Done"	
		} Else If (!(newColor2 != "") && (newColor2 != newColor1)) {
			newColor2 := newColor
		} Else {
			newColor1 := newColor
		}
	}
}
ToolTip % errCount "`r`n" originColor "`r`n" newColor "`r`n" "1:" newColor1 "`r`n" "2:" newColor2 "`r`n" "3:" newColor3 "`r`n" colorScan
Return

!#RButton Up::
	SetTimer, WhileHolding, Off
	ToolTip
Return
If you run this, you'll see that it fills up each 3 slots with the first color it encounters but I'm confused by this because those conditions shouldn't be met if that color is equal to the previous. The errCount variable is to try and pick up distinct colors and avoid anti-aliasing of borders though I think it might be complicating things, is there anything obviously wrong with the code here?

I'm trying to store up to 3 color values when I click-drag over them excluding the background color, can this be better done?
User avatar
Off Topic
Posts: 43
Joined: 07 Oct 2017, 20:57

Re: Storing distinct colors during a click and drag

23 Apr 2018, 21:44

Whoops, I realized I was comparing the wrong variables:

Code: Select all

WhileHolding:
dragXstart := (vCurX - originX)
dragYstart := (vCurY - originY)
PixelGetColor, newColor, vCurX, vCurY, RGB
If (newColor != originColor) {
	If (colorScan = "Done")
		Return
	++errCount
	If (errCount > 5) {
		errCount = 0
		If ((newColor2 != "") && (newColor != newColor2)) {     ;   This should be newColor from PixelGetColor
			newColor3 := newColor
			colorScan := "Done"	
		} Else If ((newColor1 != "") && (newColor != newColor1)) {      ;   This should be newColor from PixelGetColor
			newColor2 := newColor
		} Else {
			newColor1 := newColor
		}
	}
}
And I changed it to an If statement instead of a While loop, and now it works as I expected -- but still, are there better ways of doing this?

Image

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, Oblomov228, PsysimSV, uchihito and 163 guests