Deciding whether text should be black or white for a given background color
Deciding whether text should be black or white for a given background color
- Subject: Deciding whether text should be black or white for a given background color
- From: Keith Blount <email@hidden>
- Date: Mon, 4 Dec 2006 09:36:25 -0800 (PST)
Hello,
I have a custom view that draws text on various "cards". Each of these cards can have any background colour, which is defined by a fully-customisable label colour assigned by the user.
By default, the text is drawn on these cards in black. However, if the user wants to use a really dark label colour (eg. dark purple), this makes the text really difficult to read. Ideally, I would like the text to be drawn in black for light-coloured backgrounds, and in white for dark-coloured backgrounds.
The problem is, how do I programmatically decide what is a light- and what is a dark-coloured background using methods availalbe to NSColor? Is there a well-known algorithm for deciding this? At first I thought that if I just checked if the red, green and blue components of the background colour were 0.5 or above (ie. nearer white), then I would use black text, and white otherwise. But obviously, that is silly, as it would fail for colours with two components very dark and one light (if you see what I mean).
So, my question is, is there a simple way of deciding whether black or white provides the best contrast for any given NSColor?
Many thanks in advance,
Keith
____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden