Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts

    On colours and readability...

    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

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Ames, IA, USA
    Posts
    373
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Need more emoticons?
    Visit Catman's Private Stock

  • #3
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Quality Cheers mate.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •