A function to calculate whether to use black or white text given a background color

The function

r, g, and b should be Number; intended to be an integer between 0 and 255 (inclusive). Return value is Boolean.

Also at Github gists and text-color.js. MY SITE IS NOT A CDN


Click for a new color. Colors are random, displayed text color is based on the above function

The sun did not shine.
It was too wet to play.
So we sat in the house
All that cold, cold, wet day.

Black. White.
If you never did
You should.
These things are fun.
and Fun is good.

Black. White.


There are lots of occasions where one needs to determine if text should be black or white based on the background of it. This function calculates whether black text should be used using a method that is fully W3C compliant. (All 16.7 million RGB values are tested against another function that is by definition compliant.)

This function is designed (to the best of my ability) to be simple to understand, fast, and short at the same. This is personally what what I see as good, you may modify it as you like.


See: W3C contrast calculating procedure

Contrast calculation, what my function is based on
Source of the 0.179 threshold

Placeholder text above:
- Dr. Suess. The Cat in the Hat. 1957.
- Dr. Suess. One Fish Two Fish Red Fish Blue Fish. 1960.