Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Although colors chosen by your method may contrast, they might still be unreadable. Have you ever seen a webpage with a pure blue background and pure yellow text? Perhaps a more eye-friendly method is to always use black or white foreground text, which would hardly clash with anything

To determine whether white or black would contrast more with your random background color, you can use the luminosity (brightness) of the background color. The luminosity is the value the color would become when converted to grayscale. What's nice is that human perception is built in -- the formula for luminosity takes into account the differences in how red, green, and blue contribute to the perception of "brightness". If the luminosity is brighter than 50% gray, use black text, otherwise use white text. The results are very readable.

sub random_colors { my ($r, $g, $b) = map { int rand 256 } 1 .. 3; my $lum = ($r * 0.3) + ($g * 0.59) + ($b * 0.11); my $bg = sprintf("#%02x%02x%02x", $r, $g, $b); my $fg = $lum < 128 ? "white" : "black"; return ($bg, $fg); } for (1 .. 10) { my ($bg, $fg) = random_colors(); print qq{ <table><tr><td bgcolor=$bg> <font color=$fg>THIS IS A TEST</font> </td></tr></table> }; }
THIS IS A TEST
THIS IS A TEST
THIS IS A TEST
THIS IS A TEST
THIS IS A TEST
THIS IS A TEST
THIS IS A TEST
THIS IS A TEST
THIS IS A TEST
THIS IS A TEST
Update: (This is for my own reference in the future as much as anything, so I can find these links when I need to.) To be nitpicky, choosing a "random" color by uniformly choosing random RGB components is not as random as one would think. RGB is not a perceptually uniform color space. A better method to generate colors that are perceptually random instead of numerically random in their RGB components would be to use a different color space like L*a*b* or L*u*v*, where the three color dimensions are closer to perceptually uniform. Uniformly select the three components in this color space, then convert to RGB. For more info, see the Color Space FAQ, questions C-35 and C-39.

But for the purposes of pg's app, uniformly choosing in RGB-space is probably quite sufficient, as we only need the color to be noticably different than the previous.

blokhead


In reply to Re: to generate a set of well contrasted colors by blokhead
in thread to generate a set of well contrasted colors by pg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-18 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found