Anyone know if there's a way to set a colour input (for a material) to match the colour of the closest material/mesh?

For example, if I had a cube that I wanted to dynamically adopt the colour of the nearest surface - so if it touched a green surface it would be green, if it touched a red surface it would be red.

Thanks :)