0

Possible Duplicate:
Check if UIColor is dark or bright?

Is there simple way to determine if a UIColor is dark or light? I'd like to put a label on a dynamic background and change its text color to white if the background's dark or to black if its a light color.

Community
  • 1
  • 1
Nico Reese
  • 261
  • 6
  • 14
  • Duplicate question. [http://stackoverflow.com/questions/2509443/check-if-uicolor-is-dark-or-bright][1] [1]: http://stackoverflow.com/questions/2509443/check-if-uicolor-is-dark-or-bright – Mark McCorkle Dec 05 '12 at 15:46

2 Answers2

6

I haven't tested this, but it may work for you...

-(BOOL) isLightColor:(UIColor*)clr {
    CGFloat white = 0;
    [clr getWhite:&white alpha:nil];
    return (white >= 0.5);
}
jjv360
  • 4,120
  • 3
  • 23
  • 37
  • 1
    The code above only works if the colour is grayscale, if it is RGB it will return white will have a value of 0. Check the code below works better: – Paulo Sep 23 '13 at 11:22
1

Calculate the color contrast between your label and background and decide on your color from there. Generally, this will involve getting the components of the colors in question.

If you google "Calculate Color Contrast" sans the quotes, you'll find some links. You may not find anything iOS specific, but you should be able to adapt the code you find, especially since they'll generally involve a function of the RGB, which is straight-forward.

RonaldBarzell
  • 3,822
  • 1
  • 16
  • 23