- (
void
)colorAction:(
id
)sender{
bgimageV.image =
nil
;
UIColor *color =
nil
;
int
colorNum = arc4random()%2;
if
(colorNum == 0)
color = [UIColor blackColor];
else
if
(colorNum == 1)
color = [UIColor whiteColor];
[bgimageV setBackgroundColor:color];
[
self
setTextColor];
}
- (
void
)setTextColor{
UIColor *color =
nil
;
if
(bgimageV.image !=
nil
)
color = [bgimageV.image mostColor];
else
color = bgimageV.backgroundColor;
if
([
self
isLightColor:color])
[textLabel setTextColor:[UIColor blackColor]];
else
[textLabel setTextColor:[UIColor whiteColor]];
}
-(
BOOL
) isLightColor:(UIColor*)clr {
CGFloat components[3];
[
self
getRGBComponents:components forColor:clr];
NSLog
(@
"%f %f %f"
, components[0], components[1], components[2]);
CGFloat num = components[0] + components[1] + components[2];
if
(num < 382)
return
NO
;
else
return
YES
;
}
- (
void
)getRGBComponents:(CGFloat [3])components forColor:(UIColor *)color {
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
int
bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
#else
int
bitmapInfo = kCGImageAlphaPremultipliedLast;
#endif
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
unsigned
char
resultingPixel[4];
CGContextRef context = CGBitmapContextCreate(&resultingPixel,
1,
1,
8,
4,
rgbColorSpace,
bitmapInfo);
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, CGRectMake(0, 0, 1, 1));
CGContextRelease(context);
CGColorSpaceRelease(rgbColorSpace);
for
(
int
component = 0; component < 3; component++) {
components[component] = resultingPixel[component];
}
}