How to iterate through list of colors

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
NinjoOnline
Posts: 5
Joined: 24 Apr 2024, 01:25
Contact:

How to iterate through list of colors

25 Apr 2024, 05:30

I have this chunk of code, when i press a button, it reads several on screen pixel colors. Atm, it's erroring on
colors := {Rare: Rare, Epic: Epic, Legendary: Legendary, Mythical: Mythical, Exotic: Exotic, Divine: Divine, Celestial: Celestial}
saying its enumerable? Unsure what to do

Code: Select all

; Define color thresholds in RGB format
Basic := [211, 211, 221]
Rare := [190, 254, 167]
Epic := [158, 240, 255]
Legendary := [255, 212, 163]
Mythical := [255, 170, 187]
Exotic := [255, 173, 254]
Divine := [254, 243, 171]
Celestial := [212, 244, 251]

; Function to calculate Euclidean distance between two colors
EuclideanDistance(color1, color2) {
    return Sqrt((color1[1] - color2[1]) ** 2 + (color1[2] - color2[2]) ** 2 + (color1[3] - color2[3]) ** 2)
}

; Function to convert hexadecimal color to RGB format
HexToRGB(hex_color) {
    blue := (hex_color >> 16) & 0xFF
    green := (hex_color >> 8) & 0xFF
    red := hex_color & 0xFF
    return [red, green, blue]
}

; Function to find the closest predefined color to a given color
FindClosestColor(color) {
    closest_color := "Basic"
    min_distance := EuclideanDistance(color, Basic)
    
    colors := {Rare: Rare, Epic: Epic, Legendary: Legendary, Mythical: Mythical, Exotic: Exotic, Divine: Divine, Celestial: Celestial}
    for color_name, color_value in colors
    {
        distance := EuclideanDistance(color, color_value)
        if (distance < min_distance) {
            closest_color := color_name
            min_distance := distance
        }
    }
    
    return closest_color
}

; Read reward value
Reward1 := FindClosestColor(HexToRGB(PixelGetColor(Item1X, ItemY)))
Reward2 := FindClosestColor(HexToRGB(PixelGetColor(Item2X, ItemY)))
Reward3 := FindClosestColor(HexToRGB(PixelGetColor(Item3X, ItemY)))

MsgBox(Reward1 . " " . Reward2 . " " . Reward3)

User avatar
boiler
Posts: 17127
Joined: 21 Dec 2014, 02:44

Re: How to iterate through list of colors

25 Apr 2024, 05:52

That’s not an object with key-value pairs. That’s an object with a bunch of properties defined. It looks like you are wanting to do the same as a v1 associative array, which in v2 is a map object:

Code: Select all

Basic := [211, 211, 221]
Rare := [190, 254, 167]
Epic := [158, 240, 255]
Legendary := [255, 212, 163]
Mythical := [255, 170, 187]
Exotic := [255, 173, 254]
Divine := [254, 243, 171]
Celestial := [212, 244, 251]

colors := Map('Basic', Basic, 'Rare', Rare, 'Epic', Epic, 'Legendary', Legendary, 'Mythical', Mythical, 'Exotic', Exotic, 'Divine', Divine, 'Celestial', Celestial)
for color_name, color_value in colors
	MsgBox color_name ': ' color_value[1] ', '  color_value[2] ', ' color_value[3]

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Kodakku and 33 guests