the commented bits pertain to your question. move the mouse over some colors for the duration of the loop, after running the script:
Code: Select all
#NoEnv
#SingleInstance Force
SetBatchLines -1
CoordMode Mouse
CoordMode Pixel
Colors := {}
Loop % i := 200
{
Sleep 25
MouseGetPos x, y
PixelGetColor color, x, y, RGB
ToolTip % Format("PixelGetColor @ ({},{}) - #{}/{}", x, y, A_Index, i), A_ScreenWidth // 2, A_ScreenHeight // 2
if (Colors.HasKey(color "")) ; if the color already exists as a key in the object
Colors[color ""]++ ; increment the existing key's value
else
Colors[color ""] := 1 ; otherwise, create the non-yet-existing key now, and give it a count value of 1
}
ToolTip
for color, count in Colors
{
result .= Format("{}:`t{}`n", color, count)
if (A_Index == 1) ; is it the 1st loop iteration?
{
mostCommon := {"color": color "", "count": count} ; there's nothing to compare to, so the first color becomes the 'most common' automaticallu by default
continue ; skip the comparison that follows, and start a new iteration of the loop
}
if (mostCommon.count < count) ; the most common color has fewer occurrences than the current color being examined
mostCommon := {"color": color "", "count": count} ; the current color, therefore, replaces and becomes the new most common color
}
MsgBox % result
MsgBox % Format("
(LTrim
Most Common Color
Color: {}
Count: {}
)", mostCommon.color, mostCommon.count)