Thanks for answers.
I've worked out a solution that works for me. I've using three independent arrays.
Each hit (for a color) - the position (in all three arrays) move one place forward in order to reduce search time for the most recently used colors.
Maybe it's possible to make the code more efficient, but I haven't got an idea on how yet. Maybe you have a better idea.
Code: Select all
; Select color (MS paint in Windows XP)
msPaintSetColorDecimal(r,g,b, colorSameValueHEX:=0x000000) {
Static r_last, g_last, b_last
Static defaultColorsArray, colorPickPosition_X, colorPickPosition_Y, indexOfMatchingColor
; Run only first time this function is called.
If (!defaultColorsArray.MaxIndex()) {
defaultColorsArray := [0x000000,0x400000,0x800000,0x804040,0xFF0000,0xFF8080, 0x808000,0x804000,0xFF8000,0xFF8040,0xFFFF00,0xFFFF80 ,0x808040,0x004000,0x008000,0x00FF00,0x80FF00,0x80FF80 ,0x808080,0x004040,0x008040,0x008080,0x00FF40,0x00FF80 ,0x408080,0x000080,0x0000FF,0x004080,0x00FFFF,0x80FFFF ,0xC0C0C0,0x000040,0x0000A0,0x8080FF,0x0080C0,0x0080FF ,0x400040,0x400040,0x800080,0x800040,0x8080C0,0xFF80C0 ,0xFFFFFF,0x400080,0x8000FF,0xFF0080,0xFF00FF,0xFF80FF]
colorPickPosition_X := [22,22,22,22,22,22,48,48,48,48,48,48,71,71,71,71,71,71,96,96,96,96,96,96,121,121,121,121,121,121,147,147,147,147,147,147,172,172,172,172,172,172,197,197,197,197,197,197]
colorPickPosition_Y := [172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64]
indexOfMatchingColor := 0
}
If (r=r_last && g=g_last && b=b_last) ; Color value is the same since last function call
Return
IfWinNotActive, ahk_class MSPaintApp
{
MsgBox, This function requires that MS Paint has focus.
Return
}
MouseGetPos, musX, musY
Click 157,39 ; Open menu "Colors"
Sleep 5
;SendInput {Down}{Enter} ; This doesn't always work in english version of Ms Paint (XP), but always work in norwegian version for some odd reason.
Click 178,62 ; Click on sub menu "Edit Colors"
WinWaitActive, Edit Colors
; Call the function that takes care of the three arrays. That is looking for default colors (less clicks).
If colorIsDefault(colorSameValueHEX, defaultColorsArray, colorPickPosition_X, colorPickPosition_Y, indexOfMatchingColor)
{
TrayTip, Info, Value of default color found is %colorSameValueHEX%
MouseMove, colorPickPosition_X[indexOfMatchingColor], colorPickPosition_Y[indexOfMatchingColor]
Sleep 4
Click
}
Else
{
; Have to define the color, because it's not in the list of default colors (MS Paint color palette).
Click, 82,279 ; Click button "Define Custom Colors >>"
Sleep, 10
If (r != r_last) {
SendInput {Alt down}r{Alt up} ; Select input field "Red"
Sleep 2
SendInput %r%
Sleep 2
}
If (g != g_last) {
SendInput {Alt down}g{Alt up} ; Select input field "Green"
Sleep 2
SendInput %g%
Sleep 2
}
If (b != b_last) {
SendInput {Alt down}u{Alt up} ; Select input field "Blue"
Sleep 2
SendInput %b%
Sleep 2
}
}
Sleep 10
Click 45,304 ; Button "Ok" to confirm the color
r_last := r
g_last := g
b_last := b
}
; Dealing with array of default colors and the colors X and Y position (in "Edit Colors" dialog box).
; Possible return values. 1=color value is in present array, 0=not found.
colorIsDefault(colorSameValueHEX, ByRef defaultColorsArray, ByRef colorPickPosition_X, ByRef colorPickPosition_Y, ByRef indexOfMatchingColor) {
Loop, 48
{
If (colorSameValueHEX = defaultColorsArray[A_Index]) {
If (A_Index < 3) {
indexOfMatchingColor := A_Index
Return 1
}
n_minus := A_Index -1
tmp_standardFarger := defaultColorsArray[n_minus]
tmp_x := colorPickPosition_X[n_minus]
tmp_y := colorPickPosition_Y[n_minus]
defaultColorsArray[n_minus] := defaultColorsArray[A_Index]
colorPickPosition_X[n_minus] := colorPickPosition_X[A_Index]
colorPickPosition_Y[n_minus] := colorPickPosition_Y[A_Index]
defaultColorsArray[A_Index] := tmp_standardFarger
colorPickPosition_X[A_Index] := tmp_x
colorPickPosition_Y[A_Index] := tmp_y
indexOfMatchingColor := n_minus
Return 1
}
}
Return 0
}