Using nearest function on a list of sample colors represented by jpg files

Hi - I am new to Wolfram programming and am trying to figure out a way to construct a list of colors supplied by a number of jpgs, and then have it match an unknown color to the nearest match in the list. I can do this, and it happily outputs the image that is closest, but I’d rather know the index number (5th one in the list, etc) or have each known color paired with a label that I could retrieve on the nearest match. Can anyone point me towards some hints on how to do this. My example below uses simple colors, but the colors used in actual use will not be anything standard, so i need to know that item[x] in the array is closest to the sampled color.

Thanks!
Scott

Eg:

Hi,

suppose you have a reference list of colours:

list = RandomColor[30]

ScreenShot2016-01-29at00.23.42.png

Then you have a random colour to identify:

inputcolor = RandomColor[]

ScreenShot2016-01-29at00.24.27.png

This gives you the index of the closest colour in the reference list:

indexnearst = Flatten[Position[list, Nearest[list, inputcolor][[1]]]][[1]]
(*24*)

If you wan to identify the RGB values of that reference colour you can use:

List @@ list[[indexnearst]]
(*{0.687419, 0.556168, 0.166387}*)

Cheers,

Marco

Marco - Thank you so much for the insight on how to do this! Wolfram is not like any of the other languages I have used, so its taking me a bit to wrap my head around it all!

I knew there had to be some way that I was missing!

Thanks!
S.

I’d recommend using just Nearest with a rule in the first argument:

indexnearst = Nearest[list → Range[Length[list]]];

indexnearst[inputcolor]

Dear Daniel,

yes, that’s much better. I did not like my “[[1]]” everywhere anyway. When that shows up in Wolfram Language Code something is usually much less elegant than it should be.

Yours is much cleaner!

Thanks,
Marco

Thank you both for the hints! I had some time today to play with this a bit more and its working quite well!

Cheers!
Scott