2015-03-05 1 views
1

Je suis intéressant, quel est l'algorithme pour obtenir le pourcentage de la couleur de l'image d'arrière-plan. Si l'image d'arrière-plan est noire, la barre d'état de la pomme est blanche et si l'image d'arrière-plan est blanche, elle devient noire. Si vous avez un exemple comment le faire dans l'application, aidez-moi s'il vous plaît.Style de barre d'état à partir de la couleur de l'image d'arrière-plan pour cent iOS

Je téléchargerai des échantillons (le fond est l'image, pas la couleur). enter image description here

enter image description here

Répondre

1

Que diriez-vous d'obtenir la couleur moyenne, puis en appliquant l'inverse?

Trouver la couleur moyenne (de Bobby Georgescu's blog)
+ Trouver la couleur inverse (de a StackOverflow answer)


/* 
UIImage+AverageColor.m 

Copyright (c) 2010, Mircea "Bobby" Georgescu 
All rights reserved. 

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met: 
* Redistributions of source code must retain the above copyright 
notice, this list of conditions and the following disclaimer. 
* Redistributions in binary form must reproduce the above copyright 
notice, this list of conditions and the following disclaimer in the 
documentation and/or other materials provided with the distribution. 
* Neither the name of the Mircea "Bobby" Georgescu nor the 
names of its contributors may be used to endorse or promote products 
derived from this software without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL Mircea "Bobby" Georgescu BE LIABLE FOR ANY 
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
*/ 

#import "UIImage+AverageColor.h" 

@implementation UIImage (AverageColor) 

- (UIColor *)averageColor { 

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    unsigned char rgba[4]; 
    CGContextRef context = CGBitmapContextCreate(rgba, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 

    CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), self.CGImage); 
    CGColorSpaceRelease(colorSpace); 
    CGContextRelease(context); 

    if(rgba[3] > 0) { 
     CGFloat alpha = ((CGFloat)rgba[3])/255.0; 
     CGFloat multiplier = alpha/255.0; 
     return [UIColor colorWithRed:((CGFloat)rgba[0])*multiplier 
           green:((CGFloat)rgba[1])*multiplier 
           blue:((CGFloat)rgba[2])*multiplier 
           alpha:alpha]; 
    } 
    else { 
     return [UIColor colorWithRed:((CGFloat)rgba[0])/255.0 
           green:((CGFloat)rgba[1])/255.0 
           blue:((CGFloat)rgba[2])/255.0 
           alpha:((CGFloat)rgba[3])/255.0]; 
    } 
} 

-(UIColor*) invertColor:(UIColor *)color 
{ 
    CGFloat r,g,b,a; 
    [color getRed:&r green:&g blue:&b alpha:&a]; 
    return [UIColor colorWithRed:1.-r green:1.-g blue:1.-b alpha:a]; 
} 

- (UIColor *)statusBarColor { 
    return [self invertColor:[self averageColor]]; 
} 
@end 
+0

grand, je vous remercie, je l'utilise un peu autre code pour obtenir la moyenne couleur, et ça marche) – LightNight