UIColor comparison

前端 未结 5 447
[愿得一人]
[愿得一人] 2020-12-17 06:04

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 c

相关标签:
5条回答
  • 2020-12-17 06:45

    [UIColor CGColor] will get you a CGColorRef, from there you can do CGColorGetComponents to get the individual components. Getting the "brightness" value depends on your definition of brightness. Getting an average of the components (in case of RGB color space) might be a good start.

    0 讨论(0)
  • 2020-12-17 06:46

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

    Edit:
    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
    
    0 讨论(0)
  • 2020-12-17 06:51

    UIColor (and CGColorRef) are generally described in RGB values. If you want to determine light or dark, you'll probably want to convert these values to something like Hue/Saturation/Brightness. But there are no built in functions like you are looking for.

    0 讨论(0)
  • 2020-12-17 06:53

    Proposed algorithm to calculate color / color brightness difference: http://maestric.com/doc/color_brightness_difference_calculator (based on w3c paper)

    0 讨论(0)
  • 2020-12-17 07:02

    here is a guide (with code provided) on UIColor expansion (using a category) and has methods such as get hexStringFromColor: It should be what you're looking for. UIColor expansion Note: I did not write this blog or code.

    0 讨论(0)
提交回复
热议问题