Skip to content

Instantly share code, notes, and snippets.

@sgtsquiggs
Last active December 26, 2015 20:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgtsquiggs/7206385 to your computer and use it in GitHub Desktop.
Save sgtsquiggs/7206385 to your computer and use it in GitHub Desktop.
Equivalent color w/ alpha for UINavigationBar's barTintColor
@implementation UIColor (iOS7NavigationBar_Expanded)
// Returns equivelent color with alpha nearest to minimum alpha
- (UIColor *) equivalentNonOpaqueColorWhenInterpolatedWithBackgroundColor: (UIColor *)backgroundColor minimumAlpha: (CGFloat) alpha
{
NSAssert(self.canProvideRGBComponents, @"Self must be a RGB color to use arithmatic operations");
NSAssert(backgroundColor.canProvideRGBComponents, @"Self must be a RGB color to use arithmatic operations");
NSAssert(self.alpha == 1, @"Self must be an opaque RGB color");
NSAssert(backgroundColor.alpha == 1, @"Background color must be an opaque RGB color");
CGFloat r, g, b, a;
if (CGColorGetNumberOfComponents(self.CGColor) == 2)
{
CGFloat w;
if (![self getWhite:&w alpha:&a]) return nil;
r = g = b = w;
}
else if (![self getRed:&r green:&g blue:&b alpha:&a]) return nil;
CGFloat r2,g2,b2,a2;
if (CGColorGetNumberOfComponents(backgroundColor.CGColor) == 2)
{
CGFloat w2;
if (![backgroundColor getWhite:&w2 alpha:&a2]) return nil;
r2 = g2 = b2 = w2;
}
else if (![backgroundColor getRed:&r2 green:&g2 blue:&b2 alpha:&a2]) return nil;
CGFloat red,green,blue;
alpha -= 0.01;
do
{
alpha += 0.01;
red = (r - r2 + r2 * alpha) / alpha;
green = (g - g2 + g2 * alpha) / alpha;
blue = (b - b2 + b2 * alpha) / alpha;
}
while (alpha < 1 && (red < 0 || green < 0 || blue < 0 || red > 1 || green > 1 || blue > 1));
UIColor *new = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
return new;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment