This blog has moved to http://teabass.com

There will be no more posts or comments on this site, please update your bookmarks and feeds to http://teabass.com.

Automatically pick text colour based on background colour

Friday, March 6th, 2009

If you let users choose a colour for a background in your web apps, you may end up with unreadable text due to a lack of contrast, this little ruby method takes a hex value (like a css colour) in the form of 0x###### where the #’s are the hex colours letters or numbers, and works out if the text colour would look better in black or white.

For example:

# black_or_white(0x53BFD0)
# => #FFFFFF

Leave a Comment