I have found answers to this question that explain how to convert an NSString hex into a UIColor (I have done this) and others explaining how to convert RGB to HSB, and others explaining how to determine lightness and darkness of RGB, but I’m wondering if there is a more direct way of figuring this out that does not involve going hex->UIColor->rgb->hsb->b.
Hex->hsb, for instance? Or hex->rgb->brightness calculation?
I’ve got backgrounds that change color each time the view loads (hex values from XML), and I need to have the text on top change to white or black accordingly so it’ll stay legible.
Help would be much appreciated, I’ve been kicking this around for days now.
See Formula to determine brightness of RGB color.
Hex colors are usually RRGGBB or RRGGBBAA (alpha).
how to convert hexadecimal to RGB
To get three ints instead of a UIColor, you can modify the answer from that to:
(The function will probably only correctly handle RGB, not RGBA.)