title: Calculate RGB value for a range of values to create heat map
body: <p>I am trying to create a heat map with python. For this I have to assign an RGB value to every value in the range of possible values. I thought of changing the color from blue (minimal value) over green to red (maximal value). </p>
  
  <p>The picture example below explains how I thought of the color composition: We have a range from 1 (pure blue) to 3 (pure red), 2 is in between resembled by green.</p>
  
  <p><img src="https://i.stack.imgur.com/npksO.png" alt="color composition RGB in range(1-3)"></p>
  
  <p>I read about linear interpolation and wrote a function that (more or less) handles the calculation for a certain value in the range between a minimum and a maximum and returns an RGB tuple. It uses <code>if</code> and <code>elif</code> conditions (which does not make me completely happy):</p>
  
  <pre><code>def convert_to_rgb(minimum, maximum, value):
      minimum, maximum = float(minimum), float(maximum)    
      halfmax = (minimum + maximum) / 2
      if minimum &lt;= value &lt;= halfmax:
          r = 0
          g = int( 255./(halfmax - minimum) * (value - minimum))
          b = int( 255. + -255./(halfmax - minimum)  * (value - minimum))
          return (r,g,b)    
      elif halfmax &lt; value &lt;= maximum:
          r = int( 255./(maximum - halfmax) * (value - halfmax))
          g = int( 255. + -255./(maximum - halfmax)  * (value - halfmax))
          b = 0
          return (r,g,b)
  </code></pre>
  
  <p>However I wonder if one could write a function for each color value <strong>without</strong> using <code>if</code> conditions. Does anybody have an idea? Thank you a lot!</p>