priore

Fade from color to color

Apr 3rd, 2017
494
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (UIColor *)fadeFromColor:(UIColor *)fromColor toColor:(UIColor *)toColor withPercentage:(CGFloat)percentage
  2. {
  3.     // get the RGBA values from the colours
  4.     CGFloat fromRed, fromGreen, fromBlue, fromAlpha;
  5.     [fromColor getRed:&fromRed green:&fromGreen blue:&fromBlue alpha:&fromAlpha];
  6.    
  7.     CGFloat toRed, toGreen, toBlue, toAlpha;
  8.     [toColor getRed:&toRed green:&toGreen blue:&toBlue alpha:&toAlpha];
  9.    
  10.     //calculate the actual RGBA values of the fade colour
  11.     CGFloat red = (toRed - fromRed) * percentage + fromRed;
  12.     CGFloat green = (toGreen - fromGreen) * percentage + fromGreen;
  13.     CGFloat blue = (toBlue - fromBlue) * percentage + fromBlue;
  14.     CGFloat alpha = (toAlpha - fromAlpha) * percentage + fromAlpha;
  15.    
  16.     // return the fade colour
  17.     return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
  18. }
RAW Paste Data