How do I make border color depend on counter color

Goal: to derive border colour colour of the counter.

Relevant reading:

Consider counters of two sides in a game. You wish to automate process of adding border but you want each side to have border of different colour.

With ImageMagick it is possible to read colour value from specific pixel, modify it and use it as border colour. This method, however, rarely yields eye-pleasing results (i.e. it is not easy to come up with such a colour modification that would, first, give appealing outcome, two, give appealing outcome for all counter types); for a better effect it's advised you use Windows script to set border colour based on a pixel value read from input image.

We will use these two files as input:

We want grey counter to have black border and dark red for the other.

We will use this script:

FOR %%G IN (input\*.png) DO (

        FOR /F "tokens=* USEBACKQ" %%F IN (`magick %%G ^
-crop 1x1+1+1 -format "%%[fx:int(255*r+.5)],%%[fx:int(255*g+.5)],%%[fx:int(255*b+.5)]" info:`) DO (
                SET pcolor=%%F

        IF "!pcolor!"=="161,162,172" SET bcolor=#000000
        IF "!pcolor!"=="181,161,143" SET bcolor=#B14246
        magick %%G -bordercolor !bcolor! -border 4 output\%%~nGOutput.png