Convert a colormap image into data values according to a legend

I have this image

that I want to turn into data using this gradient

black being 0, white being 1, and all the colors in between accordingly, so I could later use ListPlot3D to make a height map.

I tried using ImageData, to work something out using RGB values of each color point, but in the end the purple was higher than the blue, so that didn't workout.

Import data, take only one column from the legend, and extract ImageData from the legend:

img = Import["https://i.stack.imgur.com/1JxTr.png"];
leg = ImageTake[Import["https://i.stack.imgur.com/MRqiH.png"], All, {1, 1}];
legdata = ImageData[leg, DataReversed -> True][[All, 1, 1 ;; 3]];


Connect the RGB values to a number in $[0,1]$:

rules = Rule @@@ Transpose[{legdata, Subdivide[Length@legdata - 1]}];


Extract ImageData info from the image:

imgpoints = ImageData[img][[All, All, 1 ;; 3]];


Employ Nearest to find the best approximation of the color in img using colors available from leg:

pts = Table[
Nearest[legdata, imgpoints[[i, j]]][[1]], {i, 1,
Length@imgpoints}, {j, 1, Length@imgpoints[[i]]}] /. rules;


Draw points:

ListPlot3D[pts, ColorFunction -> "Rainbow", BoxRatios -> {1, 1, 1}]