Skip to content

Instantly share code, notes, and snippets.

@jamesnesfield
Created February 4, 2014 16:38
Show Gist options
  • Save jamesnesfield/8807331 to your computer and use it in GitHub Desktop.
Save jamesnesfield/8807331 to your computer and use it in GitHub Desktop.
//http://stackoverflow.com/questions/15757872/manually-color-fading-from-one-uicolor-to-another
- (UIColor *)colorFromColor:(UIColor *)fromColor toColor:(UIColor *)toColor amount:(float)amount // 0 < amount < 1
{
float dec = amount;
CGFloat fRed, fBlue, fGreen, fAlpha;
CGFloat tRed, tBlue, tGreen, tAlpha;
CGFloat red, green, blue, alpha;
if(CGColorGetNumberOfComponents(fromColor.CGColor) == 2) {
[fromColor getWhite:&fRed alpha:&fAlpha];
fGreen = fRed;
fBlue = fRed;
}
else {
[fromColor getRed:&fRed green:&fGreen blue:&fBlue alpha:&fAlpha];
}
if(CGColorGetNumberOfComponents(toColor.CGColor) == 2) {
[toColor getWhite:&tRed alpha:&tAlpha];
tGreen = tRed;
tBlue = tRed;
}
else {
[toColor getRed:&tRed green:&tGreen blue:&tBlue alpha:&tAlpha];
}
red = (dec * (tRed - fRed)) + fRed;
green = (dec * (tGreen - fGreen)) + fGreen;
blue = (dec * (tBlue - fBlue)) + fBlue;
alpha = (dec * (tAlpha - fAlpha)) + fAlpha;
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment