Setting RGB value minus "0x" as array index

Get help with using AutoHotkey and its commands and hotkeys
kairushinjuu
Posts: 35
Joined: 07 May 2020, 07:02

Setting RGB value minus "0x" as array index

Post by kairushinjuu » 12 Jan 2021, 02:05

Hello everybody, hope we are keeping well.



I have run into a problem that with my current level and understanding I cannot seem to break. I am trying to build a reference of all the colours my mouse pointer captures below it and then logs how many times they exist.



I am trying to get it to come out as an associative array where the colour will be the index and the value will be the number of occurrences.

Problem is when it's any hex number that has a format similar to 0x01212, 0x002231, 0x000123 it will only be logged into the index as 1212, 2231, 123.

I have tried everything I understand, but the result is always the same.


Code: Select all

i :=1
OccNum := 1
target := 25
colourCnt := {}



clipboard  :=



while (  i != target +1 )  { ; START

    

    MouseGetPos, TempX1, TempY1

    MouseGetPos, TempX2, TempY2

    Dis1 := TempX2 -TempX1

    TranDis1 := StrReplace(Dis1, "-" , "")

    Dis2 := TempY2 -TempY1

    TranDis2 := StrReplace(Dis2, "-" , "")

    if  ( TranDis1 > 0) || ( TranDis2 > 0 ) 

        {

    MouseGetPos, CordX, CordY

    PixelGetColor, Colour, CordX, CordY, RGB

    Colour := StrReplace(Colour, "0x" , "")

    if (colourCnt.HasKey(Colour)) {

    colourCnt[Colour] := "Occurrences :  " OccNum++

    } else  {

    colourCnt[Colour] := "Occurrences :  "  1

    }

    i++

} 

} ; END

    

     NewColourCount := {}

    

    for k, v in colourCnt     ; Thought this would fix index formatting

    {

        if ( StrLen(k) = 3 ) {

            k := "000" . k

       } else if  ( StrLen(k) = 4 ) {

                k := "00" . k

          } else if  ( StrLen(k) = 4 ) {

                    k := "0" . k

            } else {

                k := k

                }

        NewColourCount[k] := v

        OrigData .= "Colour : " k " " v "`n"  ; Only partially fixed, Index name still unchanged

    }

    

   

For k, v in NewColourCount ;Index names still come out as 1212, 2231, 123 etc

    Msgbox  % "Key : " k "`nValue : " v 



    

        clipboard  := OrigData

        Msgbox  % "Finished"

      



Return



Heres what the code now looks like after a few revisions.. The data contained in OrigData, shows the formatting correctly when pasted into notepad etc i.e 01212, 002231, 000123 will be outputted.



Any suggestions and or explanations would be immensely appreciated.



Thank you.

User avatar
Hellbent
Posts: 1194
Joined: 23 Sep 2017, 13:34

Re: Setting RGB value minus "0x" as array index

Post by Hellbent » 12 Jan 2021, 04:33

Hope this helps.

Code: Select all

#SingleInstance,Force

ColorsObject := {}

Gui,1:+AlwaysOnTop -Caption +Border
Gui,1:Font,cLime
Gui,1:Color,000000
Gui,1:Margin,0,0
Gui,1:Add,Text,xm ym w50 h50 center 0x201 hwndTextHwnd, 0
Gui,1:Show,x100 y100 w50 h50

return
GuiContextMenu:
*ESC::
	ExitApp

Numpad1::
	CoordMode, Mouse, Screen
	CoordMode, Pixel, Screen
	GuiControl,1:,% texthwnd,
	While(!GetKeyState("Ctrl")){
		ToolTip, Press "Ctrl" To Capture
		MouseGetPos, x, y
		PixelGetColor, Color, x, y, RGB
		Gui,1:Color,% Color
	}
	ToolTip, 
	StringTrimLeft, NewColor, Color, 2
	if(!ColorsObject.HasKey("" NewColor))
		ColorsObject["" NewColor] := 0
	ColorsObject["" NewColor]++
	GuiControl,1:,% texthwnd,% ColorsObject["" NewColor]
	return

Numpad2::
	output := ""
	for k, v in ColorsObject
		output .= k " " v "`n"
	msgbox, % output "`nTotal Colors: " ColorsObject.Count()
	return
	

Post Reply

Return to “Ask For Help”