title: How to decode color mapping in matplotlib's Colormap?
body: <p>I know how to map a number to a color from this post: <a href="https://stackoverflow.com/questions/28752727/map-values-to-colors-in-matplotlib">Map values to colors in matplotlib</a></p>
  
  <p>But I don't know how to decode the mapping to get my original color, assuming this is a one-to-one mapping, which it must be I figure. </p>
  
  <p>I am encoding an image for visualization purposes, but I need to be able to decode it and read the original data values.</p>
  
  <p>For reference, here are the Colormap docs: <a href="http://matplotlib.org/api/cm_api.html" rel="noreferrer">http://matplotlib.org/api/cm_api.html</a></p>
  
  <p>Here's my try to the main answer below, which still isn't working right.</p>
  
  <pre><code>from PIL import Image
  import numpy as np
  import matplotlib
  import matplotlib.cm as cm
  values = [670, 894, 582, 103, 786, 348, 972, 718, 356, 692]
  minima = 103
  maxima = 972
  norm = matplotlib.colors.Normalize(vmin=minima, vmax=maxima, clip=True)
  mapper = cm.ScalarMappable(norm=norm, cmap=cm.gist_rainbow_r)
  c = []
  for i in range(10):
      c.append(mapper.to_rgba(values[i], bytes=True))
  print(c) # [(75, 255, 0, 255), (255, 77, 0, 255), (0, 255, 64, 255), (255, 0, 191, 255), (255, 250, 0, 255), (0, 72, 255, 255), (255, 0, 40, 255), (151, 255, 0, 255), (0, 83, 255, 255), (108, 255, 0, 255)]
  
  def get_value_from_cm(color, cmap, colrange):
      # color = matplotlib.colors.to_rgba(color)
      r = np.linspace(colrange[0], colrange[1], 10) # there are 10 values
      norm = matplotlib.colors.Normalize(colrange[0], colrange[1])
      mapvals = cmap(norm(r))[:, :4] # there are 4 channels: r,g,b,a
      distance = np.sum((mapvals - color) ** 2, axis=1)
      return r[np.argmin(distance)]
  
  decoded_colors = []
  for i in range(10):
      decoded_colors.append(get_value_from_cm(c[i], cm.gist_rainbow_r, colrange=[minima, maxima]))
  print(decoded_colors) # [778.88888888888891, 778.88888888888891, 489.22222222222223, 103.0, 778.88888888888891, 392.66666666666669, 103.0, 778.88888888888891, 392.66666666666669, 778.88888888888891]
  </code></pre>