这次的版本迭代中提一个要获取主页图片的主色调动态的去改变子页面的背景颜色 刚一听觉得太简单了 就磨刀霍霍的去开始写 但是发现网上很多代码回去图片主色调的方法 都有一个通病就是 获取图片主色调 特别耗时 导致进入子页面许久才能显示到背景颜色 查了很多资料 加入了一个算法 大大减少了耗时 也借鉴了其他网友的一些代码
static void RGBtoHSV( float r, float g, float b, float *h, float *s, float *v )
{
float min, max, delta;
min = MIN( r, MIN( g, b ));
max = MAX( r, MAX( g, b ));
*v = max; // v
delta = max - min;
if( max != 0 )
*s = delta / max; // s
else {
// r = g = b = 0 // s = 0, v is undefined
*s = 0;
*h = -1;
return;
}
if( r == max )
*h = ( g - b ) / delta; // between yellow & magenta
else if( g == max )
*h = 2 + ( b - r ) / delta; // between cyan & yellow
else
*h = 4 + ( r - g ) / delta; // between magenta & cyan
*h *= 60; // degrees
if( *h < 0 )
*h += 360;
}
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 点击按钮更改不同的背景颜色
for (int i = 0; i<5; i++) {
UIButton *btn = [UIButton new];
[btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%zd",i+1]] forState:UIControlStateNormal];
btn.frame = CGRectMake(200, 100*(i+1), 80, 80);
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
}
- (void)btnClick:(UIButton *)sender{
UIColor *loginC = [self mostColor:sender.currentBackgroundImage];
self.view.backgroundColor = loginC;
}
-(UIColor*)mostColor:(UIImage*)image{
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
int bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
#else
int bitmapInfo = kCGImageAlphaPremultipliedLast;
#endif
//第一步 先把图片缩小 加快计算速度. 但越小结果误差可能越大
CGSize thumbSize=CGSizeMake(40, 40);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL,
thumbSize.width,
thumbSize.height,
8,//bits per component
thumbSize.width*4,
colorSpace,
bitmapInfo);
CGRect drawRect = CGRectMake(0, 0, thumbSize.width, thumbSize.height);
CGContextDrawImage(context, drawRect, image.CGImage);
CGColorSpaceRelease(colorSpace);
//第二步 取每个点的像素值
unsigned char* data = CGBitmapContextGetData (context);
if (data == NULL) return nil;
NSArray *MaxColor=nil;
// NSCountedSet *cls=[NSCountedSet setWithCapacity:thumbSize.width*thumbSize.height];
float maxScore=0;
for (int x=0; x<thumbSize.width*thumbSize.height; x++) {
int offset = 4*x;
int red = data[offset];
int green = data[offset+1];
int blue = data[offset+2];
int alpha = data[offset+3];
if (alpha<25)continue;
float h,s,v;
RGBtoHSV(red, green, blue, &h, &s, &v);
float y = MIN(abs(red*2104+green*4130+blue*802+4096+131072)>>13, 235);
y= (y-16)/(235-16);
if (y>0.9) continue;
float score = (s+0.1)*x;
if (score>maxScore) {
maxScore = score;
}
MaxColor=@[@(red),@(green),@(blue),@(alpha)];
}
CGContextRelease(context);
return [UIColor colorWithRed:([MaxColor[0] intValue]/255.0f) green:([MaxColor[1] intValue]/255.0f) blue:([MaxColor[2] intValue]/255.0f) alpha:([MaxColor[3] intValue]/255.0f)];
}
@end
GitHub https://github.com/liuniuliuniu/LLInterestDemo/tree/master/LLGetPhotoColor
效果图