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?
5 Answers
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

- 52,040
- 14
- 137
- 178
-
That's how I do it, if the value (V in HSV, aka brightness) is < 0.5 then it's dark. – progrmr Sep 01 '10 at 22:44
-
Thanks, this is perfect. I can just check if greater than or less than .5, like progmr suggests. – sol Sep 02 '10 at 00:09
-
1As of June 2011, the linked-to category is dead. What was the category's name? Any unique method names? These could be used to search for the code from another source. – Jeremy W. Sherman Jun 06 '11 at 15:00
-
maybe this is helpful for u http://craigcoded.com/2010/11/30/getting-hsv-from-uicolor/ – vikingosegundo Sep 29 '11 at 22:47
-
or this: http://stackoverflow.com/questions/5284427/how-do-i-get-the-hue-saturation-and-brightness-from-a-uicolor – vikingosegundo Sep 29 '11 at 23:06
-
Yeah: found the original category embedded in some other project at github: https://github.com/davidblackuk/Dashboards/blob/a8bb2d0bb7103782042107919192712602d29de9/IOSDashBoards/IOSDashBoards/UIColor-HSVAdditions.m – vikingosegundo Sep 29 '11 at 23:11
-
The perception of brightness is dependant on the hue, hence this approach is too simplistic. You can't just compute the brightness from the components that way. – alecail Mar 22 '13 at 18:20
-
I know that, but it give you a clue, while rgb can't give u even that – vikingosegundo Mar 22 '13 at 19:36
-
Since iOS 5 new instance methods were added. See "getHue:saturation:brightness:alpha:" https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIColor_Class/Reference/Reference.html – Schoob Mar 20 '14 at 07:22
-
@Schoob, yeah, here is an example: http://stackoverflow.com/questions/8265233/how-do-i-use-gethuesaturationbrightnessalpha/8265301#8265301 – vikingosegundo Mar 20 '14 at 09:30
[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.

- 29,501
- 5
- 55
- 57
-
I wouldn't use the average, the human eye is peak responsive. Use the brightest of the RGB components to gauge brightness. 00FF00 will seem brighter than 555555 but they have the same average. – progrmr Sep 01 '10 at 22:49
-
1This wont work very well — an example: the colors rgb(108 11 11) and rgb(107 91 12) actually have the same brightness (0.42) but the sums (and so the averages) are 130 and 210. not really close. – vikingosegundo Sep 01 '10 at 23:04
-
as I said, it depends very much on how you define "brightness". Human eye is not the only possible measurement device. – unbeli Sep 02 '10 at 18:21
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.

- 5,393
- 2
- 22
- 14
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.

- 9,780
- 1
- 41
- 56
-
This is good. hexStringFromColor gives me the hex string in the format "01f3df". But I can't figure out how to convert that to an actual hex number for comparison. – sol Sep 01 '10 at 21:58
-
the r g b values wont help, as this model says nothing about brightness of a color. You can tell it for each of its components, but not for all of them together. – vikingosegundo Sep 01 '10 at 22:50
Proposed algorithm to calculate color / color brightness difference: http://maestric.com/doc/color_brightness_difference_calculator (based on w3c paper)

- 2,446
- 1
- 18
- 30