Match colors in RasterLayer color space to the provided legend values
Colors provided by the legend do not always correspond exactly with the colors in the colortable of a RasterLayer object. They are usually pretty close, though, so this function finds the closest colors, maps them to the appropriate colors in the Raster* object, and applies that to the legend.
Raster colors in
x are mapped to those in
legend by converting
the RGB hexadecimal values to a 3D vector of values for red, green and blue.
The closest values are then assigned using 3D Pythagorean theorem to compute
the distance among all colors. The minimum distance in three dimensional
space is the color in
x that gets mapped to the appropriate color in