美文网首页
根据背景颜色的深浅确定字的颜色

根据背景颜色的深浅确定字的颜色

作者: 海笙樾 | 来源:发表于2018-04-25 16:09 被阅读0次

    一般在开发的时候会遇到有动态切换背景图片或者网络请求背景图片的情况,这时候需要根据背景的深浅来设置当前字体的颜色,下面来实现只根据背景颜色的深浅来设置字体颜色是白色还是黑色

    1. 缩小图片,提高精度

    CGSize thumbSize=CGSizeMake(50, 50);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL,thumbSize.width,thumbSize.height,8,thumbSize.width*4,colorSpace, bitmapInfo);
    CGRect drawRect = CGRectMake(0, 0, thumbSize.width, thumbSize.height);
    CGContextDrawImage(context, drawRect, image.CGImage);
    CGColorSpaceRelease(colorSpace);````
    ##2.取点的像素值
    ````unsigned char* data = CGBitmapContextGetData (context);
    if (data == NULL) return nil;
    NSCountedSet *cls=[NSCountedSet setWithCapacity:thumbSize.width*thumbSize.height];
    for (int x=0; x<thumbSize.width; x++) {
            for (int y=0; y<thumbSize.height; y++) {
                int offset = 4*(x*y);
                int red = data[offset];
                int green = data[offset+1];
                int blue = data[offset+2];
                int alpha = data[offset+3];
                NSArray *clr=@[@(red),@(green),@(blue),@(alpha)];
                [cls addObject:clr];
            }
        }
    CGContextRelease(context);````
    ## 3.遍历得到大部分的颜色
    ````NSEnumerator *enumerator = [cls objectEnumerator];
        NSArray *curColor = nil;
        NSArray *MaxColor=nil;
        NSUInteger MaxCount=0;
        while ( (curColor = [enumerator nextObject]) != nil )
        {
            NSUInteger tmpCount = [cls countForObject:curColor];
            if ( tmpCount < MaxCount ) continue;
            MaxCount=tmpCount;
            MaxColor=curColor;
        }````
    ## 4.得到背景的主体颜色
    ````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)];````
    ### 代码
    ````-(UIColor*)mainColorOfImage:(UIImage *)image{
        int bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
        //第一步 先把图片缩小 加快计算速度. 但越小结果误差可能越大
        CGSize thumbSize=CGSizeMake(50, 50);
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef context = CGBitmapContextCreate(NULL,
                                                     thumbSize.width,
                                                     thumbSize.height,
                                                     8,
                                                     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;
        NSCountedSet *cls=[NSCountedSet setWithCapacity:thumbSize.width*thumbSize.height];
        for (int x=0; x<thumbSize.width; x++) {
            for (int y=0; y<thumbSize.height; y++) {
                int offset = 4*(x*y);
                int red = data[offset];
                int green = data[offset+1];
                int blue = data[offset+2];
                int alpha = data[offset+3];
                NSArray *clr=@[@(red),@(green),@(blue),@(alpha)];
                [cls addObject:clr];
            }
        }
        CGContextRelease(context);
        //第三步 找到出现次数最多的那个颜色
        NSEnumerator *enumerator = [cls objectEnumerator];
        NSArray *curColor = nil;
        NSArray *MaxColor=nil;
        NSUInteger MaxCount=0;
        while ( (curColor = [enumerator nextObject]) != nil )
        {
            NSUInteger tmpCount = [cls countForObject:curColor];
            if ( tmpCount < MaxCount ) continue;
            MaxCount=tmpCount;
            MaxColor=curColor;
        }
        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)];
    }
    

    5.获取RGP值

        int bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
        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];
        }}````
    
    ## 6 .判断颜色的深浅
    
    ````CGFloat components[3];
        [self getRGBComponents:components forColor:color];
        CGFloat num = components[0] + components[1] + components[2];
        if(num < 382)
            return NO;
        else
            return YES;````
    

    相关文章

      网友评论

          本文标题:根据背景颜色的深浅确定字的颜色

          本文链接:https://www.haomeiwen.com/subject/xirylftx.html