View Full Version : On colours and readability...

04-14-2003, 06:28 PM
I'm sure this has been raised before, but:

Say I'm pulling out some text content from a database; the content has a hex colour value attached to it. If it's a light colour, I want it on a dark background. And vice versa.

I could just knock up a script that takes the hex value for the curent colour and makes another; by reversing it for example. (We're not necessarily dealing with websafe colours here :))

But I'd like, basically, for any colours that would show up best on black to be on black, and any colours that read best on white to be on white.

Can I somehow work out the "best colour to be on" from the hex? Like, if it's mainly letters, or if the 3 values for RGB are roughly similar? Am I just off in Spudworld again?

And is case you hadn't guessed, this is more of an exercise than a practical problem, so "add the background colour as a database field" is not a valid answer ;)

04-14-2003, 06:55 PM
I'd calculate the brightness using this formula (derived from one you can find at W3C in their Accessiblity information):

B = (299(Rv * Rv * Rv) + 587(Gv * Gv * Gv) + 114(Bv * Bv * Bv))/1658137500

where Rv, Gv, and Bv refer to red, green, and blue values. Cubing produces better relative brightness values than using the base color values.

With this formula, black is 0 and white is 10, so deciding which should be merely a matter selecting black or white depending on whether B is greater or lesser than 5.

04-14-2003, 06:59 PM
Quality :) Cheers mate.:thumbsup:

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum