title: iOS Find Color at Point Between Two Colors
body: <p>I have a problem: I need to be able to take two colors and make a 'virtual gradient' out of them. I then need to be able to find the color at any point on this line. My current approach is this:</p>
  
  <pre><code>if (fahrenheit &lt; kBottomThreshold)
  {
      return [UIColor colorWithRed:kBottomR/255.0f green:kBottomG/255.0f blue:kBottomB/255.0f alpha:1];
  }
  if (fahrenheit &gt; kTopThreshold)
  {
      return [UIColor colorWithRed:kTopR/255.0f green:kTopG/255.0f blue:kTopB/255.0f alpha:1];
  }
  
  double rDiff = kTopR - kBottomR;
  double gDiff = kTopG - kBottomG;
  double bDiff = kTopB - kBottomB;
  
  double tempDiff = kTopThreshold - kBottomThreshold;
  
  double rValue;
  double gValue;
  double bValue;
  
  rValue = kBottomR + ((rDiff/tempDiff) * fahrenheit);
  gValue = kBottomG + ((gDiff/tempDiff) * fahrenheit);
  bValue = kBottomB + ((bDiff/tempDiff) * fahrenheit);
  
  return [UIColor colorWithRed:rValue/255.0f green:gValue/255.0f blue:bValue/255.0f alpha:1];
  </code></pre>
  
  <p>Variables:</p>
  
  <ul>
  <li><code>fahrenheit</code> is a variable passed into my function that is the number on this virtual line that I want to find the color for.</li>
  <li><code>kTopR</code>, <code>kTopB</code>, and <code>kTopG</code> are the RGB values for one end of the gradient. Same for their <code>kBottom</code> counterparts.</li>
  <li><code>kBottomThreshold</code> and <code>kTopThreshold</code> are the endpoints of my gradient.</li>
  </ul>
  
  <p><strong>Here's my problem:</strong> <em><strong>When <code>fahrenheit</code> goes over either end of the gradient, the gradient seems to 'jump' to a different value.</em></strong></p>
  
  <p>I've included an example project, hosted on my S3 server, <a href="https://s3.amazonaws.com/erwaysoftwarestackoverflow/StackOverflowExample.xcarchive.zip" rel="noreferrer">here</a>.</p>
  
  <p><em><strong>You really need to download the project and try it on the simulator/device to see what I mean</em></strong> (unless you are crazy smart and can tell just by looking at the code)</p>