UIColor比较 [英] UIColor comparison

查看:76
本文介绍了UIColor比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于UIColor,我需要确定它是亮还是暗。如果我可以访问颜色的十六进制值,我可以检查它是否大于或小于某个阈值十六进制数,但似乎没有办法做到这一点。在那儿?或者有其他方法可以检查UIColor的亮度值吗?

Given a UIColor, I need to determine if it is "light" or "dark". If I could access the hex value of the color, I could just check if it was greater than or less than a certain threshold hex number, but there appears to be no way to do that. Is there? Or is there another way I could check the brightness value of a UIColor?

推荐答案

你可以安装此类别用于扩展 UIColor 以了解HSV / HSB并进行比较 [aUIColor brightness]

You could install this Category for extending UIColor for knowing HSV/HSB and compare [aUIColor brightness]

编辑:

我在一些 github托管的项目,它的主旨是: https://gist.github.com/1252197


I found the same code in some github-hosted project, made a gist of it: https://gist.github.com/1252197

#import "UIColor-HSVAdditions.h"

@implementation UIColor (UIColor_HSVAdditions)
+(struct hsv_color)HSVfromRGB:(struct rgb_color)rgb
{
    struct hsv_color hsv;

    CGFloat rgb_min, rgb_max;
    rgb_min = MIN3(rgb.r, rgb.g, rgb.b);
    rgb_max = MAX3(rgb.r, rgb.g, rgb.b);

    hsv.val = rgb_max;
    if (hsv.val == 0) {
        hsv.hue = hsv.sat = 0;
        return hsv;
    }

    rgb.r /= hsv.val;
    rgb.g /= hsv.val;
    rgb.b /= hsv.val;
    rgb_min = MIN3(rgb.r, rgb.g, rgb.b);
    rgb_max = MAX3(rgb.r, rgb.g, rgb.b);

    hsv.sat = rgb_max - rgb_min;
    if (hsv.sat == 0) {
        hsv.hue = 0;
        return hsv;
    }

    if (rgb_max == rgb.r) {
        hsv.hue = 0.0 + 60.0*(rgb.g - rgb.b);
        if (hsv.hue < 0.0) {
            hsv.hue += 360.0;
        }
    } else if (rgb_max == rgb.g) {
        hsv.hue = 120.0 + 60.0*(rgb.b - rgb.r);
    } else /* rgb_max == rgb.b */ {
        hsv.hue = 240.0 + 60.0*(rgb.r - rgb.g);
    }

    return hsv;
}
-(CGFloat)hue
{
    struct hsv_color hsv;
    struct rgb_color rgb;
    rgb.r = [self red];
    rgb.g = [self green];
    rgb.b = [self blue];
    hsv = [UIColor HSVfromRGB: rgb];
    return (hsv.hue / 360.0);
}
-(CGFloat)saturation
{
    struct hsv_color hsv;
    struct rgb_color rgb;
    rgb.r = [self red];
    rgb.g = [self green];
    rgb.b = [self blue];
    hsv = [UIColor HSVfromRGB: rgb];
    return hsv.sat;
}
-(CGFloat)brightness
{
    struct hsv_color hsv;
    struct rgb_color rgb;
    rgb.r = [self red];
    rgb.g = [self green];
    rgb.b = [self blue];
    hsv = [UIColor HSVfromRGB: rgb];
    return hsv.val;
}
-(CGFloat)value
{
    return [self brightness];
}
@end

这篇关于UIColor比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆