title: Manually color fading from one UIColor to another
body: <p>I'm trying to fade one <code>UIColor</code> to another in a <code>drawRect</code>. I've created this function to calculate a color at a certain percentage:</p>
  
  <pre><code>- (UIColor *)colorFromColor:(UIColor *)fromColor toColor:(UIColor *)toColor percent:(float)percent
  {
      float dec = percent / 100.f;
      CGFloat fRed, fBlue, fGreen, fAlpha;
      CGFloat tRed, tBlue, tGreen, tAlpha;
      CGFloat red, green, blue, alpha;
  
      if(CGColorGetNumberOfComponents(fromColor.CGColor) == 2) {
          [fromColor getWhite:&amp;fRed alpha:&amp;fAlpha];
          fGreen = fRed;
          fBlue = fRed;
      }
      else {
          [fromColor getRed:&amp;fRed green:&amp;fGreen blue:&amp;fBlue alpha:&amp;fAlpha];
      }
      if(CGColorGetNumberOfComponents(toColor.CGColor) == 2) {
          [toColor getWhite:&amp;tRed alpha:&amp;tAlpha];
          tGreen = tRed;
          tBlue = tRed;
      }
      else {
          [toColor getRed:&amp;tRed green:&amp;tGreen blue:&amp;tBlue alpha:&amp;tAlpha];
      }
  
      red = (dec * (tRed - fRed)) + fRed;
      blue = (dec * (tGreen - fGreen)) + fGreen;
      green = (dec * (tBlue - fBlue)) + fBlue;
      alpha = (dec * (tAlpha - fAlpha)) + fAlpha;
  
      return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
  }
  </code></pre>
  
  <p>What this does is takes each R/G/B/A value and increments it depending on the percentage.</p>
  
  <p>It kind of works, but doesn't fade <code>[UIColor purpleColor]</code> to <code>[UIColor redColor]</code> correctly. At 0.1 percent it shows up as a green color rather than purple, so it appears to fade from green to red instead of purple to red.</p>
  
  <p>Anyone know what I'm doing wrong? Is this the wrong approach to calculating this? Is there a more accurate way to do this?</p>